Skip to content

Commit 71ef6e2

Browse files
committed
scoped phpunit
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
1 parent c483d40 commit 71ef6e2

File tree

23 files changed

+2186
-12
lines changed

23 files changed

+2186
-12
lines changed

Makefile.PL

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ WriteMakefile(
1010
VERSION_FROM => 'lib/GPH.pm',
1111
LICENSE => 'perl',
1212
PREREQ_PM => {
13-
"File::Basename" => 0,
14-
"Time::Piece" => 0,
15-
"XML::LibXML" => 0,
16-
"Cwd" => 0
13+
"File::Basename" => 0,
14+
"Time::Piece" => 0,
15+
"XML::LibXML" => 0,
16+
"Cwd" => 0,
17+
"File::Find::Rule" => 0,
1718
},
1819
CONFIGURE_REQUIRES => {
1920
"ExtUtils::MakeMaker" => 0

lib/GPH.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package GPH;
33
use strict;
44
use warnings FATAL => 'all';
55

6-
our $VERSION = '1.2.1';
6+
our $VERSION = '1.3.0';
77

88
1;
99

lib/GPH/Composer.pm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,23 @@ sub match {
3737
return 0;
3838
}
3939

40+
sub getNamespaces {
41+
my ($self, @paths) = @_;
42+
my (%reversed, @result);
43+
%reversed = reverse %{$self->{classmap}};
44+
45+
foreach my $path (@paths) {
46+
$path = '/' . $path if rindex $path, '/', 0;
47+
48+
next if !defined $reversed{$path};
49+
push(@result, $reversed{$path});
50+
}
51+
52+
return(@result);
53+
};
54+
4055
sub parseClassMap {
4156
my ($self, $path) = @_;
42-
my %classmap = ();
4357

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

@@ -107,6 +121,10 @@ matches a FQCN to the classmap limited by a collection of paths. returns C<1> on
107121
108122
returns reference to the parsed classmap hash.
109123
124+
=item C<< -E<gt>getNamespaces(@paths) >>
125+
126+
returns a list of namespaces for given paths.
127+
110128
=item C<< -E<gt>parseClassMap() >> B<(internal)>
111129
112130
parses the classmap file with relevant paths (vendor dir is ignored) and stores it in a hash map.

lib/GPH/PHPStan/Cache.pm

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package GPH::PHPStan::Cache;
2+
3+
use strict;
4+
use warnings FATAL => 'all';
5+
6+
sub new {
7+
my ($class, %args) = @_;
8+
9+
my $self = {
10+
depth => $args{depth} || 1,
11+
relative => $args{relative} || undef,
12+
dependencies => undef,
13+
};
14+
15+
bless $self, $class;
16+
17+
return $self;
18+
}
19+
20+
sub parseResultCache {
21+
my ($self, %args) = @_;
22+
my ($fh, $line, $key);
23+
my $in_array = 0;
24+
my $in_dependant = 0;
25+
my $in_dependent_files = 0;
26+
27+
(exists($args{path})) or die "$!";
28+
29+
open $fh, '<', $args{path} or die "unable to open cache file: $!";
30+
31+
32+
while ($line = <$fh>) {
33+
chomp $line;
34+
35+
if ($line =~ /^\s*'dependencies'\s*=>\s*array\s*\($/) {
36+
$in_array = 1;
37+
} elsif ($in_array && $in_dependant == 0 && $line =~ /^\s*'([^']+)'\s*=>\s*$/) {
38+
$key = $self->relative($1);
39+
$in_dependant = 1;
40+
} elsif ($in_array && $in_dependant && $line =~ /^\s*'dependentFiles'\s*=>\s*$/) {
41+
$in_dependent_files = 1;
42+
} elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*[0-9]+\s*=>\s*'([^']+)',$/) {
43+
push(@{$self->{dependencies}{$key}}, $self->relative($1));
44+
} elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*\),\s*$/) {
45+
$in_dependent_files = 0;
46+
} elsif ($in_array && $in_dependant && $in_dependent_files == 0 && $line =~ /^\s*\),\s*$/) {
47+
$in_dependant = 0;
48+
}
49+
}
50+
51+
close($fh);
52+
53+
return($self);
54+
}
55+
56+
sub relative {
57+
my ($self, $line) = @_;
58+
59+
if (!defined $self->{relative}) {
60+
return($line);
61+
}
62+
63+
return substr $line, index($line, $self->{relative});
64+
};
65+
66+
sub dependencies {
67+
my ($self, @paths) = @_;
68+
my (@unique, @result);
69+
@result = @paths;
70+
71+
for (my $i = 1; $i <= $self->{depth}; $i++) {
72+
push(@result, $self->iterate(@result));
73+
}
74+
75+
@unique = do { my %seen; grep { !$seen{$_}++ } @result };
76+
77+
return(@unique);
78+
};
79+
80+
sub iterate {
81+
my ($self, @paths) = @_;
82+
my ($path, $dependant, @unique, @result);
83+
@result = @paths;
84+
85+
foreach $path (@paths) {
86+
for $dependant (@{$self->{dependencies}{$path}}) {
87+
push(@result, $dependant);
88+
}
89+
}
90+
91+
@unique = do { my %seen; grep { !$seen{$_}++ } @result };
92+
93+
return(@unique);
94+
};
95+
96+
1;
97+
98+
__END__
99+
100+
=head1 NAME
101+
102+
GPH::PHPStan::Cache - parse dependencies from phpstan's resultCache.php file.
103+
104+
=head1 SYNOPSIS
105+
106+
use GPH::PHPStan::Cache;
107+
108+
my $cache = GPH::PHPStan::Cache->new((depth => 1, relative => 'src/'));
109+
110+
=head1 METHODS
111+
112+
=over 4
113+
114+
=item C<< -E<gt>new(%args) >>
115+
116+
the C<new> method creates a new GPH::PHPUnit::Config. it takes a hash of options, valid option keys include:
117+
118+
=over
119+
120+
=item path B<(required)>
121+
122+
path to the C<resultCache.php> file
123+
124+
=item depth
125+
126+
depth of the dependency scan. defaults to 1. setting it to 2 for instance will retrieve the dependencies of the dependencies as well.
127+
128+
=item relative
129+
130+
when you want relative paths, to which directory should they be relative
131+
132+
=back
133+
134+
=item C<< -E<gt>parseResultCache(%config) >>
135+
136+
parses the cache file. it takes a hash of options, valid option keys include:
137+
138+
=over
139+
140+
=item path B<(required)>
141+
142+
path to the C<resultCache.php> file
143+
144+
=back
145+
146+
=item C<< -E<gt>relative($line) >> B<(internal)>
147+
148+
converts file path to relative path
149+
150+
=item C<< -E<gt>dependencies(@paths) >>
151+
152+
collects all dependencies for given C<@paths> and given C<$depth>
153+
154+
=item C<< -E<gt>iterate(@paths) >> B<(internal)>
155+
156+
collects all dependencies for given C<@paths>
157+
158+
=back
159+
160+
=head1 AUTHOR
161+
162+
the GPH::PHPUnit::Config module was written by wicliff wolda <[email protected]>
163+
164+
=head1 COPYRIGHT AND LICENSE
165+
166+
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
167+
168+
=cut

0 commit comments

Comments
 (0)