lib/Perl/ToPerl6/Document.pm

filter: Statementstmt 76.3% 207 / 271 Branchbran 47.2% 52 / 110 Conditioncond 64.2% 36 / 56 MC/DCmcdc 45.1% 14 / 31 Subroutinesub 86.0% 43 / 50 Podpod 100% 21 / 21 totaltotal 69.2% 373 / 539 CC 83 SCAR 30.1
CC
83
Cov
68%
element_is_in_lexical_scope_after_statement_containing
132.0
_find_use_re_modifiers_in_scope_from_element
72.0
ppix_regexp_from_element
20.0
CRAP 20.2
« lib/Perl/ToPerl6/Utils/PPI.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Variables/QuoteHashKeys.pm »
1 package Perl::ToPerl6::Document;
2  
3 13 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp qw< confess >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use List::Util qw< reduce >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Scalar::Util qw< blessed refaddr weaken >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use PPI::Document::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use PPIx::Utilities::Node qw< split_ppi_node_by_namespace >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 ▶use Perl::ToPerl6::Annotation;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Perl::ToPerl6::Exception::Parse qw< throw_parse >;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Perl::ToPerl6::Utils qw< :booleans :characters shebang_line >;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 13 ▶use PPIx::Regexp 0.010 qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #-----------------------------------------------------------------------------
24  
25 13 our $AUTOLOAD;
26 212 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
27 212 my ( $function_name ) = $AUTOLOAD =~ m/ ([^:\']+) \z /xms;
28 212 ▶ return if $function_name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $function_name eq "DESTROY" 54 158 212
29 158 my $self = shift;
30 158 return $self->{_doc}->$function_name(@_);
31 }
32  
33 #-----------------------------------------------------------------------------
34  
35 51 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 51 my ($class, @args) = @_;
37  
38 51 my $self = bless {}, $class;
39  
40 51 $self->_init_common();
41 51 $self->_init_from_external_source(@args);
42  
43 51 return $self;
44 }
45  
46 #-----------------------------------------------------------------------------
47  
48 4 ▶sub _new_for_parent_document {
Subroutine _new_for_parent_document: called CC=1 SCAR=0.0
49 4 my ($class, $ppi_document, $parent_document) = @_;
50  
51 4 my $self = bless {}, $class;
52  
53 4 $self->_init_common();
54  
55 4 $self->{_doc} = $ppi_document;
56 4 $self->{_is_module} = $parent_document->is_module();
57  
58 4 return $self;
59 }
60  
61 #-----------------------------------------------------------------------------
62  
63 55 ▶sub _init_common {
Subroutine _init_common: called CC=1 SCAR=0.0
64 55 my ($self) = @_;
65  
66 55 $self->{_annotations} = [];
67 55 $self->{_suppressed_transformations} = [];
68 55 $self->{_disabled_line_map} = {};
69  
70 55 return;
71 }
72  
73 #-----------------------------------------------------------------------------
74  
75 51 ▶sub _init_from_external_source {
Subroutine _init_from_external_source: called CC=6 SCAR=20.3
76 51 my $self = shift;
77 51 my %args;
78  
79 51 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 51 51
80 0 warnings::warnif(
81 'deprecated',
82 'Perl::ToPerl6::Document->new($source) deprecated, use Perl::ToPerl6::Document->new(-source => $source) instead.'
83 );
84 0 %args = ('-source' => shift);
85 } else {
86 51 %args = @_;
87 }
88  
89 51 my $source_code = $args{'-source'};
90  
91 # $source_code can be a file name, or a reference to a
92 # PPI::Document, or a reference to a scalar containing source
93 # code. In the last case, PPI handles the translation for us.
94  
95 51 ▶ my $ppi_document =
Branch: 2 branches
BranchTrueFalseTotal
_is_ppi_doc($source_code) ? : 18 33 51
ref $source_code ? : 33 0 33
96 _is_ppi_doc($source_code)
97 ? $source_code
98 : ref $source_code
99 ? PPI::Document->new($source_code)
100 : PPI::Document::File->new($source_code);
101  
102 # Bail on error
103 51 ▶ if (not defined $ppi_document) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ppi_document) 0 51 51
104 0 my $errstr = PPI::Document::errstr();
105 0 ▶ my $file = ref $source_code ? undef : $source_code;
Branch: 1 branch
BranchTrueFalseTotal
ref $source_code ? : 0 0 0
106 0 throw_parse
107 message => qq<Can't parse code: $errstr>,
108 file_name => $file;
109 }
110  
111 51 $self->{_doc} = $ppi_document;
112 51 $self->index_locations();
113 51 $self->_disable_shebang_fix();
114 51 $self->{_filename_override} = $args{'-filename-override'};
115 51 $self->{_is_module} = $self->_determine_is_module(\%args);
116  
117 51 return;
118 }
119  
120 #-----------------------------------------------------------------------------
121  
122 51 ▶sub _is_ppi_doc {
Subroutine _is_ppi_doc: called CC=2 SCAR=7.1
123 51 my ($ref) = @_;
124 51 ▶ return blessed($ref) && $ref->isa('PPI::Document');
Condition66%2/3
builtin::blessed($ref) && $ref->isa('PPI::Document')
!ll&&!rl&&r
33018
MC/DC50%1/2
builtin::blessed($ref) && $ref->isa('PPI::Document')
builtin::blessed($ref) $ref->isa('PPI::Document')
Truth table66%2/3
A && B
A= builtin::blessed($ref)B= $ref->isa('PPI::Document')
ABresult
0X0
100
111
125 }
126  
127 #-----------------------------------------------------------------------------
128  
129 3 ▶sub ppi_document {
Subroutine ppi_document: called CC=1 SCAR=0.0
Pod documented
130 3 my ($self) = @_;
131 3 return $self->{_doc};
132 }
133  
134 #-----------------------------------------------------------------------------
135  
136 6 ▶sub isa {
Subroutine isa: called CC=4 SCAR=17.1
Pod documented
137 6 my ($self, @args) = @_;
138 return $self->SUPER::isa(@args)
139 6 ▶ || ( (ref $self) && $self->{_doc} && $self->{_doc}->isa(@args) );
Condition44%4/9
$self->SUPER::isa(@args) || ref $self && $self->{'_doc'} && $self->{'_doc'}->isa(@args)
l!l&&r!l&&!r
330
ref $self and $self->{"_doc"} and $self->{"_doc"}->isa(@args)
!ll&&!rl&&r
003
ref $self and $self->{"_doc"}
!ll&&!rl&&r
003
MC/DC0%0/4
$self->SUPER::isa(@args) || ref $self && $self->{'_doc'} && $self->{'_doc'}->isa(@args)
$self->SUPER::isa(@args) ref $self $self->{"_doc"} $self->{"_doc"}->isa(@args)
Truth table40%2/5
A || B and C and D
A= $self->SUPER::isa(@args)B= ref $selfC= $self->{"_doc"}D= $self->{"_doc"}->isa(@args)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
140 }
141  
142 #-----------------------------------------------------------------------------
143  
144 237 ▶sub find {
Subroutine find: called CC=6 SCAR=17.9
Pod documented
145 237 my ($self, $wanted, @more_args) = @_;
146  
147 # This method can only find elements by their class names. For
148 # other types of searches, delegate to the PPI::Document
149 237 ▶ if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx) 141 96 237
Condition100%6/6
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
l!l&&r!l&&!r
140196
ref $wanted or not $wanted
l!l&&r!l&&!r
139197
MC/DC100%3/3
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
ref $wanted not $wanted not $wanted =~ / \A PPI:: /msx
Truth table100%4/4
A or not(B) or not(C)
A= ref $wantedB= not $wantedC= not $wanted =~ / \A PPI:: /msx
ABCresult
1XX1
01X1
0011
0000
150 141 return $self->{_doc}->find($wanted, @more_args);
151 }
152  
153 # Build the class cache if it doesn't exist. This happens at most
154 # once per Perl::ToPerl6::Document instance. %elements of will be
155 # populated as a side-effect of calling the $finder_sub coderef
156 # that is produced by the caching_finder() closure.
157 96 ▶ if ( !$self->{_elements_of} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_elements_of"}) 19 77 96
158  
159 19 my %cache = ( 'PPI::Document' => [ $self ] );
160  
161 # The cache refers to $self, and $self refers to the cache. This
162 # creates a circular reference that leaks memory (i.e. $self is not
163 # destroyed until execution is complete). By weakening the reference,
164 # we allow perl to collect the garbage properly.
165 19 weaken( $cache{'PPI::Document'}->[0] );
166  
167 19 my $finder_coderef = _caching_finder( \%cache );
168 19 $self->{_doc}->find( $finder_coderef );
169 19 $self->{_elements_of} = \%cache;
170 }
171  
172 # find() must return false-but-defined on fail
173 96 ▶ return $self->{_elements_of}->{$wanted} || q{};
Condition100%2/2
$self->{'_elements_of'}{$wanted} || ''
l!l
2967
MC/DC100%1/1
$self->{'_elements_of'}{$wanted} || ''
$self->{'_elements_of'}{$wanted}
Truth table100%2/2
A
A= $self->{'_elements_of'}{$wanted}
Aresult
11
00
174 }
175  
176 #-----------------------------------------------------------------------------
177  
178 28 ▶sub find_first {
Subroutine find_first: called CC=5 SCAR=16.1
Pod documented
179 28 my ($self, $wanted, @more_args) = @_;
180  
181 # This method can only find elements by their class names. For
182 # other types of searches, delegate to the PPI::Document
183 28 ▶ if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx) 27 1 28
Condition100%6/6
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
l!l&&r!l&&!r
1891
ref $wanted or not $wanted
l!l&&r!l&&!r
17110
MC/DC100%3/3
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
ref $wanted not $wanted not $wanted =~ / \A PPI:: /msx
Truth table100%4/4
A or not(B) or not(C)
A= ref $wantedB= not $wantedC= not $wanted =~ / \A PPI:: /msx
ABCresult
1XX1
01X1
0011
0000
184 27 return $self->{_doc}->find_first($wanted, @more_args);
185 }
186  
187 1 my $result = $self->find($wanted);
188 1 ▶ return $result ? $result->[0] : $result;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 1 0 1
189 }
190  
191 #-----------------------------------------------------------------------------
192  
193 4 ▶sub find_any {
Subroutine find_any: called CC=5 SCAR=16.1
Pod documented
194 4 my ($self, $wanted, @more_args) = @_;
195  
196 # This method can only find elements by their class names. For
197 # other types of searches, delegate to the PPI::Document
198 4 ▶ if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx) 3 1 4
Condition100%6/6
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
l!l&&r!l&&!r
211
ref $wanted or not $wanted
l!l&&r!l&&!r
112
MC/DC100%3/3
ref $wanted or not $wanted or not $wanted =~ / \A PPI:: /msx
ref $wanted not $wanted not $wanted =~ / \A PPI:: /msx
Truth table100%4/4
A or not(B) or not(C)
A= ref $wantedB= not $wantedC= not $wanted =~ / \A PPI:: /msx
ABCresult
1XX1
01X1
0011
0000
199 3 return $self->{_doc}->find_any($wanted, @more_args);
200 }
201  
202 1 my $result = $self->find($wanted);
203 1 ▶ return $result ? 1 : $result;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 1 0 1
204 }
205  
206 #-----------------------------------------------------------------------------
207  
208 2 ▶sub namespaces {
Subroutine namespaces: called CC=1 SCAR=0.0
Pod documented
209 2 my ($self) = @_;
210  
211 2 return keys %{ $self->_nodes_by_namespace() };
212 }
213  
214 #-----------------------------------------------------------------------------
215  
216 0 ▶sub subdocuments_for_namespace {
Subroutine subdocuments_for_namespace: not called CC=2 SCAR=17.9
Pod documented
217 0 my ($self, $namespace) = @_;
218  
219 0 my $subdocuments = $self->_nodes_by_namespace()->{$namespace};
220  
221 0 ▶ return $subdocuments ? @{$subdocuments} : ();
Branch: 1 branch
BranchTrueFalseTotal
$subdocuments ? : 0 0 0
222 }
223  
224 #-----------------------------------------------------------------------------
225  
226 0 ▶sub ppix_regexp_from_element {
Subroutine ppix_regexp_from_element: not called CC=4 SCAR=30.0
Pod documented
227 0 my ( $self, $element ) = @_;
228  
229 0 ▶ if ( blessed( $element ) && $element->isa( 'PPI::Element' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($element) and $element->isa('PPI::Element')) { } 0 0 0
Condition0%0/3
builtin::blessed($element) and $element->isa("PPI::Element")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($element) and $element->isa("PPI::Element")
builtin::blessed($element) $element->isa("PPI::Element")
Truth table0%0/3
A and B
A= builtin::blessed($element)B= $element->isa("PPI::Element")
ABresult
0X0
100
111
230 0 my $addr = refaddr( $element );
231 return $self->{_ppix_regexp_from_element}{$addr}
232 0 ▶ if exists $self->{_ppix_regexp_from_element}{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_ppix_regexp_from_element"}{$addr} 0 0 0
233 0 return ( $self->{_ppix_regexp_from_element}{$addr} =
234 PPIx::Regexp->new( $element,
235 default_modifiers =>
236 $self->_find_use_re_modifiers_in_scope_from_element(
237 $element ),
238 ) );
239 } else {
240 0 return PPIx::Regexp->new( $element );
241 }
242 }
243  
244 0 ▶sub _find_use_re_modifiers_in_scope_from_element {
Subroutine _find_use_re_modifiers_in_scope_from_element: not called CC=8 SCAR=42.8
245 0 my ( $self, $elem ) = @_;
246 0 my @found;
247 0 ▶ foreach my $use_re ( @{ $self->find( 'PPI::Statement::Include' ) || [] } )
Condition0%0/2
$self->find('PPI::Statement::Include') || []
l!l
00
MC/DC0%0/1
$self->find('PPI::Statement::Include') || []
$self->find('PPI::Statement::Include')
Truth table0%0/2
A
A= $self->find('PPI::Statement::Include')
Aresult
11
00
248 {
249 0 ▶ 're' eq $use_re->module()
Branch: 1 branch
BranchTrueFalseTotal
"re" eq $use_re->module or next 0 0 0
250 or next;
251 0 ▶ $self->element_is_in_lexical_scope_after_statement_containing(
Branch: 1 branch
BranchTrueFalseTotal
$self->element_is_in_lexical_scope_after_statement_containing($elem, $use_re) or next 0 0 0
252 $elem, $use_re )
253 or next;
254 0 ▶ my $prefix = 'no' eq $use_re->type() ? q{-} : $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
'no' eq $use_re->type ? : 0 0 0
255 push @found,
256 0 map { "$prefix$_" }
257 0 grep { m{ \A / }smx }
258 map {
259 0 ▶ $_->isa( 'PPI::Token::Quote' ) ? $_->string() :
Branch: 2 branches
BranchTrueFalseTotal
$_->isa('PPI::Token::Quote') ? : 0 0 0
$_->isa('PPI::Token::QuoteLike::Words') ? : 0 0 0
260 $_->isa( 'PPI::Token::QuoteLike::Words' ) ? $_->literal() :
261 $_->content() }
262 $use_re->schildren();
263 }
264 0 return \@found;
265 }
266  
267 #-----------------------------------------------------------------------------
268  
269 # This got hung on the Perl::ToPerl6::Document, rather than living in
270 # Perl::ToPerl6::Utils::PPI, because of the possibility that caching of scope
271 # objects would turn out to be desirable.
272  
273 0 ▶sub element_is_in_lexical_scope_after_statement_containing {
Subroutine element_is_in_lexical_scope_after_statement_containing: not called CC=11 SCAR=48.8
Pod documented
274 0 my ( $self, $inner_elem, $outer_elem ) = @_;
275  
276 # If the outer element defines a scope, we're true if and only if
277 # the outer element contains the inner element.
278 0 ▶ $outer_elem->scope()
Branch: 1 branch
BranchTrueFalseTotal
$outer_elem->scope and return $inner_elem->descendant_of($outer_elem) 0 0 0
279 and return $inner_elem->descendant_of( $outer_elem );
280  
281 # In the more general case:
282  
283 # The last element of the statement containing the outer element
284 # must be before the inner element. If not, we know we're false,
285 # without walking the parse tree.
286  
287 0 ▶ my $stmt = $outer_elem->statement()
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $outer_elem->statement or return 0 0 0
288 or return;
289 0 ▶ my $last_elem = $stmt->last_element()
Branch: 1 branch
BranchTrueFalseTotal
my $last_elem = $stmt->last_element or return 0 0 0
290 or return;
291  
292 0 ▶ my $stmt_loc = $last_elem->location()
Branch: 1 branch
BranchTrueFalseTotal
my $stmt_loc = $last_elem->location or return 0 0 0
293 or return;
294  
295 0 ▶ my $inner_loc = $inner_elem->location()
Branch: 1 branch
BranchTrueFalseTotal
my $inner_loc = $inner_elem->location or return 0 0 0
296 or return;
297  
298 0 ▶ $stmt_loc->[0] > $inner_loc->[0]
Branch: 1 branch
BranchTrueFalseTotal
$stmt_loc->[0] > $inner_loc->[0] and return 0 0 0
299 and return;
300 0 ▶ $stmt_loc->[0] == $inner_loc->[0]
Branch: 1 branch
BranchTrueFalseTotal
$stmt_loc->[0] == $inner_loc->[0] and $stmt_loc->[1] > $inner_loc->[1] and return 0 0 0
Condition0%0/3
$stmt_loc->[0] == $inner_loc->[0] and $stmt_loc->[1] > $inner_loc->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$stmt_loc->[0] == $inner_loc->[0] and $stmt_loc->[1] > $inner_loc->[1]
$stmt_loc->[0] == $inner_loc->[0] $stmt_loc->[1] > $inner_loc->[1]
Truth table0%0/3
A and B
A= $stmt_loc->[0] == $inner_loc->[0]B= $stmt_loc->[1] > $inner_loc->[1]
ABresult
0X0
100
111
301 and $stmt_loc->[1] > $inner_loc->[1]
302 and return;
303  
304 # Since we know the inner element is after the outer element, find
305 # the element that defines the scope of the statement that contains
306 # the outer element.
307  
308 0 my $parent = $stmt;
309 0 ▶ while ( ! $parent->scope() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->scope) 0 0 0
310 0 ▶ $parent = $parent->parent()
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent or return 0 0 0
311 or return;
312 }
313  
314 # We're true if and only if the scope of the outer element contains
315 # the inner element.
316  
317 0 return $inner_elem->descendant_of( $parent );
318  
319 }
320  
321 #-----------------------------------------------------------------------------
322  
323 52 ▶sub filename {
Subroutine filename: called CC=3 SCAR=11.0
Pod documented
324 52 my ($self) = @_;
325  
326 52 ▶ if (defined $self->{_filename_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_filename_override'}) { } 2 50 52
327 2 return $self->{_filename_override};
328 }
329 else {
330 50 my $doc = $self->{_doc};
331 50 ▶ return $doc->can('filename') ? $doc->filename() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$doc->can('filename') ? : 50 0 50
332 }
333 }
334  
335 #-----------------------------------------------------------------------------
336  
337 6 ▶sub highest_explicit_perl_version {
Subroutine highest_explicit_perl_version: called CC=4 SCAR=13.9
Pod documented
338 6 my ($self) = @_;
339  
340 my $highest_explicit_perl_version =
341 6 $self->{_highest_explicit_perl_version};
342  
343 6 ▶ if ( not exists $self->{_highest_explicit_perl_version} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_highest_explicit_perl_version"}) 6 0 6
344 6 my $includes = $self->find( \&_is_a_version_statement );
345  
346 6 ▶ if ($includes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($includes) { } 5 1 6
347 # Note: this doesn't use List::Util::max() because that function
348 # doesn't use the overloaded ">=" etc of a version object. The
349 # reduce() style lets version.pm take care of all comparing.
350 #
351 # For reference, max() ends up looking at the string converted to
352 # an NV, or something like that. An underscore like "5.005_04"
353 # provokes a warning and is chopped off at "5.005" thus losing the
354 # minor part from the comparison.
355 #
356 # An underscore "5.005_04" is supposed to mean an alpha release
357 # and shouldn't be used in a perl version. But it's shown in
358 # perlfunc under "use" (as a number separator), and appears in
359 # several modules supplied with perl 5.10.0 (like version.pm
360 # itself!). At any rate if version.pm can understand it then
361 # that's enough for here.
362 $highest_explicit_perl_version =
363 4 ▶ reduce { $a >= $b ? $a : $b }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$a >= $b ? : 2 2 4
364 9 map { version->new( $_->version() ) }
365 5 @{$includes};
366 }
367 else {
368 1 $highest_explicit_perl_version = undef;
369 }
370  
371 $self->{_highest_explicit_perl_version} =
372 6 $highest_explicit_perl_version;
373 }
374  
375 6 ▶ return $highest_explicit_perl_version if $highest_explicit_perl_version;
Branch: 1 branch
BranchTrueFalseTotal
if $highest_explicit_perl_version 5 1 6
376 1 return;
377 }
378  
379 #-----------------------------------------------------------------------------
380  
381 3 ▶sub uses_module {
Subroutine uses_module: called CC=1 SCAR=0.0
Pod documented
382 3 my ($self, $module_name) = @_;
383  
384 3 return exists $self->_modules_used()->{$module_name};
385 }
386  
387 #-----------------------------------------------------------------------------
388  
389 5 ▶sub process_annotations {
Subroutine process_annotations: called CC=1 SCAR=0.0
Pod documented
390 5 my ($self) = @_;
391  
392 5 my @annotations = Perl::ToPerl6::Annotation->create_annotations($self);
393 5 $self->add_annotation(@annotations);
394 5 return $self;
395 }
396  
397 #-----------------------------------------------------------------------------
398  
399 24 ▶sub line_is_disabled_for_transformer {
Subroutine line_is_disabled_for_transformer: called CC=6 SCAR=19.9
Pod documented
400 24 my ($self, $line, $transformer) = @_;
401 24 ▶ my $transformer_name = ref $transformer || $transformer;
Condition33%1/3
ref $transformer || $transformer
l!l&&r!l&&!r
2400
MC/DC0%0/2
ref $transformer || $transformer
ref $transformer $transformer
Truth table33%1/3
A || B
A= ref $transformerB= $transformer
ABresult
1X1
011
000
402  
403 # HACK: This Transformer is special. If it is active, it cannot be
404 # disabled by a "## no mogrify" annotation. Rather than create a general
405 # hook in Transformer.pm for enabling this behavior, we chose to hack
406 # it here, since this isn't the kind of thing that most transformers do
407  
408 24 ▶ return 0 if $transformer_name eq
Branch: 1 branch
BranchTrueFalseTotal
if $transformer_name eq "Perl::ToPerl6::Transformer::Miscellanea::ProhibitUnrestrictedNoCritic" 0 24 24
409 'Perl::ToPerl6::Transformer::Miscellanea::ProhibitUnrestrictedNoCritic';
410  
411 24 ▶ return 0 unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 24 24
412 24 ▶ return 1 if $self->{_disabled_line_map}->{$line}->{$transformer_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_disabled_line_map"}{$line}{$transformer_name} 0 24 24
413 24 ▶ return 1 if $self->{_disabled_line_map}->{$line}->{ALL};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_disabled_line_map"}{$line}{"ALL"} 0 24 24
414 24 return 0;
415 }
416  
417 #-----------------------------------------------------------------------------
418  
419 5 ▶sub add_annotation {
Subroutine add_annotation: called CC=5 SCAR=23.0
Pod documented
420 5 my ($self, @annotations) = @_;
421  
422 # Add annotation to our private map for quick lookup
423 5 for my $annotation (@annotations) {
424  
425 0 my ($start, $end) = $annotation->effective_range();
426 0 ▶ my @affected_transformers = $annotation->disables_all_transformers ?
Branch: 1 branch
BranchTrueFalseTotal
$annotation->disables_all_transformers ? : 0 0 0
427 qw(ALL) : $annotation->disabled_transformers();
428  
429 # TODO: Find clever way to do this with hash slices
430 0 for my $line ($start .. $end) {
431 0 for my $transformer (@affected_transformers) {
432 0 $self->{_disabled_line_map}->{$line}->{$transformer} = 1;
433 }
434 }
435 }
436  
437 5 push @{ $self->{_annotations} }, @annotations;
438 5 return $self;
439 }
440  
441 #-----------------------------------------------------------------------------
442  
443 0 ▶sub annotations {
Subroutine annotations: not called CC=1 SCAR=6.9
Pod documented
444 0 my ($self) = @_;
445 0 return @{ $self->{_annotations} };
446 }
447  
448 #-----------------------------------------------------------------------------
449  
450 0 ▶sub add_suppressed_transformation {
Subroutine add_suppressed_transformation: not called CC=1 SCAR=6.9
Pod documented
451 0 my ($self, $transformation) = @_;
452 0 push @{$self->{_suppressed_transformations}}, $transformation;
453 0 return $self;
454 }
455  
456 #-----------------------------------------------------------------------------
457  
458 0 ▶sub suppressed_transformations {
Subroutine suppressed_transformations: not called CC=1 SCAR=6.9
Pod documented
459 0 my ($self) = @_;
460 0 return @{ $self->{_suppressed_transformations} };
461 }
462  
463 #-----------------------------------------------------------------------------
464  
465 1 ▶sub is_program {
Subroutine is_program: called CC=1 SCAR=0.0
Pod documented
466 1 my ($self) = @_;
467  
468 1 return not $self->is_module();
469 }
470  
471 #-----------------------------------------------------------------------------
472  
473 7 ▶sub is_module {
Subroutine is_module: called CC=1 SCAR=0.0
Pod documented
474 7 my ($self) = @_;
475  
476 7 return $self->{_is_module};
477 }
478  
479 #-----------------------------------------------------------------------------
480 # PRIVATE functions & methods
481  
482 54 ▶sub _is_a_version_statement {
Subroutine _is_a_version_statement: called CC=3 SCAR=11.4
483 54 my (undef, $element) = @_;
484  
485 54 ▶ return 0 if not $element->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement::Include") 45 9 54
486 9 ▶ return 1 if $element->version();
Branch: 1 branch
BranchTrueFalseTotal
if $element->version 9 0 9
487 0 return 0;
488 }
489  
490 #-----------------------------------------------------------------------------
491  
492 19 ▶sub _caching_finder {
Subroutine _caching_finder: called CC=1 SCAR=0.0
493 19 my $cache_ref = shift; # These vars will persist for the life
494 19 my %isa_cache = (); # of the code ref that this sub returns
495  
496  
497 # Gather up all the PPI elements and sort by @ISA. Note: if any
498 # instances used multiple inheritance, this implementation would
499 # lead to multiple copies of $element in the $elements_of lists.
500 # However, PPI::* doesn't do multiple inheritance, so we are safe
501  
502 240 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
503 240 my (undef, $element) = @_;
504 240 my $classes = $isa_cache{ref $element};
505 240 ▶ if ( !$classes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($classes) 110 130 240
506 110 $classes = [ ref $element ];
507 # Use a C-style loop because we append to the classes array inside
508 110 ▶ for ( my $i = 0; $i < @{$classes}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$classes;}) 385 110 495
509 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
510 385 push @{$classes}, @{"$classes->[$i]::ISA"};
511 385 ▶ $cache_ref->{$classes->[$i]} ||= [];
Condition100%2/2
$$cache_ref{$classes->[$i]} ||= []
l!l
175210
MC/DC100%1/1
$$cache_ref{$classes->[$i]} ||= []
$$cache_ref{$classes->[$i]}
Truth table100%2/2
A
A= $$cache_ref{$classes->[$i]}
Aresult
11
00
512 }
513 110 $isa_cache{$classes->[0]} = $classes;
514 }
515  
516 240 for my $class ( @{$classes} ) {
517 784 push @{$cache_ref->{$class}}, $element;
518 }
519  
520 240 return 0; # 0 tells find() to keep traversing, but not to store this $element
521 19 };
522 }
523  
524 #-----------------------------------------------------------------------------
525  
526 51 ▶sub _disable_shebang_fix {
Subroutine _disable_shebang_fix: called CC=3 SCAR=12.0
527 51 my ($self) = @_;
528  
529 # When you install a program using ExtUtils::MakeMaker or Module::Build, it
530 # inserts some magical code into the top of the file (just after the
531 # shebang). This code allows people to call your program using a shell,
532 # like `sh my_script`. Unfortunately, this code causes several Transformer
533 # transformations, so we disable them as if they had "## no mogrify" annotations.
534  
535 51 ▶ my $first_stmnt = $self->schild(0) || return;
Condition100%2/2
$self->schild(0) || (return)
l!l
492
MC/DC100%1/1
$self->schild(0) || (return)
$self->schild(0)
Truth table100%2/2
A
A= $self->schild(0)
Aresult
11
00
536  
537 # Different versions of MakeMaker and Build use slightly different shebang
538 # fixing strings. This matches most of the ones I've found in my own Perl
539 # distribution, but it may not be bullet-proof.
540  
541 49 my $fixin_rx = qr<^eval 'exec .* \$0 \$[{]1[+]"\$@"}'\s*[\r\n]\s*if.+;>ms; ## no mogrify (ExtendedFormatting)
542 49 ▶ if ( $first_stmnt =~ $fixin_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_stmnt =~ /$fixin_rx/ms) 0 49 49
543 0 my $line = $first_stmnt->location->[0];
544 0 $self->{_disabled_line_map}->{$line}->{ALL} = 1;
545 0 $self->{_disabled_line_map}->{$line + 1}->{ALL} = 1;
546 }
547  
548 49 return $self;
549 }
550  
551 #-----------------------------------------------------------------------------
552  
553 51 ▶sub _determine_is_module {
Subroutine _determine_is_module: called CC=9 SCAR=27.8
554 51 my ($self, $args) = @_;
555  
556 51 my $file_name = $self->filename();
557 51 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_name and ref $args->{"-program-extensions"} eq "ARRAY") 0 51 51
Condition66%2/3
defined $file_name and ref $args->{"-program-extensions"} eq "ARRAY"
!ll&&!rl&&r
5010
MC/DC0%0/2
defined $file_name and ref $args->{"-program-extensions"} eq "ARRAY"
defined $file_name ref $args->{"-program-extensions"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $file_nameB= ref $args->{"-program-extensions"} eq "ARRAY"
ABresult
0X0
100
111
558 defined $file_name
559 and ref $args->{'-program-extensions'} eq 'ARRAY'
560 ) {
561 0 foreach my $ext ( @{ $args->{'-program-extensions'} } ) {
562 0 ▶ my $regex =
Branch: 1 branch
BranchTrueFalseTotal
ref $ext eq 'Regexp' ? : 0 0 0
563 ref $ext eq 'Regexp'
564 ? $ext
565 0 : qr< @{ [ quotemeta $ext ] } \z >xms;
566  
567 0 ▶ return $FALSE if $file_name =~ m/$regex/smx;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /$regex/msx 0 0 0
568 }
569 }
570  
571 51 ▶ return $FALSE if shebang_line($self);
Branch: 1 branch
BranchTrueFalseTotal
if shebang_line($self) 1 50 51
572 50 ▶ return $FALSE if defined $file_name && $file_name =~ m/ [.] PL \z /smx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file_name and $file_name =~ / [.] PL \z /msx 1 49 50
Condition66%2/3
defined $file_name and $file_name =~ / [.] PL \z /msx
!ll&&!rl&&r
4901
MC/DC50%1/2
defined $file_name and $file_name =~ / [.] PL \z /msx
defined $file_name $file_name =~ / [.] PL \z /msx
Truth table66%2/3
A and B
A= defined $file_nameB= $file_name =~ / [.] PL \z /msx
ABresult
0X0
100
111
573  
574 49 return $TRUE;
575 }
576  
577 #-----------------------------------------------------------------------------
578  
579 2 ▶sub _nodes_by_namespace {
Subroutine _nodes_by_namespace: called CC=4 SCAR=14.0
580 2 my ($self) = @_;
581  
582 2 my $nodes = $self->{_nodes_by_namespace};
583  
584 2 ▶ return $nodes if $nodes;
Branch: 1 branch
BranchTrueFalseTotal
if $nodes 0 2 2
585  
586 2 my $ppi_document = $self->ppi_document();
587 2 ▶ if (not $ppi_document) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ppi_document) 0 2 2
588 0 return $self->{_nodes_by_namespace} = {};
589 }
590  
591 2 my $raw_nodes_map = split_ppi_node_by_namespace($ppi_document);
592  
593 2 my %wrapped_nodes;
594 2 ▶ while ( my ($namespace, $raw_nodes) = each %{$raw_nodes_map} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$raw_nodes_map;}) 4 2 6
595 $wrapped_nodes{$namespace} = [
596 4 map { __PACKAGE__->_new_for_parent_document($_, $self) }
597 4 @{$raw_nodes}
598 ];
599 }
600  
601 2 return $self->{_nodes_by_namespace} = \%wrapped_nodes;
602 }
603  
604 #-----------------------------------------------------------------------------
605  
606 # Note: must use exists on return value to determine membership because all
607 # the values are false, unlike the result of hashify().
608 3 ▶sub _modules_used {
Subroutine _modules_used: called CC=5 SCAR=16.1
609 3 my ($self) = @_;
610  
611 3 my $mapping = $self->{_modules_used};
612  
613 3 ▶ return $mapping if $mapping;
Branch: 1 branch
BranchTrueFalseTotal
if $mapping 1 2 3
614  
615 2 my $includes = $self->find('PPI::Statement::Include');
616 2 ▶ if (not $includes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($includes) 1 1 2
617 1 return $self->{_modules_used} = {};
618 }
619  
620 1 my %mapping;
621 1 for my $module (
622 1 ▶ grep { $_ } map { $_->module() || $_->pragma() } @{$includes}
Condition33%1/3
$_->module || $_->pragma
l!l&&r!l&&!r
100
MC/DC0%0/2
$_->module || $_->pragma
$_->module $_->pragma
Truth table33%1/3
A || B
A= $_->moduleB= $_->pragma
ABresult
1X1
011
000
623 ) {
624 # Significanly ess memory than $h{$k} => 1. Thanks Mr. Lembark.
625 1 $mapping{$module} = ();
626 }
627  
628 1 return $self->{_modules_used} = \%mapping;
629 }
630  
631 #-----------------------------------------------------------------------------
632  
633 13 1;
634  
635 __END__
« lib/Perl/ToPerl6/Utils/PPI.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Variables/QuoteHashKeys.pm »