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