From 3e75c82444b04aee4600b8718cd5e9ac91c49a4a Mon Sep 17 00:00:00 2001 From: wickedOne Date: Sun, 11 Feb 2024 07:23:39 +0100 Subject: [PATCH] perlcritic fixes --- Makefile.PL | 1 + README.md | 2 +- lib/GPH/Composer.pm | 21 ++++++++++----------- lib/GPH/Gitlab.pm | 17 ++++++++++------- lib/GPH/Infection.pm | 2 +- lib/GPH/PHPUnit.pm | 6 +++--- t/unit/GPH/Composer.t | 13 +++++++------ t/unit/GPH/Gitlab.t | 8 +++++--- t/unit/GPH/Infection.t | 12 ++++++------ t/unit/GPH/PHPUnit.t | 26 ++++++++++++-------------- 10 files changed, 56 insertions(+), 52 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 6182fe0..1871707 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,7 @@ WriteMakefile( "Test2::V0" => 0, "Test2::Tools::Spec" => 0, "Data::Dumper" => 0, + "Readonly" => 0, }, 'test' => { TESTS => 't/unit/GPH/*.t t/unit/GPH/*/*.t' diff --git a/README.md b/README.md index a63b41a..15ba05e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ the following environment variables are used by all scripts most scripts generate a custom config file on the fly. for more details on supported (static) analysis tools and implementation examples see the following: -1. [PHPUNIT](doc/PHPUnit.md) +1. [PHPUnit](doc/PHPUnit.md) 2. [Infection](doc/Infection.md) 3. [Psalm](doc/Psalm.md) 4. [PHPStan](doc/PHPStan.md) diff --git a/lib/GPH/Composer.pm b/lib/GPH/Composer.pm index 744bd78..f4b50f1 100755 --- a/lib/GPH/Composer.pm +++ b/lib/GPH/Composer.pm @@ -12,11 +12,6 @@ package GPH::Composer; use strict; use warnings FATAL => 'all'; -#------------------------------------------------------------------------------ -# Static properties -# classmap contains hash of all relevant classes and their paths -my %classmap = (); - #------------------------------------------------------------------------------ # Construct new class # @@ -67,15 +62,19 @@ sub match { # Returns: hash reference with ${classname} => $path sub parseClassMap { my ($self, $path) = @_; + my %classmap = (); + + open(my $fh, '<', $path) or die "can't open classmap file $!"; - open(my $fh, $path) or die "can't open classmap file $!"; + my @lines = <$fh>; + + close($fh); - while (<$fh>) { - chomp $_; - next unless /\$baseDir\s\./; + for my $line (@lines) { + next unless $line =~ /\$baseDir\s\./; - my ($class, $path) = split / => \$baseDir \. /; - $classmap{strip($class)} = strip($path); + my ($class, $code_path) = split(/ => \$baseDir \. /, $line); + $classmap{strip($class)} = strip($code_path); } close($fh); diff --git a/lib/GPH/Gitlab.pm b/lib/GPH/Gitlab.pm index b31c03d..ed46d64 100755 --- a/lib/GPH/Gitlab.pm +++ b/lib/GPH/Gitlab.pm @@ -26,26 +26,29 @@ sub new { my ($class, $path, $owner, @excludes) = @_; my (%codeowners, %excludeHash, %blacklist); - open(my $fh, $path) or die "unable to open codeowners file, initialization failed $!"; - # build excludes hash for quick lookup foreach my $item (@excludes) { $excludeHash{$item} = 1; } - while (<$fh>) { - chomp $_; + open(my $fh, '<', $path) or die "unable to open codeowners file, initialization failed $!"; + + my @lines = <$fh>; + + close($fh); + + for my $line (@lines) { # skip if line does not contain @ - next unless /^.*\s\@[\w]+\/.*$/; + next unless $line =~ /^.*\s\@[\w]+\/.*$/x; - my ($class_path, $owners) = split(' ', $_, 2); + my ($class_path, $owners) = split(' ', $line, 2); # skip if path is excluded next if exists $excludeHash{$class_path}; foreach (split(' ', $owners)) { - next unless /(\@[\w\-\/]{0,})$/; + next unless /(\@[\w\-\/]{0,})$/x; if (not exists $codeowners{$1}) { $codeowners{$1} = []; diff --git a/lib/GPH/Infection.pm b/lib/GPH/Infection.pm index 1050c11..627fc19 100644 --- a/lib/GPH/Infection.pm +++ b/lib/GPH/Infection.pm @@ -43,7 +43,7 @@ sub parse { my $msi = 0; my $covered = 0; - while () { + while (<>) { if ($_ =~ '([\d]+) covered mutants were not detected') { $escapees = $1; } diff --git a/lib/GPH/PHPUnit.pm b/lib/GPH/PHPUnit.pm index f5e0821..10bc503 100755 --- a/lib/GPH/PHPUnit.pm +++ b/lib/GPH/PHPUnit.pm @@ -52,7 +52,7 @@ sub new { bless $self, $class; if (defined $baseline) { - open(my $fh, $baseline) or die "unable to open phpunit baseline file ${baseline} $!"; + open(my $fh, '<', $baseline) or die "unable to open phpunit baseline file ${baseline} $!"; my @lines = (); while (<$fh>) { @@ -74,7 +74,7 @@ sub new { sub parse { my ($self) = @_; - while () { + while (<>) { chomp $_; # ignore lines with spaces @@ -84,7 +84,7 @@ sub parse { next if $self->{composer}->match($_, @{$self->{baseline}}); next if $self->{composer}->match($_, @{$self->{gitlab}->getBlacklistPaths()}); # read next line for stats - my $stats = ; + my $stats = <>; $self->{classreport}{$_} = $stats; $self->{stats}->add($stats); diff --git a/t/unit/GPH/Composer.t b/t/unit/GPH/Composer.t index 08ac3e4..f6f288e 100644 --- a/t/unit/GPH/Composer.t +++ b/t/unit/GPH/Composer.t @@ -1,17 +1,18 @@ #!/usr/bin/perl package t::unit::GPH::Composer; +use strict; use warnings; use Test2::V0 -target => 'GPH::Composer'; use Test2::Tools::Spec; + use Data::Dumper; +use Readonly; -use constant CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php'; +local $SIG{__WARN__} = sub {}; -local $SIG{__WARN__} = sub { - # warn $_[0] unless ($_[0] =~ /^Module::Pluggable will be removed/); -}; +Readonly my $CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php'; describe "class `$CLASS`" => sub { tests 'it can be instantiated' => sub { @@ -23,7 +24,7 @@ describe "class `$CLASS`" => sub { $exception = dies { $warnings = warns { - $object = $CLASS->new(CLASSMAP_FILE); + $object = $CLASS->new($CLASSMAP_FILE); }; }; @@ -64,7 +65,7 @@ describe 'test matching' => sub { $exception = dies { $warnings = warns { - $object = $CLASS->new(CLASSMAP_FILE); + $object = $CLASS->new($CLASSMAP_FILE); $result = $object->match($className, @paths); }; }; diff --git a/t/unit/GPH/Gitlab.t b/t/unit/GPH/Gitlab.t index e1696ab..22d03e8 100644 --- a/t/unit/GPH/Gitlab.t +++ b/t/unit/GPH/Gitlab.t @@ -6,9 +6,11 @@ use warnings; use Test2::V0 -target => 'GPH::Gitlab'; use Test2::Tools::Spec; + use Data::Dumper; +use Readonly; -use constant CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS'; +Readonly my $CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS'; local $SIG{__WARN__} = sub {}; @@ -23,7 +25,7 @@ describe "class `$CLASS`" => sub { @excludes = qw{.gitlab-ci.yml}; $exception = dies { $warnings = warns { - $object = $CLASS->new(CODEOWNERS_FILE, '@teams/alpha', @excludes); + $object = $CLASS->new($CODEOWNERS_FILE, '@teams/alpha', @excludes); }; }; @@ -40,7 +42,7 @@ describe "class `$CLASS`" => sub { @excludes = qw{.gitlab-ci.yml}; $exception = dies { $warnings = warns { - $object = $CLASS->new(CODEOWNERS_FILE, '@teams/alpha', @excludes); + $object = $CLASS->new($CODEOWNERS_FILE, '@teams/alpha', @excludes); }; }; diff --git a/t/unit/GPH/Infection.t b/t/unit/GPH/Infection.t index bffa306..1b89b82 100644 --- a/t/unit/GPH/Infection.t +++ b/t/unit/GPH/Infection.t @@ -122,15 +122,15 @@ Metrics: Covered Code MSI: ${output_msi}% "; - open my $fh, "<", \$stdin; - local *STDIN = $fh; - open (my $LOG, '>', \$stdout); - select $LOG; - $exception = dies { $warnings = warns { + local *ARGV; + open *ARGV, '<', \$stdin or die "no can do $!"; + $object = $CLASS->new($msi, $covered); $exit_code = $object->parse(); + + close *ARGV; }; }; @@ -139,7 +139,7 @@ Metrics: is($exit_code, $expected_code, 'expected code returned'); if ($expected_code == 3 and $escapees > 0) { - like($stdout, '\[warning\]', 'warning') + like($stdout, '\[warning\]', 'warning'); } } }; diff --git a/t/unit/GPH/PHPUnit.t b/t/unit/GPH/PHPUnit.t index b44d3fa..c387750 100644 --- a/t/unit/GPH/PHPUnit.t +++ b/t/unit/GPH/PHPUnit.t @@ -8,11 +8,12 @@ use Test2::V0 -target => 'GPH::PHPUnit'; use Test2::Tools::Spec; use Data::Dumper; +use Readonly; -use constant CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS'; -use constant CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php'; -use constant PHPUNIT_OUTPUT_FILE => './t/share/PHPUnit/phpunit-output.txt'; -use constant PHPUNIT_BASELINE_FILE => './t/share/PHPUnit/phpunit-baseline.txt'; +Readonly my $CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS'; +Readonly my $CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php'; +Readonly my $PHPUNIT_OUTPUT_FILE => './t/share/PHPUnit/phpunit-output.txt'; +Readonly my $PHPUNIT_BASELINE_FILE => './t/share/PHPUnit/phpunit-baseline.txt'; local $SIG{__WARN__} = sub {}; @@ -39,7 +40,7 @@ describe 'configuration options' => sub { case 'maximal options' => sub { $threshold = 95.5; @excludes = qw{.gitlab-ci.yml}; - $baseline = PHPUNIT_BASELINE_FILE; + $baseline = $PHPUNIT_BASELINE_FILE; $expected_threshold = $threshold; @expected_baseline = qw{/src/Service/Provider/ /src/Mapper/}; }; @@ -49,7 +50,7 @@ describe 'configuration options' => sub { $exception = dies { $warnings = warns { - $object = $CLASS->new($owner, CODEOWNERS_FILE, CLASSMAP_FILE, $threshold, \@excludes, $baseline); + $object = $CLASS->new($owner, $CODEOWNERS_FILE, $CLASSMAP_FILE, $threshold, \@excludes, $baseline); }; }; @@ -94,22 +95,19 @@ describe "parsing phpunit report output" => sub { }; tests "test `$CLASS` exit code" => sub { - my ($stdin, $stdout, $object, $exception, $warnings, $exit_code); + my ($stdout, $object, $exception, $warnings, $exit_code); my @excludes = qw{.gitlab-ci.yml}; - $object = $CLASS->new('@teams/alpha', CODEOWNERS_FILE, CLASSMAP_FILE, $threshold, \@excludes, PHPUNIT_BASELINE_FILE); $exception = dies { $warnings = warns { - open ($stdin, '<', PHPUNIT_OUTPUT_FILE); - local *STDIN = $stdin; - open(my $output, '>', \$stdout) or die; - my $fh = select $output; + local *ARGV; + open *ARGV, '<', $PHPUNIT_OUTPUT_FILE or die "can't open ${PHPUNIT_OUTPUT_FILE}"; + $object = $CLASS->new('@teams/alpha', $CODEOWNERS_FILE, $CLASSMAP_FILE, $threshold, \@excludes, $PHPUNIT_BASELINE_FILE); $exit_code = $object->parse(); - select $fh; - close $output; + close *ARGV; }; };