Skip to content

Commit

Permalink
dependency and segment improvements
Browse files Browse the repository at this point in the history
- added possibility to exclude directories from dependency scanning
- added more flexibility to control max number of files per segment
  • Loading branch information
wickedOne committed Jun 8, 2024
1 parent 71ef6e2 commit 9e3eeb6
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 34 deletions.
6 changes: 4 additions & 2 deletions lib/GPH/PHPStan/Cache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package GPH::PHPStan::Cache;
use strict;
use warnings FATAL => 'all';

use Cwd;

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

Expand All @@ -26,7 +28,7 @@ sub parseResultCache {

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

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


while ($line = <$fh>) {
Expand Down Expand Up @@ -119,7 +121,7 @@ the C<new> method creates a new GPH::PHPUnit::Config. it takes a hash of options
=item path B<(required)>
path to the C<resultCache.php> file
path to the C<resultCache.php> file relative to the script execution path.
=item depth
Expand Down
13 changes: 9 additions & 4 deletions lib/GPH/Util/Files.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ sub segment {
push(@{$result{$group}}, $path);
}

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

foreach $key (keys %result) {
my $max = $args{segment_max}{$key} || $args{max} || 1000;
$size = scalar(@{$result{$key}});
next unless $size > $args{max};
next unless $size > $max;

my $index = 1;

while (scalar(@{$result{$key}}) > $args{max}) {
my @segment = splice @{$result{$key}}, 0, $args{max};
while (scalar(@{$result{$key}}) > $max) {
my @segment = splice @{$result{$key}}, 0, $max;
$result{$key . '.' . $index} = \@segment;
$index++;
}
Expand Down Expand Up @@ -85,6 +86,10 @@ the path depth from which to create the segments. defaults to 1.
the maximum number of files per segment.
=item segment_max
a hash defining the max number of files per segment name (e.g. C<< segment_max => {'tests.Unit' => 1000} >>)
=back
=back
Expand Down
48 changes: 39 additions & 9 deletions lib/GPH/Util/PhpDependencyParser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@ sub new {
}

sub dir {
my ($self, $dir, $strip) = @_;
my ($self, %args) = @_;

my @files = File::Find::Rule->file()
->name('*.php')
->in($dir)
;
(exists($args{directories}) && exists($args{strip})) or die "$!";

my $rule = File::Find::Rule->new;

if (exists($args{excludes})) {
$rule->or(
$rule->new->exec(sub {
my ($shortname, $path, $fullname) = @_;
foreach my $exclude (@{$args{excludes}}) {
return 1 if $fullname =~ $exclude;
}
return 0;
})->prune->discard,
$rule->new
);
}

my @files = $rule->name('*.php')->in(@{$args{directories}});

foreach my $file (@files) {
$self->parse($file, $strip);
$self->parse($file, $args{strip});
}

return ($self);
Expand Down Expand Up @@ -226,10 +240,26 @@ GPH::Util::PhpDependencyParser - parses one or more php files and builds a depen
the C<new> method creates a new GPH::Util::PhpDependencyParser.
=item C<< -E<gt>dir($directory, $strip) >>
=item C<< -E<gt>dir(%args) >>
scans and builds a dependency map from all php files in C< $directory >. the resulting paths will be stripped of the
prefix defined in C<$strip>
scans and builds a dependency map from all php files in defined directories. the resulting paths will be stripped of the
prefix if defined in the C<$strip> argument. the dir method takes a hash of options, valid option keys include:
=over
=item directories B<(required)>
an array of directory paths (relative to script execution) to scan
=item strip B<(required)>
the prefix to strip from the paths
=item excludes
an array of directory paths (relative to script execution) to exclude from the scan
=back
=item C<< -E<gt>parse($filepath, $strip) >>
Expand Down
File renamed without changes.
62 changes: 62 additions & 0 deletions t/unit/GPH/Util/Files.t
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,68 @@ describe "class `$CLASS` segment method" => sub {
'object as expected'
) or diag Dumper(%segments);
};

tests 'segment with depth 2 and max 1 and segment max' => sub {
my ($object, $exception, $warnings, %segments);

$exception = dies {
$warnings = warns {
$object = $CLASS->new();
%segments = $object->segment((paths => @files, depth => 2, max => 1, segment_max => {'tests.Functional' => 2}));
};
};

is($exception, undef, 'no exception thrown');
is($warnings, 0, 'no warnings generated');

is(
\%segments,
hash {
field "tests.Unit" => array {
item 'tests/Unit/Parser/MapperTest.php';
end;
};
field "tests.Functional" => array {
item 'tests/Functional/Parser/MapperTest.php';
item 'tests/Functional/Parser/MapperTestCase.php';
end;
};
end;
},
'object as expected'
) or diag Dumper(%segments);
};

tests 'segment with depth 2, without max and segment max' => sub {
my ($object, $exception, $warnings, %segments);

$exception = dies {
$warnings = warns {
$object = $CLASS->new();
%segments = $object->segment((paths => @files, depth => 2, segment_max => {'tests.Unit' => 1}));
};
};

is($exception, undef, 'no exception thrown');
is($warnings, 0, 'no warnings generated');

is(
\%segments,
hash {
field "tests.Unit" => array {
item 'tests/Unit/Parser/MapperTest.php';
end;
};
field "tests.Functional" => array {
item 'tests/Functional/Parser/MapperTest.php';
item 'tests/Functional/Parser/MapperTestCase.php';
end;
};
end;
},
'object as expected'
) or diag Dumper(%segments);
};
};

done_testing();
Expand Down
Loading

0 comments on commit 9e3eeb6

Please sign in to comment.