Skip to content

Commit 9e3eeb6

Browse files
committed
dependency and segment improvements
- added possibility to exclude directories from dependency scanning - added more flexibility to control max number of files per segment
1 parent 71ef6e2 commit 9e3eeb6

File tree

6 files changed

+229
-34
lines changed

6 files changed

+229
-34
lines changed

lib/GPH/PHPStan/Cache.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package GPH::PHPStan::Cache;
33
use strict;
44
use warnings FATAL => 'all';
55

6+
use Cwd;
7+
68
sub new {
79
my ($class, %args) = @_;
810

@@ -26,7 +28,7 @@ sub parseResultCache {
2628

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

29-
open $fh, '<', $args{path} or die "unable to open cache file: $!";
31+
open $fh, '<', getcwd() . '/' . $args{path} or die "unable to open cache file: $!";
3032

3133

3234
while ($line = <$fh>) {
@@ -119,7 +121,7 @@ the C<new> method creates a new GPH::PHPUnit::Config. it takes a hash of options
119121
120122
=item path B<(required)>
121123
122-
path to the C<resultCache.php> file
124+
path to the C<resultCache.php> file relative to the script execution path.
123125
124126
=item depth
125127

lib/GPH/Util/Files.pm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ sub segment {
2525
push(@{$result{$group}}, $path);
2626
}
2727

28-
return(%result) unless defined $args{max};
28+
return(%result) unless exists($args{max}) or exists($args{segment_max});
2929

3030
foreach $key (keys %result) {
31+
my $max = $args{segment_max}{$key} || $args{max} || 1000;
3132
$size = scalar(@{$result{$key}});
32-
next unless $size > $args{max};
33+
next unless $size > $max;
3334

3435
my $index = 1;
3536

36-
while (scalar(@{$result{$key}}) > $args{max}) {
37-
my @segment = splice @{$result{$key}}, 0, $args{max};
37+
while (scalar(@{$result{$key}}) > $max) {
38+
my @segment = splice @{$result{$key}}, 0, $max;
3839
$result{$key . '.' . $index} = \@segment;
3940
$index++;
4041
}
@@ -85,6 +86,10 @@ the path depth from which to create the segments. defaults to 1.
8586
8687
the maximum number of files per segment.
8788
89+
=item segment_max
90+
91+
a hash defining the max number of files per segment name (e.g. C<< segment_max => {'tests.Unit' => 1000} >>)
92+
8893
=back
8994
9095
=back

lib/GPH/Util/PhpDependencyParser.pm

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,29 @@ sub new {
1919
}
2020

2121
sub dir {
22-
my ($self, $dir, $strip) = @_;
22+
my ($self, %args) = @_;
2323

24-
my @files = File::Find::Rule->file()
25-
->name('*.php')
26-
->in($dir)
27-
;
24+
(exists($args{directories}) && exists($args{strip})) or die "$!";
25+
26+
my $rule = File::Find::Rule->new;
27+
28+
if (exists($args{excludes})) {
29+
$rule->or(
30+
$rule->new->exec(sub {
31+
my ($shortname, $path, $fullname) = @_;
32+
foreach my $exclude (@{$args{excludes}}) {
33+
return 1 if $fullname =~ $exclude;
34+
}
35+
return 0;
36+
})->prune->discard,
37+
$rule->new
38+
);
39+
}
40+
41+
my @files = $rule->name('*.php')->in(@{$args{directories}});
2842

2943
foreach my $file (@files) {
30-
$self->parse($file, $strip);
44+
$self->parse($file, $args{strip});
3145
}
3246

3347
return ($self);
@@ -226,10 +240,26 @@ GPH::Util::PhpDependencyParser - parses one or more php files and builds a depen
226240
227241
the C<new> method creates a new GPH::Util::PhpDependencyParser.
228242
229-
=item C<< -E<gt>dir($directory, $strip) >>
243+
=item C<< -E<gt>dir(%args) >>
230244
231-
scans and builds a dependency map from all php files in C< $directory >. the resulting paths will be stripped of the
232-
prefix defined in C<$strip>
245+
scans and builds a dependency map from all php files in defined directories. the resulting paths will be stripped of the
246+
prefix if defined in the C<$strip> argument. the dir method takes a hash of options, valid option keys include:
247+
248+
=over
249+
250+
=item directories B<(required)>
251+
252+
an array of directory paths (relative to script execution) to scan
253+
254+
=item strip B<(required)>
255+
256+
the prefix to strip from the paths
257+
258+
=item excludes
259+
260+
an array of directory paths (relative to script execution) to exclude from the scan
261+
262+
=back
233263
234264
=item C<< -E<gt>parse($filepath, $strip) >>
235265

t/unit/GPH/Util/Files.t

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,68 @@ describe "class `$CLASS` segment method" => sub {
132132
'object as expected'
133133
) or diag Dumper(%segments);
134134
};
135+
136+
tests 'segment with depth 2 and max 1 and segment max' => sub {
137+
my ($object, $exception, $warnings, %segments);
138+
139+
$exception = dies {
140+
$warnings = warns {
141+
$object = $CLASS->new();
142+
%segments = $object->segment((paths => @files, depth => 2, max => 1, segment_max => {'tests.Functional' => 2}));
143+
};
144+
};
145+
146+
is($exception, undef, 'no exception thrown');
147+
is($warnings, 0, 'no warnings generated');
148+
149+
is(
150+
\%segments,
151+
hash {
152+
field "tests.Unit" => array {
153+
item 'tests/Unit/Parser/MapperTest.php';
154+
end;
155+
};
156+
field "tests.Functional" => array {
157+
item 'tests/Functional/Parser/MapperTest.php';
158+
item 'tests/Functional/Parser/MapperTestCase.php';
159+
end;
160+
};
161+
end;
162+
},
163+
'object as expected'
164+
) or diag Dumper(%segments);
165+
};
166+
167+
tests 'segment with depth 2, without max and segment max' => sub {
168+
my ($object, $exception, $warnings, %segments);
169+
170+
$exception = dies {
171+
$warnings = warns {
172+
$object = $CLASS->new();
173+
%segments = $object->segment((paths => @files, depth => 2, segment_max => {'tests.Unit' => 1}));
174+
};
175+
};
176+
177+
is($exception, undef, 'no exception thrown');
178+
is($warnings, 0, 'no warnings generated');
179+
180+
is(
181+
\%segments,
182+
hash {
183+
field "tests.Unit" => array {
184+
item 'tests/Unit/Parser/MapperTest.php';
185+
end;
186+
};
187+
field "tests.Functional" => array {
188+
item 'tests/Functional/Parser/MapperTest.php';
189+
item 'tests/Functional/Parser/MapperTestCase.php';
190+
end;
191+
};
192+
end;
193+
},
194+
'object as expected'
195+
) or diag Dumper(%segments);
196+
};
135197
};
136198

137199
done_testing();

0 commit comments

Comments
 (0)