DocumentName.pm

filter: Statementstmt 79.2% 65 / 82 Branchbran 73.5% 25 / 34 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 73.6% 14 / 19 Podpod 100% 3 / 3 totaltotal 77.8% 123 / 158 CC 22 SCAR 13.9
CC
22
Cov
78%
_api
7.1
extract_via_statement
6.9
import
5.8
CRAP 4.0
↑ Summary DocumentName/Result.pm »
1 3 ▶use 5.006; # our
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package PPIx::DocumentName;
6  
7 # ABSTRACT: Utility to extract a name from a PPI Document
8 3 our $VERSION = '1.01'; # VERSION
9  
10 3 ▶use PPI::Util qw( _Document );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 sub log_info(&@);
14 sub log_debug(&@);
15 sub log_trace(&@);
16  
17 3 my %callers;
18  
19 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.7
20 3 ▶ if ( $INC{'Log/Contextual.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Log/Contextual.pm'}) { } 0 3 3
21 ## Hide from autoprereqs
22 0 require 'Log/Contextual/WarnLogger.pm'; ## no critic (Modules::RequireBarewordIncludes)
23 0 my $deflogger = Log::Contextual::WarnLogger->new( { env_prefix => 'PPIX_DOCUMENTNAME', } );
24 0 Log::Contextual->import( 'log_info', 'log_debug', 'log_trace', '-default_logger' => $deflogger );
25 }
26 else {
27 3 require Carp;
28 0 ▶ *log_info = sub (&@) { Carp::carp( $_[0]->() ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
29 3 ▶ *log_debug = sub (&@) { };
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 ▶ *log_trace = sub (&@) { };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32 }
33  
34 4 ▶sub import {
Subroutine import: called CC=5 SCAR=17.6
35 4 my(undef, %args) = @_;
36 4 ▶ if(defined $args{'-api'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"-api"}) 2 2 4
37 2 ▶ if($args{'-api'} != 0 && $args{'-api'} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-api"} != 0 and $args{"-api"} != 1) 1 1 2
Condition66%2/3
$args{"-api"} != 0 and $args{"-api"} != 1
!ll&&!rl&&r
011
MC/DC50%1/2
$args{"-api"} != 0 and $args{"-api"} != 1
$args{"-api"} != 0 $args{"-api"} != 1
Truth table66%2/3
A and B
A= $args{"-api"} != 0B= $args{"-api"} != 1
ABresult
0X0
100
111
38 1 Carp::croak("illegal api level: $args{'-api'}");
39 }
40 1 ▶ if($] < 5.010) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.01) { } 0 1 1
41 0 my($package) = caller;
42 0 $callers{$package} = $args{'-api'};
43 0 require Carp;
44 0 Carp::carp("Because of the age of your Perl, -api $args{'-api'} " .
45 'will be package scoped instead of block scoped. ' .
46 'Please upgrade to 5.10 or better.');
47 } else {
48 1 $^H{'PPIx::DocumentName/api'} = $args{'-api'}; ## no critic (Variables::RequireLocalizedPunctuationVars)
49 }
50 }
51 }
52  
53 61 ▶sub _api {
Subroutine _api: called CC=6 SCAR=19.6
54 61 my ( $api ) = @_;
55 61 ▶ if($] < 5.010) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.01) { } 0 61 61
56 0 my($package) = caller 1;
57 0 ▶ $api = $callers{$package} unless defined $api;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api 0 0 0
58 } else {
59 61 my $hh = (caller 1)[10];
60 61 ▶ $api = $hh->{'PPIx::DocumentName/api'} if defined $hh && !defined $api;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hh and not defined $api 18 43 61
Condition66%2/3
defined $hh and not defined $api
!ll&&!rl&&r
43018
MC/DC50%1/2
defined $hh and not defined $api
defined $hh not defined $api
Truth table66%2/3
A and not(B)
A= defined $hhB= not defined $api
ABresult
0X0
100
111
61 }
62 61 ▶ $api = 0 unless defined $api;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api 21 40 61
63 61 return $api;
64 }
65  
66 10 ▶sub _result {
Subroutine _result: called CC=1 SCAR=0.0
67 10 my($name, $ppi_document, $node) = @_;
68 10 require PPIx::DocumentName::Result;
69 10 PPIx::DocumentName::Result->_new($name, $ppi_document, $node); ## no critic (Subroutines::ProtectPrivateSubs)
70 }
71  
72 ## OO
73  
74  
75 13 ▶sub extract {
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
76 13 my ( $self, $ppi_document ) = @_;
77 13 my $api = _api(undef);
78 13 ▶ my $result = $self->extract_via_comment($ppi_document, $api) || $self->extract_via_statement($ppi_document, $api);
Condition100%3/3
$self->extract_via_comment($ppi_document, $api) || $self->extract_via_statement($ppi_document, $api)
l!l&&r!l&&!r
454
MC/DC100%2/2
$self->extract_via_comment($ppi_document, $api) || $self->extract_via_statement($ppi_document, $api)
$self->extract_via_comment($ppi_document, $api) $self->extract_via_statement($ppi_document, $api)
Truth table100%3/3
A || B
A= $self->extract_via_comment($ppi_document, $api)B= $self->extract_via_statement($ppi_document, $api)
ABresult
1X1
011
000
79 13 return $result;
80 }
81  
82  
83 22 ▶sub extract_via_statement {
Subroutine extract_via_statement: called CC=6 SCAR=19.3
Pod documented
84 22 my ( undef, $ppi_document, $api ) = @_;
85  
86 22 $api = _api($api);
87  
88 # Keep alive until done
89 # https://github.com/adamkennedy/PPI/issues/112
90 22 my $dom = _Document($ppi_document);
91 22 my $pkg_node = $dom->find_first('PPI::Statement::Package');
92 22 ▶ if ( not $pkg_node ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg_node) 8 14 22
93 0 ▶ log_debug { "No PPI::Statement::Package found in <<$ppi_document>>" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
94 # The old API was inconsistant here, for just this method, returns
95 # empty list on failure. This is unfortunately different from
96 # extract_via_comment.
97 8 ▶ return 1 == $api ? undef : ();
Branch: 1 branch
BranchTrueFalseTotal
1 == $api ? : 4 4 8
98 }
99 14 ▶ if ( not $pkg_node->namespace ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg_node->namespace) 0 14 14
100 0 ▶ log_debug { "PPI::Statement::Package $pkg_node has empty namespace in <<$ppi_document>>" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ return 1 == $api ? undef : ();
Branch: 1 branch
BranchTrueFalseTotal
1 == $api ? : 0 0 0
102 }
103 14 my $name = $pkg_node->namespace;
104 14 ▶ return 1 == $api ? _result($name, $dom, $pkg_node) : $name;
Branch: 1 branch
BranchTrueFalseTotal
1 == $api ? : 6 8 14
105 }
106  
107  
108 26 ▶sub extract_via_comment {
Subroutine extract_via_comment: called CC=4 SCAR=13.9
Pod documented
109 26 my ( undef, $ppi_document, $api ) = @_;
110  
111 26 $api = _api($api);
112 26 my $node;
113  
114 26 my $regex = qr{ ^ \s* \#+ \s* PODNAME: \s* (.+) $ }x; ## no critic (RegularExpressions)
115 26 my $content;
116 196 ▶ my $finder = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
117 196 my $maybe = $_[1];
118 196 ▶ return 0 unless $maybe->isa('PPI::Token::Comment');
Branch: 1 branch
BranchTrueFalseTotal
unless $maybe->isa("PPI::Token::Comment") 188 8 196
119 0 ▶ log_trace { "Found comment node $maybe" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
120 8 ▶ if ( $maybe->content =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe->content =~ /$regex/x) 8 0 8
121 8 $content = $1;
122 8 $node = $maybe;
123 8 return 1;
124 }
125 0 return 0;
126 26 };
127  
128 # Keep alive until done
129 # https://github.com/adamkennedy/PPI/issues/112
130 26 my $dom = _Document($ppi_document);
131 26 $dom->find_first($finder);
132  
133 0 ▶ log_debug { "<<$ppi_document>> has no PODNAME comment" } if not $content;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $content 18 8 26
134  
135 26 ▶ return 1 == $api && defined $content ? _result($content, $dom, $node) : $content;
Branch: 1 branch
BranchTrueFalseTotal
1 == $api && defined $content ? : 4 22 26
Condition100%3/3
1 == $api and defined $content
!ll&&!rl&&r
1484
MC/DC100%2/2
1 == $api and defined $content
1 == $api defined $content
Truth table100%3/3
A and B
A= 1 == $apiB= defined $content
ABresult
0X0
100
111
136 }
137  
138 3 1;
139  
140 __END__
↑ Summary DocumentName/Result.pm »