lib/PPIx/Element/Sub.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 18 / 18 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 83 / 83 CC 14 SCAR 17.8
CC
14
Cov
100%
identify_next_sub
10.0
identify_sub
4.0
identify_associated_sub
2.0
CRAP 6.0
↑ Summary
1 16 ▶use 5.006; # our
Subroutine BEGIN: called CC=1 SCAR=0.0
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package PPIx::Element::Sub;
6  
7 16 our $VERSION = '0.001000'; # TRIAL
8  
9 # ABSTRACT: Find subroutines associated with any element.
10  
11 16 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 16 our @EXPORT_OK = qw( identify_sub identify_next_sub identify_associated_sub );
14  
15 16 ▶use Exporter 5.57 qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 108 ▶sub identify_sub {
Subroutine identify_sub: called CC=4 SCAR=13.9
Pod documented
19 108 my ($element) = @_;
20  
21 # any <sub Foo;> contains itself.
22 108 ▶ return $element if $element->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Statement::Sub") 14 94 108
23  
24 # no parents => can't find a sub => must be main execution
25 94 ▶ return unless $element->can('parent') and defined( my $parent = $element->parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $element->can("parent") and defined(my $parent = $element->parent) 18 76 94
Condition100%3/3
$element->can("parent") and defined(my $parent = $element->parent)
!ll&&!rl&&r
21676
MC/DC100%2/2
$element->can("parent") and defined(my $parent = $element->parent)
$element->can("parent") defined(my $parent = $element->parent)
Truth table100%3/3
A and B
A= $element->can("parent")B= defined(my $parent = $element->parent)
ABresult
0X0
100
111
26  
27 # If we have a parent, ask it for which sub we're in.
28 76 return identify_sub($parent);
29 }
30  
31 81 ▶sub identify_next_sub {
Subroutine identify_next_sub: called CC=10 SCAR=23.0
Pod documented
32 81 my ($element) = @_;
33  
34 # Parentless nodes can't have siblings
35 81 ▶ return unless $element->can('parent') and defined( my $parent = $element->parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $element->can("parent") and defined(my $parent = $element->parent) 16 65 81
Condition100%3/3
$element->can("parent") and defined(my $parent = $element->parent)
!ll&&!rl&&r
21465
MC/DC100%2/2
$element->can("parent") and defined(my $parent = $element->parent)
$element->can("parent") defined(my $parent = $element->parent)
Truth table100%3/3
A and B
A= $element->can("parent")B= defined(my $parent = $element->parent)
ABresult
0X0
100
111
36  
37 # parents without children can't give us siblings
38 # but *their* parents could
39 65 ▶ return identify_next_sub($parent) unless $parent->can('children');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->can("children") 1 64 65
40  
41 64 my $own_addr = refaddr($element);
42 64 my $seen_self = 0;
43  
44 # Search direct siblings deeply
45 64 for my $sibling ( $parent->children ) {
46  
47 # Iterate forwards until self is found.
48 200 ▶ if ( not $seen_self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_self) 145 55 200
49 145 ▶ $seen_self = 1 if refaddr($sibling) eq $own_addr;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($sibling) eq $own_addr 61 84 145
50 145 next;
51 }
52  
53 # For successive siblings
54 # return the first sub found
55 55 ▶ return $sibling if $sibling->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Statement::Sub") 7 48 55
56  
57 # If a sibling has children, a sub could be one of them
58 # note: a false value == fail, but explit undef is invalid query
59 48 ▶ if ( $sibling->can('find_first') and my $result = $sibling->find_first('PPI::Statement::Sub') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling->can("find_first") and my $result = $sibling->find_first("PPI::Statement::Sub")) 2 46 48
Condition100%3/3
$sibling->can("find_first") and my $result = $sibling->find_first("PPI::Statement::Sub")
!ll&&!rl&&r
4422
MC/DC100%2/2
$sibling->can("find_first") and my $result = $sibling->find_first("PPI::Statement::Sub")
$sibling->can("find_first") my $result = $sibling->find_first("PPI::Statement::Sub")
Truth table100%3/3
A and B
A= $sibling->can("find_first")B= my $result = $sibling->find_first("PPI::Statement::Sub")
ABresult
0X0
100
111
60 2 return $result;
61 }
62 }
63  
64 # When no sibling is a sub, assume that the sub is further down
65 # the document, making it a sibling that succeeds a parent, or a descendent
66 # of a parents sibling.
67 55 return identify_next_sub($parent);
68 }
69  
70 16 ▶sub identify_associated_sub {
Subroutine identify_associated_sub: called CC=2 SCAR=6.9
Pod documented
71 16 my ($element) = @_;
72  
73 # First, resolve to the containers, as all comments inside a
74 # sub are related to the sub itself
75 16 ▶ if ( my $parent = identify_sub($element) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = identify_sub($element)) 7 9 16
76 7 return $parent;
77 }
78  
79 # Otherwise, look into the next-sibling tree to find the subsequent sub
80 9 return identify_next_sub($element);
81 }
82  
83 16 1;
84  
85 __END__
↑ Summary