Login
Accelerate ProhibitMutatingListFunctions by making it check just
[gknop/Perl-Critic.git] / lib / Perl / Critic / Document.pm
CommitLineData
58a9e587 1########################################################################
a73f4a71
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
5bf96118
CD
6########################################################################
7
8package Perl::Critic::Document;
9
5bf96118 10use strict;
58a9e587 11use warnings;
5bf96118
CD
12use PPI::Document;
13
58a9e587
JRT
14#----------------------------------------------------------------------------
15
a7340650 16our $VERSION = 0.22;
5bf96118
CD
17
18#----------------------------------------------------------------------------
19
20our $AUTOLOAD;
21sub AUTOLOAD { ## no critic(ProhibitAutoloading)
22 my ( $function_name ) = $AUTOLOAD =~ m/ ([^:\']+) \z /xms;
23 return if $function_name eq 'DESTROY';
24 my $self = shift;
25 return $self->{_doc}->$function_name(@_);
26}
27
58a9e587 28#----------------------------------------------------------------------------
5bf96118 29
58a9e587
JRT
30sub new {
31 my ($class, $doc) = @_;
5bf96118
CD
32 return bless { _doc => $doc }, $class;
33}
34
58a9e587
JRT
35#----------------------------------------------------------------------------
36
5bf96118 37sub find {
58a9e587 38 my ($self, $wanted) = @_;
5bf96118 39
58a9e587
JRT
40 # This method can only find elements by their class names. For
41 # other types of searches, delegate to the PPI::Document
5bf96118
CD
42 if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
43 return $self->{_doc}->find($wanted, @_);
44 }
58a9e587
JRT
45
46 # Build the class cache if it doesn't exist. This happens at most
47 # once per Perl::Critic::Document instance. %elements of will be
48 # populated as a side-effect of calling the $finder_sub coderef
49 # that is produced by the caching_finder() closure.
5bf96118 50 if ( !$self->{_elements_of} ) {
58a9e587
JRT
51 my %cache = ( 'PPI::Document' => [ $self ] );
52 my $finder_coderef = _caching_finder( \%cache );
53 $self->{_doc}->find( $finder_coderef );
54 $self->{_elements_of} = \%cache;
55 }
56
57 # find() must return false-but-defined on fail
58 return $self->{_elements_of}->{$wanted} || q{};
59}
60
61#----------------------------------------------------------------------------
62
fb21e21e
CD
63sub find_first {
64 my ($self, $wanted) = @_;
65
66 # This method can only find elements by their class names. For
67 # other types of searches, delegate to the PPI::Document
68 if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
69 return $self->{_doc}->find_first($wanted, @_);
70 }
71
72 my $result = $self->find($wanted);
73 return $result ? $result->[0] : $result;
74}
75
76#----------------------------------------------------------------------------
77
f5eeac3b
CD
78sub find_any {
79 my ($self, $wanted) = @_;
80
81 # This method can only find elements by their class names. For
82 # other types of searches, delegate to the PPI::Document
83 if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
84 return $self->{_doc}->find_any($wanted, @_);
85 }
86
87 my $result = $self->find($wanted);
88 return $result ? 1 : $result;
89}
90
91#----------------------------------------------------------------------------
92
60108aef
CD
93sub filename {
94 my ($self) = @_;
95 return $self->{_doc}->can('filename') ? $self->{_doc}->filename : undef;
96}
97
98#----------------------------------------------------------------------------
99
58a9e587
JRT
100sub _caching_finder {
101
102 my $cache_ref = shift; # These vars will persist for the life
103 my %isa_cache = (); # of the code ref that this sub returns
104
105
106 # Gather up all the PPI elements and sort by @ISA. Note: if any
107 # instances used multiple inheritance, this implementation would
108 # lead to multiple copies of $element in the $elements_of lists.
109 # However, PPI::* doesn't do multiple inheritance, so we are safe
110
111 return sub {
112 my $element = $_[1];
113 my $classes = $isa_cache{ref $element};
114 if ( !$classes ) {
115 $classes = [ ref $element ];
116 # Use a C-style loop because we append to the classes array inside
117 for ( my $i = 0; $i < @{$classes}; $i++ ) { ## no critic(ProhibitCStyleForLoops)
118 no strict 'refs'; ## no critic(ProhibitNoStrict)
119 push @{$classes}, @{"$classes->[$i]::ISA"};
120 $cache_ref->{$classes->[$i]} ||= [];
5bf96118 121 }
58a9e587
JRT
122 $isa_cache{$classes->[0]} = $classes;
123 }
5bf96118 124
58a9e587
JRT
125 for my $class ( @{$classes} ) {
126 push @{$cache_ref->{$class}}, $element;
127 }
5bf96118 128
58a9e587
JRT
129 return 0; # 0 tells find() to keep traversing, but not to store this $element
130 };
5bf96118
CD
131}
132
58a9e587
JRT
133#----------------------------------------------------------------------------
134
5bf96118 1351;
58a9e587 136
5bf96118
CD
137__END__
138
a73f4a71
JRT
139=pod
140
141=for stopwords pre-caches
142
5bf96118
CD
143=head1 NAME
144
145Perl::Critic::Document - Caching wrapper around PPI::Document
146
147=head1 SYNOPSIS
148
149 use PPI::Document;
150 use Perl::Critic::Document;
151 my $doc = PPI::Document->new('Foo.pm');
152 $doc = Perl::Critic::Document->new($doc);
153 ## Then use the instance just like a PPI::Document
154
155=head1 DESCRIPTION
156
157Perl::Critic does a lot of iterations over the PPI document tree via
158the C<PPI::Document::find()> method. To save some time, this class
159pre-caches a lot of the common C<find()> calls in a single traversal.
160Then, on subsequent requests we return the cached data.
161
162This is implemented as a facade, where method calls are handed to the
163stored C<PPI::Document> instance.
164
165=head1 CAVEATS
166
167This facade does not implement the overloaded operators from
168L<PPI::Document> (that is, the C<use overload ...> work). Therefore,
169users of this facade must not rely on that syntactic sugar. So, for
170example, instead of C<my $source = "$doc";> you should write C<my
171$source = $doc->content();>
172
173Perhaps there is a CPAN module out there which implements a facade
174better than we do here?
175
176=head1 METHODS
177
178=over
179
7076e807
CD
180=item $pkg->new($doc)
181
182Create a new instance referencing a PPI::Document instance.
183
5bf96118
CD
184=item $self->find($wanted)
185
fb21e21e
CD
186=item $self->find_first($wanted)
187
f5eeac3b
CD
188=item $self->find_any($wanted)
189
fb21e21e 190If C<$wanted> is a simple PPI class name, then the cache is employed.
f5eeac3b
CD
191Otherwise we forward the call to the corresponding method of the
192C<PPI::Document> instance.
5bf96118 193
e7f2d995
CD
194=item $self->filename()
195
196Returns the filename for the source code if applicable
197(PPI::Document::File) or C<undef> otherwise (PPI::Document).
198
5bf96118
CD
199=back
200
201=head1 AUTHOR
202
203Chris Dolan <cdolan@cpan.org>
204
205=head1 COPYRIGHT
206
207Copyright (c) 2006 Chris Dolan. All rights reserved.
208
209This program is free software; you can redistribute it and/or modify
210it under the same terms as Perl itself. The full text of this license
211can be found in the LICENSE file included with this module.
212
213=cut
737d3b65
CD
214
215# Local Variables:
216# mode: cperl
217# cperl-indent-level: 4
218# fill-column: 78
219# indent-tabs-mode: nil
220# c-indentation-style: bsd
221# End:
dbb78cdc 222# ex: set ts=8 sts=4 sw=4 expandtab :