blib/lib/CPANfile/Parse/PPI.pm

filter: Statementstmt 98.1% 106 / 108 Branchbran 88.7% 55 / 62 Conditioncond 85.7% 12 / 14 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 94.1% 16 / 17 Podpod 100% 1 / 1 totaltotal 93.3% 197 / 211 CC 39 SCAR 26.7
CC
39
Cov
94%
_parse
26.1
_scan_attrs
9.0
__ANON__
2.1
CRAP 14.4
↑ Summary
1 package CPANfile::Parse::PPI;
2  
3 # ABSTRACT: Parse I<cpanfile>s with PPI
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 our $VERSION = '0.06'; # VERSION
9  
10 12 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use List::Util qw(first any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 my $strict;
16  
17 has meta => (
18 is => 'ro',
19 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 28 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
21 28 ▶ die if 'HASH' ne ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $_[0] 0 28 28
22 }
23 12 );
24  
25 has modules => (
26 is => 'ro',
27 28 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
28 28 ▶ die if 'ARRAY' ne ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $_[0] 0 28 28
29 }
30 12 );
31  
32  
33 31 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
34 31 my ($class, $file_or_code) = @_;
35  
36 31 my ($meta, @modules) = _parse( $file_or_code );
37  
38 return {
39 28 modules => \@modules,
40 meta => $meta,
41 };
42 }
43  
44 12 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
45 12 ▶ $strict = 1 if grep{ $_ eq '-strict' }@_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "-strict";} @_ 3 9 12
46 }
47  
48 31 ▶sub _parse {
Subroutine _parse: called CC=26 SCAR=32.6
49 31 my ($file_or_code) = @_;
50  
51 31 my $doc = PPI::Document->new( $file_or_code );
52  
53 # 'feature' and 'on' are handled separately
54 31 my @bindings = qw(
55 mirror osname
56 requires recommends conflicts suggests
57 test_requires author_requires configure_requires build_requires
58 );
59  
60 my $requires = $doc->find(
61 3777 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
62 3777 ▶ $_[1]->isa('PPI::Token::Word') and do {
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and do { my $content = $_[1]->content; &first(sub { $content eq $_; } , @bindings) } 432 3345 3777
63 432 my $content = $_[1]->content;
64 432 ▶ first { $content eq $_ } @bindings;
Subroutine __ANON__: called CC=1 SCAR=0.0
65 }
66 }
67 31 );
68  
69 31 ▶ return if !$requires;
Branch: 1 branch
BranchTrueFalseTotal
unless $requires 0 31 31
70  
71 31 my @modules;
72 31 my $meta = {};
73  
74 REQUIRED:
75 31 ▶ for my $required ( @{ $requires || [] } ) {
Condition50%1/2
$requires || []
l!l
310
MC/DC0%0/1
$requires || []
$requires
Truth table50%1/2
A
A= $requires
Aresult
11
00
76  
77 # 'mirror' can be an attribute for "requires" as well as a keyword
78 # _scan_attrs should have removed all 'mirrors' that are used as
79 # an attribute for 'requires'. So skip those PPI nodes...
80 304 ▶ next REQUIRED if !$required;
Branch: 1 branch
BranchTrueFalseTotal
unless $required 0 304 304
81  
82  
83 304 my $value = $required->snext_sibling;
84  
85 304 my $type = $required->content;
86 304 my %on_feature = (
87 on => '',
88 feature => '',
89 );
90  
91 304 ▶ if ( $type eq 'mirror' or $type eq 'osname' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "mirror" or $type eq "osname") 2 302 304
Condition66%2/3
$type eq "mirror" or $type eq "osname"
l!l&&r!l&&!r
20302
MC/DC50%1/2
$type eq "mirror" or $type eq "osname"
$type eq "mirror" $type eq "osname"
Truth table66%2/3
A or B
A= $type eq "mirror"B= $type eq "osname"
ABresult
1X1
011
000
92 0 ▶ push @{ $meta->{$type} }, $value->content if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 2 2
93 }
94  
95 304 ▶ if ( -1 != index $type, '_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($type, "_") != -1) 2 302 304
96 2 (my $stage, $type) = split /_/, $type, 2;
97 2 ▶ $stage = 'develop' if $stage eq 'author';
Branch: 1 branch
BranchTrueFalseTotal
if $stage eq "author" 1 1 2
98 2 $on_feature{on} = $stage;
99 }
100  
101 304 my %attr = _scan_attrs( $required, $type );
102  
103 304 ▶ next REQUIRED if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 2 302 304
104  
105 302 ▶ my $can_string = $value->can('string') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value->can('string') ? : 300 2 302
106 302 ▶ my $prereq = $can_string ?
Branch: 1 branch
BranchTrueFalseTotal
$can_string ? : 300 2 302
107 $value->string :
108 $value->content;
109  
110 #next REQUIRED if $prereq eq 'perl';
111  
112 302 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa("PPI::Token::Symbol") or $prereq =~ /\A[^A-Za-z]/) 6 296 302
Condition100%3/3
$value->isa("PPI::Token::Symbol") or $prereq =~ /\A[^A-Za-z]/
l!l&&r!l&&!r
24296
MC/DC100%2/2
$value->isa("PPI::Token::Symbol") or $prereq =~ /\A[^A-Za-z]/
$value->isa("PPI::Token::Symbol") $prereq =~ /\A[^A-Za-z]/
Truth table100%3/3
A or B
A= $value->isa("PPI::Token::Symbol")B= $prereq =~ /\A[^A-Za-z]/
ABresult
1X1
011
000
113 $value->isa('PPI::Token::Symbol') ||
114 $prereq =~ m{\A[^A-Za-z]}
115 ) {
116 6 ▶ carp 'Cannot handle dynamic code' if !$strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 3 3 6
117 6 ▶ croak 'Cannot handle dynamic code' if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 3 3 6
118  
119 3 next REQUIRED;
120 }
121  
122 296 my $parent_node = $value;
123  
124 PARENT:
125 296 while ( 1 ) {
126 593 $parent_node = $parent_node->parent;
127 593 ▶ last PARENT if !$parent_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_node 0 593 593
128 593 ▶ last PARENT if $parent_node->isa('PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
if $parent_node->isa("PPI::Document") 158 435 593
129  
130 435 ▶ if ( $parent_node->isa('PPI::Structure::Block') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_node->isa("PPI::Structure::Block")) 139 296 435
131 139 $parent_node = $parent_node->parent;
132 my ($on_feature) = $parent_node->find_first(
133 161 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
134 # need to create token var because 'any' messes up $_
135 161 my $token = $_[1];
136 $token->isa('PPI::Token::Word')
137 && (
138 146 ▶ any { $token->content eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
139 161 ▶ (qw{on feature})
Branch: 1 branch
BranchTrueFalseTotal
$token->isa("PPI::Token::Word") and &any(sub { $token->content eq $_; } , "on", "feature") 140 21 161
140 );
141 }
142 139 );
143 139 ▶ if ($on_feature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_feature) { } 138 1 139
144 138 my $word = $on_feature->snext_sibling;
145 138 ▶ my $condition
Branch: 1 branch
BranchTrueFalseTotal
$word->can('string') ? : 106 32 138
146 = $word->can('string')
147 ? $word->string
148 : $word->content;
149 138 $on_feature{ $on_feature->content } = $condition;
150 138 last PARENT;
151  
152 }
153 else {
154 1 next PARENT;
155 }
156  
157 }
158 }
159  
160 296 my $version = '';
161 296 my $sibling = $value->snext_sibling;
162 SIBLING:
163 296 while ( 1 ) {
164 365 ▶ last SIBLING if !$sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless $sibling 69 296 365
165  
166 296 ▶ do { $sibling = $sibling->snext_sibling; next SIBLING } if !$sibling->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless ($sibling->isa("PPI::Token::Operator")) 69 227 296
167  
168 227 my $value = $sibling->snext_sibling;
169 227 ▶ last SIBLING if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 5 222 227
170  
171 222 ▶ $version = $value->can('string') ? $value->string : $value->content;
Branch: 1 branch
BranchTrueFalseTotal
$value->can('string') ? : 195 27 222
172  
173 222 last SIBLING;
174 }
175  
176 push @modules, {
177 name => $prereq,
178 version => $version,
179 type => $type,
180 stage => $on_feature{on},
181 feature => $on_feature{feature},
182 296 %attr,
183 };
184 }
185  
186 28 return $meta, @modules;
187 }
188  
189 304 ▶sub _scan_attrs {
Subroutine _scan_attrs: called CC=9 SCAR=22.0
190 304 my ($required, $type) = @_;
191  
192 304 ▶ return if $type ne 'requires' && $type ne 'recommends';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "requires" and $type ne "recommends" 5 299 304
Condition100%3/3
$type ne "requires" and $type ne "recommends"
!ll&&!rl&&r
281185
MC/DC100%2/2
$type ne "requires" and $type ne "recommends"
$type ne "requires" $type ne "recommends"
Truth table100%3/3
A and B
A= $type ne "requires"B= $type ne "recommends"
ABresult
0X0
100
111
193  
194 299 my $sibling = $required->snext_sibling;
195  
196 299 my %attr;
197 my @to_delete;
198 299 my $delete;
199  
200 299 ▶ while ( $sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling) 1108 299 1407
201 1108 my $content = $sibling->content;
202 1108 ▶ if ( $content eq 'mirror' or $content eq 'dist' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq "mirror" or $content eq "dist") 8 1100 1108
Condition100%3/3
$content eq "mirror" or $content eq "dist"
l!l&&r!l&&!r
261100
MC/DC100%2/2
$content eq "mirror" or $content eq "dist"
$content eq "mirror" $content eq "dist"
Truth table100%3/3
A or B
A= $content eq "mirror"B= $content eq "dist"
ABresult
1X1
011
000
203 8 $delete = 1;
204 8 my $value_node = $sibling->snext_sibling->snext_sibling;
205 8 ▶ $attr{$content} = $value_node->can('string') ?
Branch: 1 branch
BranchTrueFalseTotal
$value_node->can('string') ? : 8 0 8
206 $value_node->string :
207 $value_node->content;
208 }
209  
210 1108 ▶ push @to_delete, $sibling if $delete;
Branch: 1 branch
BranchTrueFalseTotal
if $delete 32 1076 1108
211 1108 $sibling = $sibling->snext_sibling;
212 }
213  
214 299 $_->remove for @to_delete;
215  
216 299 return %attr;
217 }
218  
219 12 1;
220  
221 __END__
↑ Summary