• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wickedOne / gitlab-perl-helpers / 9930703181

14 Jul 2024 09:03PM UTC coverage: 100.0%. Remained the same
9930703181

push

github

web-flow
regex fixes (#30)

- fixed regex bug which caused the script to step out of the teardown
  too early.

2 of 2 new or added lines in 1 file covered. (100.0%)

842 of 842 relevant lines covered (100.0%)

21.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/GPH/PHPStan/Cache.pm
1
package GPH::PHPStan::Cache;
2

3
use strict;
1✔
4
use warnings FATAL => 'all';
1✔
5

6
use Cwd;
1✔
7

8
sub new {
9
    my ($class, %args) = @_;
8✔
10

11
    my $self = {
12
        depth => $args{depth} || 1,
13
        relative => $args{relative} || undef,
14
        dependencies => undef,
8✔
15
    };
16

17
    bless $self, $class;
8✔
18

19
    return $self;
8✔
20
}
21

22
sub parseResultCache {
23
    my ($self, %args) = @_;
6✔
24
    my ($fh, $line, $key);
6✔
25
    my $in_array = 0;
6✔
26
    my $in_dependant = 0;
6✔
27
    my $in_dependent_files = 0;
6✔
28

29
    (exists($args{path})) or die "$!";
6✔
30

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

33

34
    while ($line = <$fh>) {
4✔
35
        chomp $line;
160✔
36

37
        if ($line =~ /^\s*'dependencies'\s*=>\s*array\s*\($/) {
160✔
38
            $in_array = 1;
4✔
39
        } elsif ($in_array && $in_dependant == 0 && $line =~ /^\s*'([^']+)'\s*=>\s*$/) {
40
            $key = $self->relative($1);
12✔
41
            $in_dependant = 1;
12✔
42
        } elsif ($in_array && $in_dependant && $line =~ /^\s*'dependentFiles'\s*=>\s*$/) {
43
            $in_dependent_files = 1;
16✔
44
        } elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*[0-9]+\s*=>\s*'([^']+)',$/) {
45
            push(@{$self->{dependencies}{$key}}, $self->relative($1));
12✔
46
        } elsif ($in_array && $in_dependant && $in_dependent_files && $line =~ /^\s*\),\s*$/) {
47
            $in_dependent_files = 0;
16✔
48
        } elsif ($in_array && $in_dependant && $in_dependent_files == 0 && $line =~ /^\s*\),\s*$/) {
49
            $in_dependant = 0;
12✔
50
        }
51
    }
52

53
    close($fh);
4✔
54

55
    return($self);
4✔
56
}
57

58
sub relative {
59
    my ($self, $line) = @_;
24✔
60

61
    if (!defined $self->{relative}) {
24✔
62
        return($line);
12✔
63
    }
64

65
    return substr $line, index($line, $self->{relative});
12✔
66
};
67

68
sub dependencies {
69
    my ($self, @paths) = @_;
2✔
70
    my (@unique, @result);
2✔
71
    @result = @paths;
2✔
72

73
    for (my $i = 1; $i <= $self->{depth}; $i++) {
2✔
74
        push(@result, $self->iterate(@result));
3✔
75
    }
76

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

79
    return(@unique);
2✔
80
};
81

82
sub iterate {
83
    my ($self, @paths) = @_;
3✔
84
    my ($path, $dependant, @unique, @result);
3✔
85
    @result = @paths;
3✔
86

87
    foreach $path (@paths) {
3✔
88
        for $dependant (@{$self->{dependencies}{$path}}) {
5✔
89
            push(@result, $dependant);
6✔
90
        }
91
    }
92

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

95
    return(@unique);
3✔
96
};
97

98
1;
99

100
__END__
101

102
=head1 NAME
103

104
GPH::PHPStan::Cache - parse dependencies from phpstan's resultCache.php file.
105

106
=head1 SYNOPSIS
107

108
    use GPH::PHPStan::Cache;
109

110
    my $cache = GPH::PHPStan::Cache->new((depth => 1, relative => 'src/'));
111

112
=head1 METHODS
113

114
=over 4
115

116
=item C<< -E<gt>new(%args) >>
117

118
the C<new> method creates a new GPH::PHPUnit::Config. it takes a hash of options, valid option keys include:
119

120
=over
121

122
=item path B<(required)>
123

124
path to the C<resultCache.php> file relative to the script execution path.
125

126
=item depth
127

128
depth of the dependency scan. defaults to 1. setting it to 2 for instance will retrieve the dependencies of the dependencies as well.
129

130
=item relative
131

132
when you want relative paths, to which directory should they be relative
133

134
=back
135

136
=item C<< -E<gt>parseResultCache(%config) >>
137

138
parses the cache file. it takes a hash of options, valid option keys include:
139

140
=over
141

142
=item path B<(required)>
143

144
path to the C<resultCache.php> file
145

146
=back
147

148
=item C<< -E<gt>relative($line) >> B<(internal)>
149

150
converts file path to relative path
151

152
=item C<< -E<gt>dependencies(@paths) >>
153

154
collects all dependencies for given C<@paths> and given C<$depth>
155

156
=item C<< -E<gt>iterate(@paths) >> B<(internal)>
157

158
collects all dependencies for given C<@paths>
159

160
=back
161

162
=head1 AUTHOR
163

164
the GPH::PHPUnit::Config module was written by wicliff wolda <wicliff.wolda@gmail.com>
165

166
=head1 COPYRIGHT AND LICENSE
167

168
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
169

170
=cut
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc