Skip to content

Commit

Permalink
scoped phpunit
Browse files Browse the repository at this point in the history
experimental stuff in order to try and create scoped phpunit jobs

- added php dependency parser
- added phpunit config module
- added files util
- added phpstan cache parser
  • Loading branch information
wickedOne committed May 18, 2024
1 parent c483d40 commit 71ef6e2
Show file tree
Hide file tree
Showing 23 changed files with 2,186 additions and 12 deletions.
9 changes: 5 additions & 4 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ WriteMakefile(
VERSION_FROM => 'lib/GPH.pm',
LICENSE => 'perl',
PREREQ_PM => {
"File::Basename" => 0,
"Time::Piece" => 0,
"XML::LibXML" => 0,
"Cwd" => 0
"File::Basename" => 0,
"Time::Piece" => 0,
"XML::LibXML" => 0,
"Cwd" => 0,
"File::Find::Rule" => 0,
},
CONFIGURE_REQUIRES => {
"ExtUtils::MakeMaker" => 0
Expand Down
2 changes: 1 addition & 1 deletion lib/GPH.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package GPH;
use strict;
use warnings FATAL => 'all';

our $VERSION = '1.2.1';
our $VERSION = '1.3.0';

1;

Expand Down
20 changes: 19 additions & 1 deletion lib/GPH/Composer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ sub match {
return 0;
}

sub getNamespaces {
my ($self, @paths) = @_;
my (%reversed, @result);
%reversed = reverse %{$self->{classmap}};

foreach my $path (@paths) {
$path = '/' . $path if rindex $path, '/', 0;

next if !defined $reversed{$path};
push(@result, $reversed{$path});
}

return(@result);
};

sub parseClassMap {
my ($self, $path) = @_;
my %classmap = ();

open(my $fh, '<', $path) or die "can't open classmap file $!";

Expand Down Expand Up @@ -107,6 +121,10 @@ matches a FQCN to the classmap limited by a collection of paths. returns C<1> on
returns reference to the parsed classmap hash.
=item C<< -E<gt>getNamespaces(@paths) >>
returns a list of namespaces for given paths.
=item C<< -E<gt>parseClassMap() >> B<(internal)>
parses the classmap file with relevant paths (vendor dir is ignored) and stores it in a hash map.
Expand Down
168 changes: 168 additions & 0 deletions lib/GPH/PHPStan/Cache.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package GPH::PHPStan::Cache;

use strict;
use warnings FATAL => 'all';

sub new {
my ($class, %args) = @_;

my $self = {
depth => $args{depth} || 1,
relative => $args{relative} || undef,
dependencies => undef,
};

bless $self, $class;

return $self;
}

sub parseResultCache {
my ($self, %args) = @_;
my ($fh, $line, $key);
my $in_array = 0;
my $in_dependant = 0;
my $in_dependent_files = 0;

(exists($args{path})) or die "$!";

open $fh, '<', $args{path} or die "unable to open cache file: $!";


while ($line = <$fh>) {
chomp $line;

if ($line =~ /^\s*'dependencies'\s*=>\s*array\s*\($/) {
$in_array = 1;
} elsif ($in_array && $in_dependant == 0 && $line =~ /^\s*'([^']+)'\s*=>\s*$/) {
$key = $self->relative($1);
$in_dependant = 1;
} elsif ($in_array && $in_dependant && $line =~ /^\s*'dependentFiles'\s*=>\s*$/) {
$in_dependent_files = 1;
} elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*[0-9]+\s*=>\s*'([^']+)',$/) {
push(@{$self->{dependencies}{$key}}, $self->relative($1));
} elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*\),\s*$/) {
$in_dependent_files = 0;
} elsif ($in_array && $in_dependant && $in_dependent_files == 0 && $line =~ /^\s*\),\s*$/) {
$in_dependant = 0;
}
}

close($fh);

return($self);
}

sub relative {
my ($self, $line) = @_;

if (!defined $self->{relative}) {
return($line);
}

return substr $line, index($line, $self->{relative});
};

sub dependencies {
my ($self, @paths) = @_;
my (@unique, @result);
@result = @paths;

for (my $i = 1; $i <= $self->{depth}; $i++) {
push(@result, $self->iterate(@result));
}

@unique = do { my %seen; grep { !$seen{$_}++ } @result };

return(@unique);
};

sub iterate {
my ($self, @paths) = @_;
my ($path, $dependant, @unique, @result);
@result = @paths;

foreach $path (@paths) {
for $dependant (@{$self->{dependencies}{$path}}) {
push(@result, $dependant);
}
}

@unique = do { my %seen; grep { !$seen{$_}++ } @result };

return(@unique);
};

1;

__END__
=head1 NAME
GPH::PHPStan::Cache - parse dependencies from phpstan's resultCache.php file.
=head1 SYNOPSIS
use GPH::PHPStan::Cache;
my $cache = GPH::PHPStan::Cache->new((depth => 1, relative => 'src/'));
=head1 METHODS
=over 4
=item C<< -E<gt>new(%args) >>
the C<new> method creates a new GPH::PHPUnit::Config. it takes a hash of options, valid option keys include:
=over
=item path B<(required)>
path to the C<resultCache.php> file
=item depth
depth of the dependency scan. defaults to 1. setting it to 2 for instance will retrieve the dependencies of the dependencies as well.
=item relative
when you want relative paths, to which directory should they be relative
=back
=item C<< -E<gt>parseResultCache(%config) >>
parses the cache file. it takes a hash of options, valid option keys include:
=over
=item path B<(required)>
path to the C<resultCache.php> file
=back
=item C<< -E<gt>relative($line) >> B<(internal)>
converts file path to relative path
=item C<< -E<gt>dependencies(@paths) >>
collects all dependencies for given C<@paths> and given C<$depth>
=item C<< -E<gt>iterate(@paths) >> B<(internal)>
collects all dependencies for given C<@paths>
=back
=head1 AUTHOR
the GPH::PHPUnit::Config module was written by wicliff wolda <[email protected]>
=head1 COPYRIGHT AND LICENSE
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
Loading

0 comments on commit 71ef6e2

Please sign in to comment.