Commit | Line | Data |
---|---|---|
6036a254 | 1 | ############################################################################## |
a73f4a71 JRT |
2 | # $URL$ |
3 | # $Date$ | |
4 | # $Author$ | |
5 | # $Revision$ | |
6036a254 | 6 | ############################################################################## |
5bf96118 CD |
7 | |
8 | package Perl::Critic::Document; | |
9 | ||
df6dee2b | 10 | use 5.006001; |
5bf96118 | 11 | use strict; |
58a9e587 | 12 | use warnings; |
267b39b4 ES |
13 | |
14 | use List::Util qw< max >; | |
5bf96118 | 15 | use PPI::Document; |
267b39b4 ES |
16 | use Scalar::Util qw< weaken >; |
17 | use version; | |
5bf96118 | 18 | |
6036a254 | 19 | #----------------------------------------------------------------------------- |
58a9e587 | 20 | |
a8da49fd | 21 | our $VERSION = '1.088'; |
5bf96118 | 22 | |
6036a254 | 23 | #----------------------------------------------------------------------------- |
5bf96118 CD |
24 | |
25 | our $AUTOLOAD; | |
6e7d6c9f CD |
26 | sub AUTOLOAD { ## no critic(ProhibitAutoloading,ArgUnpacking) |
27 | my ( $function_name ) = $AUTOLOAD =~ m/ ([^:\']+) \z /xms; | |
28 | return if $function_name eq 'DESTROY'; | |
29 | my $self = shift; | |
30 | return $self->{_doc}->$function_name(@_); | |
5bf96118 CD |
31 | } |
32 | ||
6036a254 | 33 | #----------------------------------------------------------------------------- |
5bf96118 | 34 | |
58a9e587 JRT |
35 | sub new { |
36 | my ($class, $doc) = @_; | |
5bf96118 CD |
37 | return bless { _doc => $doc }, $class; |
38 | } | |
39 | ||
6036a254 | 40 | #----------------------------------------------------------------------------- |
58a9e587 | 41 | |
2b6293b2 CD |
42 | sub ppi_document { |
43 | my ($self) = @_; | |
44 | return $self->{_doc}; | |
45 | } | |
46 | ||
47 | #----------------------------------------------------------------------------- | |
48 | ||
47e1ff34 | 49 | sub isa { |
6e7d6c9f CD |
50 | my ($self, @args) = @_; |
51 | return $self->SUPER::isa(@args) | |
52 | || ( (ref $self) && $self->{_doc} && $self->{_doc}->isa(@args) ); | |
47e1ff34 CD |
53 | } |
54 | ||
6036a254 | 55 | #----------------------------------------------------------------------------- |
47e1ff34 | 56 | |
5bf96118 | 57 | sub find { |
6e7d6c9f | 58 | my ($self, $wanted, @more_args) = @_; |
5bf96118 | 59 | |
58a9e587 JRT |
60 | # This method can only find elements by their class names. For |
61 | # other types of searches, delegate to the PPI::Document | |
5bf96118 | 62 | if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) { |
6e7d6c9f | 63 | return $self->{_doc}->find($wanted, @more_args); |
5bf96118 | 64 | } |
58a9e587 JRT |
65 | |
66 | # Build the class cache if it doesn't exist. This happens at most | |
67 | # once per Perl::Critic::Document instance. %elements of will be | |
68 | # populated as a side-effect of calling the $finder_sub coderef | |
69 | # that is produced by the caching_finder() closure. | |
5bf96118 | 70 | if ( !$self->{_elements_of} ) { |
389109ec | 71 | |
58a9e587 | 72 | my %cache = ( 'PPI::Document' => [ $self ] ); |
389109ec JRT |
73 | |
74 | # The cache refers to $self, and $self refers to the cache. This | |
75 | # creates a circular reference that leaks memory (i.e. $self is not | |
76 | # destroyed until execution is complete). By weakening the reference, | |
77 | # we allow perl to collect the garbage properly. | |
78 | weaken( $cache{'PPI::Document'}->[0] ); | |
79 | ||
58a9e587 JRT |
80 | my $finder_coderef = _caching_finder( \%cache ); |
81 | $self->{_doc}->find( $finder_coderef ); | |
82 | $self->{_elements_of} = \%cache; | |
83 | } | |
84 | ||
85 | # find() must return false-but-defined on fail | |
86 | return $self->{_elements_of}->{$wanted} || q{}; | |
87 | } | |
88 | ||
6036a254 | 89 | #----------------------------------------------------------------------------- |
58a9e587 | 90 | |
fb21e21e | 91 | sub find_first { |
6e7d6c9f | 92 | my ($self, $wanted, @more_args) = @_; |
fb21e21e CD |
93 | |
94 | # This method can only find elements by their class names. For | |
95 | # other types of searches, delegate to the PPI::Document | |
96 | if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) { | |
6e7d6c9f | 97 | return $self->{_doc}->find_first($wanted, @more_args); |
fb21e21e CD |
98 | } |
99 | ||
100 | my $result = $self->find($wanted); | |
101 | return $result ? $result->[0] : $result; | |
102 | } | |
103 | ||
6036a254 | 104 | #----------------------------------------------------------------------------- |
fb21e21e | 105 | |
f5eeac3b | 106 | sub find_any { |
6e7d6c9f | 107 | my ($self, $wanted, @more_args) = @_; |
f5eeac3b CD |
108 | |
109 | # This method can only find elements by their class names. For | |
110 | # other types of searches, delegate to the PPI::Document | |
111 | if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) { | |
6e7d6c9f | 112 | return $self->{_doc}->find_any($wanted, @more_args); |
f5eeac3b CD |
113 | } |
114 | ||
115 | my $result = $self->find($wanted); | |
116 | return $result ? 1 : $result; | |
117 | } | |
118 | ||
6036a254 | 119 | #----------------------------------------------------------------------------- |
f5eeac3b | 120 | |
60108aef CD |
121 | sub filename { |
122 | my ($self) = @_; | |
123 | return $self->{_doc}->can('filename') ? $self->{_doc}->filename : undef; | |
124 | } | |
125 | ||
6036a254 | 126 | #----------------------------------------------------------------------------- |
60108aef | 127 | |
267b39b4 ES |
128 | sub highest_explicit_perl_version { |
129 | my ($self) = @_; | |
130 | ||
131 | my $highest_explicit_perl_version = | |
132 | $self->{_highest_explicit_perl_version}; | |
133 | ||
134 | if ( not exists $self->{_highest_explicit_perl_version} ) { | |
135 | my $includes = $self->find( \&_is_a_version_statement ); | |
136 | ||
137 | if ($includes) { | |
138 | $highest_explicit_perl_version = | |
139 | max map { version->new( $_->version() ) } @{$includes}; | |
140 | } | |
141 | else { | |
142 | $highest_explicit_perl_version = undef; | |
143 | } | |
144 | ||
145 | $self->{_highest_explicit_perl_version} = | |
146 | $highest_explicit_perl_version; | |
147 | } | |
148 | ||
149 | return $highest_explicit_perl_version if $highest_explicit_perl_version; | |
150 | return; | |
151 | } | |
152 | ||
153 | sub _is_a_version_statement { | |
154 | my (undef, $element) = @_; | |
155 | ||
156 | return 0 if not $element->isa('PPI::Statement::Include'); | |
157 | return 1 if $element->version(); | |
158 | return 0; | |
159 | } | |
160 | ||
161 | #----------------------------------------------------------------------------- | |
162 | ||
58a9e587 JRT |
163 | sub _caching_finder { |
164 | ||
165 | my $cache_ref = shift; # These vars will persist for the life | |
166 | my %isa_cache = (); # of the code ref that this sub returns | |
167 | ||
168 | ||
169 | # Gather up all the PPI elements and sort by @ISA. Note: if any | |
170 | # instances used multiple inheritance, this implementation would | |
171 | # lead to multiple copies of $element in the $elements_of lists. | |
172 | # However, PPI::* doesn't do multiple inheritance, so we are safe | |
173 | ||
174 | return sub { | |
6e7d6c9f | 175 | my (undef, $element) = @_; |
58a9e587 JRT |
176 | my $classes = $isa_cache{ref $element}; |
177 | if ( !$classes ) { | |
178 | $classes = [ ref $element ]; | |
179 | # Use a C-style loop because we append to the classes array inside | |
180 | for ( my $i = 0; $i < @{$classes}; $i++ ) { ## no critic(ProhibitCStyleForLoops) | |
181 | no strict 'refs'; ## no critic(ProhibitNoStrict) | |
182 | push @{$classes}, @{"$classes->[$i]::ISA"}; | |
183 | $cache_ref->{$classes->[$i]} ||= []; | |
5bf96118 | 184 | } |
58a9e587 JRT |
185 | $isa_cache{$classes->[0]} = $classes; |
186 | } | |
5bf96118 | 187 | |
58a9e587 JRT |
188 | for my $class ( @{$classes} ) { |
189 | push @{$cache_ref->{$class}}, $element; | |
190 | } | |
5bf96118 | 191 | |
58a9e587 JRT |
192 | return 0; # 0 tells find() to keep traversing, but not to store this $element |
193 | }; | |
5bf96118 CD |
194 | } |
195 | ||
6036a254 | 196 | #----------------------------------------------------------------------------- |
58a9e587 | 197 | |
5bf96118 | 198 | 1; |
58a9e587 | 199 | |
5bf96118 CD |
200 | __END__ |
201 | ||
a73f4a71 JRT |
202 | =pod |
203 | ||
204 | =for stopwords pre-caches | |
205 | ||
5bf96118 CD |
206 | =head1 NAME |
207 | ||
c728943a | 208 | Perl::Critic::Document - Caching wrapper around a PPI::Document. |
5bf96118 | 209 | |
267b39b4 | 210 | |
5bf96118 CD |
211 | =head1 SYNOPSIS |
212 | ||
213 | use PPI::Document; | |
214 | use Perl::Critic::Document; | |
215 | my $doc = PPI::Document->new('Foo.pm'); | |
216 | $doc = Perl::Critic::Document->new($doc); | |
217 | ## Then use the instance just like a PPI::Document | |
218 | ||
267b39b4 | 219 | |
5bf96118 CD |
220 | =head1 DESCRIPTION |
221 | ||
222 | Perl::Critic does a lot of iterations over the PPI document tree via | |
223 | the C<PPI::Document::find()> method. To save some time, this class | |
224 | pre-caches a lot of the common C<find()> calls in a single traversal. | |
225 | Then, on subsequent requests we return the cached data. | |
226 | ||
227 | This is implemented as a facade, where method calls are handed to the | |
228 | stored C<PPI::Document> instance. | |
229 | ||
267b39b4 | 230 | |
5bf96118 CD |
231 | =head1 CAVEATS |
232 | ||
233 | This facade does not implement the overloaded operators from | |
234 | L<PPI::Document> (that is, the C<use overload ...> work). Therefore, | |
235 | users of this facade must not rely on that syntactic sugar. So, for | |
236 | example, instead of C<my $source = "$doc";> you should write C<my | |
237 | $source = $doc->content();> | |
238 | ||
239 | Perhaps there is a CPAN module out there which implements a facade | |
240 | better than we do here? | |
241 | ||
267b39b4 ES |
242 | |
243 | =head1 CONSTRUCTOR | |
244 | ||
245 | =over | |
246 | ||
247 | =item C<< new($doc) >> | |
248 | ||
249 | Create a new instance referencing a PPI::Document instance. | |
250 | ||
251 | ||
252 | =back | |
253 | ||
254 | ||
5bf96118 CD |
255 | =head1 METHODS |
256 | ||
257 | =over | |
258 | ||
267b39b4 | 259 | =item C<< new($doc) >> |
7076e807 CD |
260 | |
261 | Create a new instance referencing a PPI::Document instance. | |
262 | ||
267b39b4 ES |
263 | |
264 | =item C<< ppi_document() >> | |
2b6293b2 CD |
265 | |
266 | Accessor for the wrapped PPI::Document instance. Note that altering this | |
267 | instance in any way can cause unpredictable failures in Perl::Critic's | |
268 | subsequent analysis because some caches may fall out of date. | |
269 | ||
5bf96118 | 270 | |
267b39b4 ES |
271 | =item C<< find($wanted) >> |
272 | ||
273 | =item C<< find_first($wanted) >> | |
fb21e21e | 274 | |
267b39b4 | 275 | =item C<< find_any($wanted) >> |
f5eeac3b | 276 | |
fb21e21e | 277 | If C<$wanted> is a simple PPI class name, then the cache is employed. |
f5eeac3b CD |
278 | Otherwise we forward the call to the corresponding method of the |
279 | C<PPI::Document> instance. | |
5bf96118 | 280 | |
267b39b4 ES |
281 | |
282 | =item C<< filename() >> | |
e7f2d995 CD |
283 | |
284 | Returns the filename for the source code if applicable | |
285 | (PPI::Document::File) or C<undef> otherwise (PPI::Document). | |
286 | ||
267b39b4 ES |
287 | |
288 | =item C<< isa( $classname ) >> | |
242f7b08 JRT |
289 | |
290 | To be compatible with other modules that expect to get a PPI::Document, the | |
8a25c8a0 | 291 | Perl::Critic::Document class masquerades as the PPI::Document class. |
242f7b08 | 292 | |
267b39b4 ES |
293 | |
294 | =item C<< highest_explicit_perl_version() >> | |
295 | ||
296 | Returns a L<version> object for the highest Perl version requirement declared | |
297 | in the document via a C<use> or C<require> statement. Returns nothing if | |
298 | there is no version statement. | |
299 | ||
300 | ||
5bf96118 CD |
301 | =back |
302 | ||
267b39b4 | 303 | |
5bf96118 CD |
304 | =head1 AUTHOR |
305 | ||
306 | Chris Dolan <cdolan@cpan.org> | |
307 | ||
267b39b4 | 308 | |
5bf96118 CD |
309 | =head1 COPYRIGHT |
310 | ||
20dfddeb | 311 | Copyright (c) 2006-2008 Chris Dolan. All rights reserved. |
5bf96118 CD |
312 | |
313 | This program is free software; you can redistribute it and/or modify | |
314 | it under the same terms as Perl itself. The full text of this license | |
315 | can be found in the LICENSE file included with this module. | |
316 | ||
317 | =cut | |
737d3b65 CD |
318 | |
319 | # Local Variables: | |
320 | # mode: cperl | |
321 | # cperl-indent-level: 4 | |
322 | # fill-column: 78 | |
323 | # indent-tabs-mode: nil | |
324 | # c-indentation-style: bsd | |
325 | # End: | |
96fed375 | 326 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |