blib/lib/Dist/Zilla/Role/PPI.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 85.7% 12 / 14 Conditioncond 85.7% 12 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 87.7% 86 / 98 CC 13 SCAR 16.2
CC
13
Cov
88%
__ANON__
9.0
ppi_document_for_file
3.0
document_assigns_to_variable
3.0
CRAP 5.1
« blib/lib/Dist/Zilla/Tester.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PodVersion.pm »
1 package Dist::Zilla::Role::PPI 6.040;
2 # ABSTRACT: a role for plugins which use PPI
3  
4 17 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =head1 DESCRIPTION
13 #pod
14 #pod This role provides some common utilities for plugins which use L<PPI>.
15 #pod
16 #pod =method ppi_document_for_file
17 #pod
18 #pod my $document = $self->ppi_document_for_file($file);
19 #pod
20 #pod Given a dzil file object (anything that does L<Dist::Zilla::Role::File>), this
21 #pod method returns a new L<PPI::Document> for that file's content.
22 #pod
23 #pod Internally, this method caches these documents. If multiple plugins want a
24 #pod document for the same file, this avoids reparsing it.
25 #pod
26 #pod =cut
27  
28 17 my %CACHE;
29  
30 161 ▶my sub _cache_key {
Subroutine _cache_key: called CC=1 SCAR=0.0
31 161 my ($name, $encoded_content) = @_;
32 161 return join qq{\0}, $name, md5($encoded_content);
33 }
34  
35 161 ▶sub ppi_document_for_file {
Subroutine ppi_document_for_file: called CC=3 SCAR=11.0
Pod documented
36 161 my ($self, $file) = @_;
37  
38 161 my $encoded_content = $file->encoded_content;
39  
40 # We cache on the MD5 checksum to detect if the document has been modified
41 # by some other plugin since it was last parsed, making our document invalid.
42 # The filename is part of the key because the document records it, and two
43 # files with identical content must not share a document.
44 161 my $key = _cache_key($file->name, $encoded_content);
45 161 ▶ return $CACHE{$key}->clone if $CACHE{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $CACHE{$key} 89 72 161
46  
47 72 my $content = $file->content;
48  
49 72 require PPI::Document;
50 72 ▶ my $document = PPI::Document->new(\$content, filename => $file->name)
Branch: 1 branch
BranchTrueFalseTotal
my $document = "PPI::Document"->new(\$content, "filename", $file->name) or Carp::croak("PPI::Document"->errstr . ' while processing file ' . $file->name) 72 0 72
51 or Carp::croak(PPI::Document->errstr . ' while processing file ' . $file->name);
52  
53 72 return ($CACHE{$key} = $document)->clone;
54 }
55  
56 #pod =method save_ppi_document_to_file
57 #pod
58 #pod my $document = $self->save_ppi_document_to_file($document,$file);
59 #pod
60 #pod Given a L<PPI::Document> and a dzil file object (anything that does
61 #pod L<Dist::Zilla::Role::File>), this method saves the serialized document in the
62 #pod file.
63 #pod
64 #pod It also updates the internal PPI document cache with the new document.
65 #pod
66 #pod =cut
67  
68 0 ▶sub save_ppi_document_to_file {
Subroutine save_ppi_document_to_file: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($self, $document, $file) = @_;
70  
71 0 my $new_content = $document->serialize;
72  
73 0 $file->content($new_content);
74  
75 0 my $encoded = $file->encoded_content;
76  
77 0 $CACHE{ _cache_key($file->name, $encoded) } = $document->clone;
78 }
79  
80 #pod =method document_assigns_to_variable
81 #pod
82 #pod if( $self->document_assigns_to_variable($document, '$FOO')) { ... }
83 #pod
84 #pod This method returns true if the document assigns to the given variable (the
85 #pod sigil must be included).
86 #pod
87 #pod =cut
88  
89 59 ▶sub document_assigns_to_variable {
Subroutine document_assigns_to_variable: called CC=3 SCAR=11.0
Pod documented
90 59 my ($self, $document, $variable) = @_;
91  
92 59 my $package_stmts = $document->find('PPI::Statement::Package');
93 59 ▶ my @namespaces = map { $_->namespace } @{ $package_stmts || []};
Condition50%1/2
$package_stmts || []
l!l
590
MC/DC0%0/1
$package_stmts || []
$package_stmts
Truth table50%1/2
A
A= $package_stmts
Aresult
11
00
94  
95 59 my ($sigil, $varname) = ($variable =~ m'^([$@%*])(.+)$');
96  
97 59 my $package;
98 1370 ▶ my $finder = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
99 1370 my $node = $_[1];
100  
101 1370 ▶ if ($node->isa('PPI::Statement')
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Statement") and not $node->isa("PPI::Statement::End") and not $node->isa("PPI::Statement::Data")) 217 1153 1370
Condition83%5/6
$node->isa("PPI::Statement") and not $node->isa("PPI::Statement::End") and not $node->isa("PPI::Statement::Data")
!ll&&!rl&&r
11530217
$node->isa("PPI::Statement") and not $node->isa("PPI::Statement::End")
!ll&&!rl&&r
11512217
MC/DC66%2/3
$node->isa("PPI::Statement") and not $node->isa("PPI::Statement::End") and not $node->isa("PPI::Statement::Data")
$node->isa("PPI::Statement") not $node->isa("PPI::Statement::End") not $node->isa("PPI::Statement::Data")
Truth table75%3/4
A and not(B) and not(C)
A= $node->isa("PPI::Statement")B= not $node->isa("PPI::Statement::End")C= not $node->isa("PPI::Statement::Data")
ABCresult
0XX0
10X0
1100
1111
102 && !$node->isa('PPI::Statement::End')
103 && !$node->isa('PPI::Statement::Data')) {
104  
105 217 ▶ if ($node->isa('PPI::Statement::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Statement::Variable")) 25 192 217
106 25 ▶ return (grep { $_ eq $variable } $node->variables) ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $variable;} $node->variables) ? : 6 19 25
107 }
108  
109 return 1 if grep {
110 192 ▶ my $child = $_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {my $child = $_; $child->isa("PPI::Token::Symbol") and grep {$child->canonical eq "$sigil${_}::$varname" and $node->content =~ /\Q$sigil$_::$varname\E.*=/u;} @namespaces;} $node->children 3 189 192
111 $child->isa('PPI::Token::Symbol')
112 and grep {
113 737 ▶ $child->canonical eq "${sigil}${_}::${varname}"
Condition100%6/6
$child->isa('PPI::Token::Symbol') && grep({$child->canonical eq "$sigil${_}::$varname" and $node->content =~ /\Q$sigil$_::$varname\E.*=/u;} @namespaces)
!ll&&!rl&&r
722123
$child->canonical eq "$sigil${_}::$varname" && $node->content =~ /\Q$sigil$_::$varname\E.*=/u
!ll&&!rl&&r
1113
MC/DC100%2/2
$child->isa('PPI::Token::Symbol') && grep({$child->canonical eq "$sigil${_}::$varname" and $node->content =~ /\Q$sigil$_::$varname\E.*=/u;} @namespaces)
$child->isa('PPI::Token::Symbol') grep({$child->canonical eq "$sigil${_}::$varname" and $node->content =~ /\Q$sigil$_::$varname\E.*=/u;} @namespaces)
MC/DC100%2/2
$child->canonical eq "$sigil${_}::$varname" && $node->content =~ /\Q$sigil$_::$varname\E.*=/u
$child->canonical eq "$sigil${_}::$varname" $node->content =~ /\Q$sigil$_::$varname\E.*=/u
Truth table100%3/3
A && B
A= $child->isa('PPI::Token::Symbol')B= grep({$child->canonical eq "$sigil${_}::$varname" and $node->content =~ /\Q$sigil$_::$varname\E.*=/u;} @namespaces)
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= $child->canonical eq "$sigil${_}::$varname"B= $node->content =~ /\Q$sigil$_::$varname\E.*=/u
ABresult
0X0
100
111
114 and $node->content =~ /\Q${sigil}${_}::${varname}\E.*=/
115 } @namespaces
116 } $node->children;
117 }
118 1342 return 0; # not found
119 59 };
120  
121 59 my $rv = $document->find_any($finder);
122 59 ▶ Carp::croak($document->errstr) unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 59 59
123  
124 59 return $rv;
125 }
126  
127 17 1;
128  
129 __END__
« blib/lib/Dist/Zilla/Tester.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PodVersion.pm »