QuoteLike/Dumper.pm

filter: Statementstmt 77.6% 132 / 170 Branchbran 48.8% 43 / 88 Conditioncond 35.0% 7 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 89.6% 26 / 29 Podpod 100% 5 / 5 totaltotal 66.0% 214 / 324 CC 60 SCAR 33.0
CC
60
Cov
65%
_source_to_dumpers
122.8
list
23.3
dump
8.4
CRAP 27.2
« QuoteLike/Utils.pm ↑ Summary QuoteLike.pm »
1 package PPIx::QuoteLike::Dumper;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PPIx::QuoteLike;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use PPIx::QuoteLike::Constant qw{ @CARP_NOT };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PPIx::QuoteLike::Utils qw{ __instance };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.024';
16  
17 2 ▶use constant SCALAR_REF => ref \0;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 {
20 2 my $default = {
21 encoding => undef,
22 file => undef,
23 indent => 2,
24 locations => 0,
25 margin => 0,
26 perl_version => 0,
27 ppi => 0,
28 short => 0,
29 significant => 0,
30 tokens => 0,
31 variables => 0,
32 };
33  
34 60 ▶ sub new {
Subroutine new: called CC=8 SCAR=21.3
Pod documented
35 60 my ( $class, $source, %arg ) = @_;
36  
37 my $self = {
38 60 %{ $default },
39 object => undef,
40 source => $source,
41 };
42  
43 60 foreach my $key ( keys %{ $default } ) {
44 defined $arg{$key}
45 660 ▶ and $self->{$key} = $arg{$key};
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{$key} and $self->{$key} = $arg{$key} 116 544 660
46 }
47  
48 $self->{object} = _isa( $source, 'PPIx::QuoteLike' ) ? $source :
49 PPIx::QuoteLike->new( $source,
50 __instance( $source, 'PPI::Element' ) ? () : (
51 location => [ 1, 1, 1, 1, -f $source ? $source :
52 undef ],
53 ),
54 60 ▶ map { $_ => $arg{$_} } qw{ encoding },
Branch: 4 branches
BranchTrueFalseTotal
$self->{"object"} = _isa($source, "PPIx::QuoteLike") ? $source : "PPIx::QuoteLike"->new($source, __instance($source, "PPI::Element") ? () : ("location", [1, 1, 1, 1, -f $source ? $source : undef]), map({$_, $arg{$_};} "encoding")) or return 8 52 60
_isa($source, 'PPIx::QuoteLike') ? : 0 60 60
__instance($source, 'PPI::Element') ? : 58 2 60
-f $source ? : 0 2 2
55 )
56 or return;
57  
58 8 ▶ return bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
59 }
60 }
61  
62 2 ▶sub dump : method { ## no critic (ProhibitBuiltinHomonyms)
Subroutine dump: called CC=7 SCAR=21.3
Pod documented
63 2 my ( $class, $source, %arg ) = @_;
64 2 my $rslt;
65 2 ▶ my $margin = ' ' x ( $arg{margin} || 0 );
Condition50%1/2
$arg{'margin'} || 0
l!l
02
MC/DC0%0/1
$arg{'margin'} || 0
$arg{'margin'}
Truth table50%1/2
A
A= $arg{'margin'}
Aresult
11
00
66 2 my $none = delete $arg{none};
67 2 foreach my $obj ( $class->_source_to_dumpers( $source, %arg ) ) {
68 7 my $src = $obj->{object}->source();
69 7 $rslt .= "\n$margin$src";
70 7 ▶ if ( _isa( $src, 'PPI::Element' ) and my $loc = $src->location() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($src, "PPI::Element") and my $loc = $src->location) 6 1 7
Condition66%2/3
_isa($src, "PPI::Element") and my $loc = $src->location
!ll&&!rl&&r
106
MC/DC50%1/2
_isa($src, "PPI::Element") and my $loc = $src->location
_isa($src, "PPI::Element") my $loc = $src->location
Truth table66%2/3
A and B
A= _isa($src, "PPI::Element")B= my $loc = $src->location
ABresult
0X0
100
111
71 $rslt .= sprintf ' %s line %d column %d',
72 6 _dor( $loc->[4], $obj->{file}, '?' ),
73 $loc->[0], $loc->[1];
74 }
75 7 $rslt .= "\n" . $obj->string();
76 }
77 2 ▶ defined $rslt
Branch: 1 branch
BranchTrueFalseTotal
defined $rslt and return $rslt 2 0 2
78 and return $rslt;
79 0 ▶ defined $none
Branch: 1 branch
BranchTrueFalseTotal
defined $none or return 0 0 0
80 or return;
81 0 $none =~ s/ (?: \A | (?<! \n ) ) \z /\n/smx;
82 0 return $none;
83 }
84  
85 7 ▶sub list {
Subroutine list: called CC=12 SCAR=31.5
Pod documented
86 7 my ( $self ) = @_;
87 7 my $indent;
88 7 my $obj = $self->{object};
89 7 my @rslt;
90 my $selector;
91 7 ▶ if ( $self->{tokens} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tokens'}) { } 0 7 7
92 0 $indent = '';
93 0 ▶ $selector = sub { return @{
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 ▶ $obj->find( 'PPIx::QuoteLike::Token' ) || [] };
Condition0%0/2
$obj->find('PPIx::QuoteLike::Token') || []
l!l
00
MC/DC0%0/1
$obj->find('PPIx::QuoteLike::Token') || []
$obj->find('PPIx::QuoteLike::Token')
Truth table0%0/2
A
A= $obj->find('PPIx::QuoteLike::Token')
Aresult
11
00
95 0 };
96 } else {
97 7 $indent = ' ' x $self->{indent};
98 my $string = sprintf '%s%s...%s',
99 7 map { _format_content( $obj, $_ ) }
100 qw{ type start finish };
101 7 push @rslt,
102 join "\t", $self->_class_name( $obj ), $string,
103 _format_attr( $obj, qw{ encoding failures interpolates
104 indentation } ),
105 $self->_perl_version( $obj ),
106 $self->_variables( $obj ),
107 ;
108 7 ▶ $selector = sub { return $obj->children() };
Subroutine __ANON__: called CC=1 SCAR=0.0
109 }
110 7 foreach my $elem ( $selector->() ) {
111 $self->{significant}
112 17 ▶ and not $elem->significant()
Branch: 1 branch
BranchTrueFalseTotal
$self->{"significant"} and not $elem->significant and next 0 17 17
Condition33%1/3
$self->{"significant"} and not $elem->significant
!ll&&!rl&&r
1700
MC/DC0%0/2
$self->{"significant"} and not $elem->significant
$self->{"significant"} not $elem->significant
Truth table33%1/3
A and not(B)
A= $self->{"significant"}B= not $elem->significant
ABresult
0X0
100
111
113 and next;
114 my $locn = $self->{locations} ?
115 17 ▶ __instance( $elem, 'PPIx::QuoteLike::Token' ) ?
Branch: 2 branches
BranchTrueFalseTotal
$self->{'locations'} ? : 0 17 17
__instance($elem, 'PPIx::QuoteLike::Token') ? : 0 0 0
116 sprintf '[ % 4d, % 3d, % 3d ] ',
117 $elem->logical_line_number(),
118 $elem->column_number(),
119 $elem->visual_column_number() :
120 ' ' x 19 :
121 '';
122 17 my @line = (
123 $self->_class_name( $elem ),
124 _quote( $elem->content() ),
125 $self->_perl_version( $elem ),
126 $self->_variables( $elem ),
127 );
128 17 my @ppi;
129 17 ▶ @ppi = $self->_ppi( $elem )
Branch: 1 branch
BranchTrueFalseTotal
@ppi = $self->_ppi($elem) and shift @ppi 0 17 17
130 and shift @ppi; # Ignore PPI::Document
131 17 foreach ( @ppi ) {
132 0 ▶ if ( $self->{locations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'locations'}) { } 0 0 0
133 0 ▶ s/ ( [0-9]+ \s+ \] ) /$1 /smxg
Branch: 1 branch
BranchTrueFalseTotal
s/ ( [0-9]+ \s+ \] ) /$1 /gmsx or substr $_, 0, 0, " " 0 0 0
134 or substr $_, 0, 0, ' ';
135 } else {
136 0 substr $_, 0, 0, ' ';
137 }
138 }
139 17 my $leader = "$locn$indent";
140 17 foreach ( join( "\t", @line ), @ppi ) {
141 17 push @rslt, "$leader$_";
142 # $locn = $self->{locations} ? ' ' x 19 : '';
143 17 $leader = '';
144 }
145 }
146 7 return @rslt;
147 }
148  
149 0 ▶sub print : method { ## no critic (ProhibitBuiltinHomonyms)
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
150 0 my ( $self ) = @_;
151 0 print $self->string();
152 0 return;
153 }
154  
155 7 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
156 7 my ( $self ) = @_;
157 7 my $margin = ' ' x $self->{margin};
158 7 return join '', map { "$margin$_\n" } $self->list();
159 }
160  
161 24 ▶sub _class_name {
Subroutine _class_name: called CC=2 SCAR=7.0
162 24 my ( $self, $obj ) = @_;
163 24 my $class = ref $obj;
164 $self->{short}
165 24 ▶ and $class =~ s/ \A PPIx::QuoteLike:: //smx;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"short"} and $class =~ s/ \A PPIx::QuoteLike:: //msx 0 24 24
166 24 return $class;
167 }
168  
169 {
170 # We have to hold a reference to the PPI document until we're done
171 # with all its elements, otherwise they evaporate. Holding it here
172 # works as long as we actually format the dump for all elements
173 # before calling this again.
174 2 my $doc;
175  
176 1 ▶ sub _doc_to_dumper {
Subroutine _doc_to_dumper: called CC=4 SCAR=14.5
177 1 my ( $class, $path, %arg ) = @_;
178 1 ▶ $doc = PPI::Document->new( $path )
Branch: 1 branch
BranchTrueFalseTotal
$doc = "PPI::Document"->new($path) or return 1 0 1
179 or return;
180 ref $path
181 1 ▶ or $arg{file} = $path;
Branch: 1 branch
BranchTrueFalseTotal
ref $path or $arg{"file"} = $path 1 0 1
182 58 return map { $class->new( $_, %arg ) }
183 1 ▶ @{ $doc->find( 'PPI::Token' ) || [] };
Condition50%1/2
$doc->find('PPI::Token') || []
l!l
10
MC/DC0%0/1
$doc->find('PPI::Token') || []
$doc->find('PPI::Token')
Truth table50%1/2
A
A= $doc->find('PPI::Token')
Aresult
11
00
184 }
185 }
186  
187 2 ▶sub _dor {
Subroutine _dor: called CC=3 SCAR=11.1
188 6 my @arg = @_;
189 6 foreach my $a ( @arg ) {
190 18 ▶ defined $a
Branch: 1 branch
BranchTrueFalseTotal
defined $a and return $a 6 12 18
191 and return $a;
192 }
193 0 return;
194 }
195  
196 7 ▶sub _format_attr {
Subroutine _format_attr: called CC=3 SCAR=11.0
197 7 my ( $obj, @arg ) = @_;
198 7 my @rslt;
199 7 foreach my $attr ( @arg ) {
200 28 ▶ defined( my $val = $obj->$attr() )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $val = $obj->$attr) or next 14 14 28
201 or next;
202 14 push @rslt, sprintf '%s=%s', $attr, _quote( $val );
203 }
204 7 return @rslt;
205 }
206  
207 21 ▶sub _format_content {
Subroutine _format_content: called CC=2 SCAR=7.0
208 21 my ( $obj, $method, @arg ) = @_;
209 21 my @val = map { $_->content() }
210 21 ▶ grep { $_->significant() }
Branch: 1 branch
BranchTrueFalseTotal
my(@val) = map({$_->content;} grep({$_->significant;} $obj->$method(@arg))) or return "?" 21 0 21
211 $obj->$method( @arg )
212 or return '?';
213 21 return join '', @val;
214 }
215  
216 67 ▶sub _isa {
Subroutine _isa: called CC=2 SCAR=6.9
217 67 my ( $arg, $class ) = @_;
218 67 ▶ Scalar::Util::blessed( $arg )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($arg) or return 0 64 3 67
219 or return 0;
220 64 return $arg->isa( $class );
221 }
222  
223 24 ▶sub _perl_version {
Subroutine _perl_version: called CC=3 SCAR=11.0
224 24 my ( $self, $elem ) = @_;
225 $self->{perl_version}
226 24 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"perl_version"} or return 21 3 24
227 21 my $intro = $elem->perl_version_introduced();
228 21 my $remov = $elem->perl_version_removed();
229 21 ▶ return defined $remov ? "$intro <= \$] < $remov" : "$intro <= \$]";
Branch: 1 branch
BranchTrueFalseTotal
defined $remov ? : 0 21 21
230 }
231  
232 17 ▶sub _ppi {
Subroutine _ppi: called CC=3 SCAR=19.2
233 17 my ( $self, $elem ) = @_;
234  
235 $self->{ppi}
236 17 ▶ and $elem->can( 'ppi' )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ppi"} and $elem->can("ppi") or return 0 17 17
Condition33%1/3
$self->{"ppi"} and $elem->can("ppi")
!ll&&!rl&&r
1700
MC/DC0%0/2
$self->{"ppi"} and $elem->can("ppi")
$self->{"ppi"} $elem->can("ppi")
Truth table33%1/3
A and B
A= $self->{"ppi"}B= $elem->can("ppi")
ABresult
0X0
100
111
237 or return;
238  
239 0 require PPI::Dumper;
240  
241 # PPI::Dumper reports line_number(), but I want
242 # logical_line_number(). There is no configuration for this, but the
243 # interface is public, so I mung it to do what I want.
244 0 my $locn = PPI::Element->can( 'location' );
245 0 ▶ local *PPI::Element::location = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 my $loc = $locn->( @_ );
247 0 $loc->[0] = $loc->[3];
248 0 return $loc;
249 0 };
250  
251 my $dumper = PPI::Dumper->new( $elem->ppi(),
252 0 map { $_ => $self->{$_} } qw{ indent locations },
253 );
254  
255 0 return $dumper->list();
256 }
257  
258 31 ▶sub _quote {
Subroutine _quote: called CC=5 SCAR=17.3
259 31 my ( $val ) = @_;
260 31 ▶ ref $val
Branch: 1 branch
BranchTrueFalseTotal
ref $val and $val = $val->content 0 31 31
261 and $val = $val->content();
262 31 ▶ defined $val
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return "undef" 31 0 31
263 or return 'undef';
264 31 ▶ Scalar::Util::looks_like_number( $val )
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($val) and return $val 14 17 31
265 and return $val;
266 17 ▶ if ( $val =~ m/ \A << /smx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ / \A << /msx) 0 17 17
267 0 chomp $val;
268 0 return "<<'__END_OF_HERE_DOCUMENT'
269 $val
270 __END_OF_HERE_DOCUMENT
271 ";
272 }
273  
274 17 $val =~ s/ (?= [\\'] )/\\/smxg;
275 17 return "'$val'";
276 }
277  
278 2 ▶sub _source_to_dumpers {
Subroutine _source_to_dumpers: called CC=15 SCAR=48.1
279 2 my ( $class, $path, %arg ) = @_;
280 2 ▶ if ( Scalar::Util::blessed( $path ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($path)) { } 0 2 2
elsif (my $ref = ref $path) { } 1 1 2
281 0 ▶ if ( _isa( $path, 'PPI::Node' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (_isa($path, 'PPI::Node')) { } 0 0 0
elsif (_isa($path, 'PPI::Element')) { } 0 0 0
282 return map {
283 0 ▶ PPIx::QuoteLike->handles( $_ ) ?
Branch: 1 branch
BranchTrueFalseTotal
'PPIx::QuoteLike'->handles($_) ? : 0 0 0
284 $class->new( $_, %arg ) : () }
285 0 ▶ @{ $path->find( 'PPI::Token' ) || [] };
Condition0%0/2
$path->find('PPI::Token') || []
l!l
00
MC/DC0%0/1
$path->find('PPI::Token') || []
$path->find('PPI::Token')
Truth table0%0/2
A
A= $path->find('PPI::Token')
Aresult
11
00
286 } elsif ( _isa( $path, 'PPI::Element' ) ) {
287 0 ▶ PPIx::QuoteLike->handles( $path )
Branch: 1 branch
BranchTrueFalseTotal
"PPIx::QuoteLike"->handles($path) and return $class->new($path, %arg) 0 0 0
288 and return $class->new( $path, %arg );
289 }
290 } elsif ( my $ref = ref $path ) {
291 1 ▶ SCALAR_REF eq $ref
Branch: 1 branch
BranchTrueFalseTotal
"SCALAR" eq $ref or return 1 0 1
292 or return;
293 1 return $class->_doc_to_dumper( $path, %arg );
294 } else {
295 1 ▶ -f $path
Branch: 1 branch
BranchTrueFalseTotal
-f $path or return $class->new($path, %arg) 0 1 1
296 or return $class->new( $path, %arg );
297 0 ▶ -T _
Branch: 1 branch
BranchTrueFalseTotal
-T _ or return 0 0 0
298 or return;
299 0 ▶ unless ( $path =~ m/ [.] (?: (?i: pl ) | pm | t ) \z /smx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ / [.] (?: (?i: pl ) | pm | t ) \z /msx) 0 0 0
300 0 ▶ open my $fh, '<', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return 0 0 0
301 or return;
302 0 ▶ defined( local $_ = <$fh> )
Branch: 1 branch
BranchTrueFalseTotal
defined(local $_ = readline $fh) or return 0 0 0
303 or return;
304 0 close $fh;
305 0 ▶ m/ perl /smx
Branch: 1 branch
BranchTrueFalseTotal
/ perl /msx or return 0 0 0
306 or return;
307 }
308 0 return $class->_doc_to_dumper( $path, %arg );
309 }
310 0 return;
311 }
312  
313 24 ▶sub _variables {
Subroutine _variables: called CC=3 SCAR=11.0
314 24 my ( $self, $elem ) = @_;
315  
316 $self->{variables}
317 24 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"variables"} or return 21 3 24
318  
319 21 ▶ my @var = $elem->variables()
Branch: 1 branch
BranchTrueFalseTotal
my(@var) = $elem->variables or return 6 15 21
320 or return;
321  
322 6 return join ',', sort @var;
323 }
324  
325 2 1;
326  
327 __END__
« QuoteLike/Utils.pm ↑ Summary QuoteLike.pm »