lib/Pod/Coverage/More/Documentation.pm

filter: Statementstmt 100% 183 / 183 Branchbran 91.1% 62 / 68 Conditioncond 84.2% 16 / 19 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 26 / 26 Podpod 100% 7 / 7 totaltotal 96.1% 302 / 314 CC 47 SCAR 18.5
CC
47
Cov
97%
_item
12.2
_read
11.0
_type_at_start
8.0
CRAP 6.4
« lib/Pod/Coverage/More/Analyzer/Perl5_38.pm ↑ Summary lib/Pod/Coverage/More/Declared/FunctionParameters.pm »
1 # ABSTRACT: What the POD of a module says about each of its subs
2 # PODNAME: Pod::Coverage::More::Documentation
3  
4 package Pod::Coverage::More::Documentation 0.003;
5  
6 15 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use List::Util qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Types::Standard qw{Bool InstanceOf Int Str Undef};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 15 ▶use Pod::Coverage::More::Types qw{type_named};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # A reader of POD.
18 # @param subs ArrayRef[Str] optional: the names of the subs of the package, which tell the headings that document subs from the others
19 # @returns Pod::Coverage::More::Documentation the reader
20 # @example my $doc = Pod::Coverage::More::Documentation->new( subs => [ 'resize', 'crop' ] );
21 47 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 47 my ( $class, %args ) = @_;
23 47 ▶ return bless( { subs => $args{subs} ? { map { $_ => 1 } @{ $args{subs} } } : undef, sections => [] }, $class );
Branch: 1 branch
BranchTrueFalseTotal
$args{'subs'} ? : 31 16 47
24 }
25  
26 # Reads the POD of C<file>.
27 # @param file Str the path of the file
28 # @returns Pod::Coverage::More::Documentation the reader
29 # @example my $doc = Pod::Coverage::More::Documentation->new->parse_file('lib/Some/Module.pm');
30 30 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
31 30 my ( $self, $file ) = @_;
32 30 return $self->_read( _parser()->parse_file($file)->root );
33 }
34  
35 # Reads the POD in the string C<pod>.
36 # @param pod Str the POD
37 # @returns Pod::Coverage::More::Documentation the reader
38 # @example my $doc = Pod::Coverage::More::Documentation->new->parse_string($pod);
39 17 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
40 17 my ( $self, $pod ) = @_;
41 17 return $self->_read( _parser()->parse_string_document($pod)->root );
42 }
43  
44 # The sections, in the order of the POD, as SECTIONS describes them.
45 # @returns the sections, as a list of HASHREFs
46 # @example my @headings = map { $_->{heading} } $doc->sections;
47 3 ▶sub sections {
Subroutine sections: called CC=1 SCAR=0.0
Pod documented
48 3 my ($self) = @_;
49 3 return @{ $self->{sections} };
50 }
51  
52 # The first section whose heading names the sub C<name>, or nothing.
53 # @param name Str the name of the sub
54 # @returns HashRef the section
55 # @example my $section = $doc->section_for('resize');
56 262 ▶sub section_for {
Subroutine section_for: called CC=3 SCAR=11.0
Pod documented
57 262 my ( $self, $name ) = @_;
58 262 foreach my $section ( @{ $self->{sections} } ) {
59 2438 ▶ return $section if any { $_ eq $name } @{ $section->{functions} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $name; } , @{$$section{"functions"};}) 217 2221 2438
60 }
61 45 return;
62 }
63  
64 # The names of all the subs that the headings name, which L<Pod::Coverage> counts as documented.
65 # @returns the names, as a list
66 # @example my @documented = $doc->identifiers;
67 16 ▶sub identifiers {
Subroutine identifiers: called CC=1 SCAR=0.0
Pod documented
68 16 my ($self) = @_;
69 16 return map { @{ $_->{functions} } } @{ $self->{sections} };
70 }
71  
72 47 ▶sub _parser {
Subroutine _parser: called CC=1 SCAR=0.0
73 47 my $parser = Pod::Simple::SimpleTree->new;
74 47 $parser->accept_directive_as_processed(qw{method func attr});
75 47 return $parser;
76 }
77  
78 # An argument list in parentheses, captured without them.
79 15 our $ARGLIST = qr/\(([^()]*)\)/;
80  
81 # A type word or a parameterized type expression, such as Str, Types::Standard::Int or ArrayRef[HashRef[Str]].
82 15 our $TYPE_NAME = qr/[[:alpha:]_]\w*(?:::\w+)*/;
83 15 our $TYPE_PARAMS = qr/(?<params>\[(?:[^\[\]]++|(?&params))*\])/;
84  
85 # The words of a paragraph that says how a sub terminates.
86 15 our @TERMINATION_WORDS = qw{die dies died croak croaks croaked confess confesses confessed throw throws exception exceptions exit exits exited};
87 15 our $TERMINATION = do { my $words = join q{|}, @TERMINATION_WORDS; qr/\b(?:$words|terminat\w*)\b/i };
88  
89 # A class name, maybe quoted, as prose names the class of a returned object.
90 15 our $CLASS_NAME = qr/['"]?([\w:]+)['"]?/;
91  
92 # A class name that starts with a capital, followed by "object" or "instance", as in "a new Foo object".
93 15 our $CLASS_OBJECT = qr/(?-i:(?=['"]?[[:upper:]]))$CLASS_NAME\s+(?:object|instance)\b/i;
94  
95 15 our %LEVEL = ( head1 => 1, head2 => 2, head3 => 3, head4 => 4, method => 2, func => 2, attr => 2 );
96  
97 47 ▶sub _read {
Subroutine _read: called CC=11 SCAR=24.0
98 47 my ( $self, $root ) = @_;
99 47 my ( undef, undef, @nodes ) = @$root;
100 47 my $current;
101 47 foreach my $node (@nodes) {
102 651 ▶ next if !ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 651 651
103 651 my $type = $node->[0];
104 651 my $level = $LEVEL{$type};
105 651 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 267 384 651
106 267 my $text = _flatten($node);
107 267 ▶ my $section = $level > 1 ? $self->_heading( $text, $node->[1]{start_line} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 237 30 267
108 267 ▶ if ( $section && ( !$self->{subs} || any { $self->{subs}{$_} } @{ $section->{functions} } ) ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($section and !$self->{"subs"} || &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})) 231 36 267
Condition100%6/6
$section and !$self->{"subs"} || &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
!ll&&!rl&&r
306231
not $self->{"subs"} or &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
l!l&&r!l&&!r
202116
MC/DC100%3/3
$section and !$self->{"subs"} || &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
$section not $self->{"subs"} &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
Truth table100%4/4
A and not(B) or C
A= $sectionB= not $self->{"subs"}C= &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
ABCresult
0XX0
1000
11X1
1011
109 231 $section->{level} = $level;
110 231 push @{ $self->{sections} }, $section;
111 231 $current = $section;
112 231 next;
113 }
114 36 ▶ undef $current if !$current || $level <= $current->{level};
Branch: 1 branch
BranchTrueFalseTotal
if not $current or $level <= $current->{"level"} 31 5 36
Condition100%3/3
not $current or $level <= $current->{"level"}
l!l&&r!l&&!r
3015
MC/DC100%2/2
not $current or $level <= $current->{"level"}
not $current $level <= $current->{"level"}
Truth table100%3/3
not(A) or B
A= not $currentB= $level <= $current->{"level"}
ABresult
1X1
011
000
115 36 next;
116 }
117 384 ▶ $self->_content( $current, $node ) if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 372 12 384
118 }
119 47 return $self;
120 }
121  
122 # _flatten and _content walk the tree of the POD, which is as deep as its nesting, and so recursion is their natural form.
123 ## no critic (Subroutines::ProhibitTailRecursion)
124  
125 # The text of a node, with its formatting codes and their markup left out.
126 1428 ▶sub _flatten {
Subroutine _flatten: called CC=3 SCAR=11.0
127 1428 my ($node) = @_;
128 1428 ▶ return $node if !ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 718 710 1428
129 710 my ( undef, undef, @kids ) = @$node;
130 710 my $text = q{};
131 710 foreach my $kid (@kids) {
132 726 $text .= _flatten($kid);
133 }
134 710 return $text;
135 }
136  
137 237 ▶sub _heading {
Subroutine _heading: called CC=4 SCAR=13.9
138 237 my ( $self, $text, $line ) = @_;
139 237 $text =~ s/[;#]//g;
140 237 $text =~ s/\$\w+->//g;
141 237 $text =~ s/\w+(?:::\w+)*::(?=\w)//g;
142  
143 237 my %args;
144 237 ▶ while ( $text =~ m/(\w+)\s*$ARGLIST/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\w+)\s*$Pod::Coverage::More::Documentation::ARGLIST/aagsx) 184 237 421
145 184 my ( $function, $list ) = ( $1, $2 );
146 184 $args{$function} = [ grep { $_ ne q{} } map { s/\A\s+|\s+\z//gr } split m/,/, $list ];
147 }
148 237 ( my $names = $text ) =~ s/\([^()]*\)//g;
149 237 ▶ my @functions = index( $names, q{,} ) >= 0 ? split( m/\s*,\s*/, $names ) : split( m/\s+/, $names );
Branch: 1 branch
BranchTrueFalseTotal
index($names, ',') != -1 ? : 14 223 237
150 237 @functions = grep { m/\A\w+\z/ } map { s/\A\s+|\s+\z//gr } @functions;
151 237 ▶ return if !@functions;
Branch: 1 branch
BranchTrueFalseTotal
unless @functions 0 237 237
152  
153 return {
154 237 heading => $text =~ s/\A\s+|\s+\z//gr,
155 line => $line,
156 functions => \@functions,
157 args => \%args,
158 types => {},
159 returns => [],
160 terminations => [],
161 verbatim => [],
162 };
163 }
164  
165 551 ▶sub _content {
Subroutine _content: called CC=7 SCAR=19.5
166 551 my ( $self, $section, $node ) = @_;
167 551 my $type = $node->[0];
168 551 my $line = $node->[1]{start_line};
169  
170 551 ▶ if ( $type =~ m/\Aover-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\Aover-/aasx) 116 435 551
171 116 my ( undef, undef, @kids ) = @$node;
172 116 foreach my $kid ( grep { ref } @kids ) {
173 179 $self->_content( $section, $kid );
174 }
175 116 return;
176 }
177 435 my $text = _flatten($node);
178 435 ▶ if ( $type eq 'Verbatim' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Verbatim") 68 367 435
179 68 push @{ $section->{verbatim} }, $text;
180 68 return;
181 }
182 367 ▶ if ( $type =~ m/\Aitem-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\Aitem-/aasx) { } 179 188 367
elsif ($text =~ /\A\s*returns?\b\s*(.*)/aaisx) { } 117 71 188
183 179 $self->_item( $section, $text, $line );
184 }
185 elsif ( $text =~ m/\A\s*returns?\b\s*(.*)/i ) {
186 117 push @{ $section->{returns} }, { %{ _returned($1) }, text => $text, line => $line };
187 }
188 367 ▶ push @{ $section->{terminations} }, $text if $text =~ $TERMINATION;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$Pod::Coverage::More::Documentation::TERMINATION/aasx 7 360 367
189 367 return;
190 }
191  
192 ## use critic
193  
194 179 ▶sub _item {
Subroutine _item: called CC=12 SCAR=25.0
195 179 my ( $self, $section, $text, $line ) = @_;
196 179 my ( $name, $rest ) = $text =~ m/\A\s*(\S+?)[:,]?(?:\s+(.*))?\z/;
197 179 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 179 179
198 179 ▶ $rest //= q{};
Condition50%1/2
$rest //= ""
l!l
1790
MC/DC0%0/1
$rest //= ""
$rest
Truth table50%1/2
A
A= $rest
Aresult
11
00
199  
200 179 ▶ if ( lc $name eq 'returns' || lc $name eq 'return' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "returns" or lc $name eq "return") 1 178 179
Condition66%2/3
lc $name eq "returns" or lc $name eq "return"
l!l&&r!l&&!r
10178
MC/DC50%1/2
lc $name eq "returns" or lc $name eq "return"
lc $name eq "returns" lc $name eq "return"
Truth table66%2/3
A or B
A= lc $name eq "returns"B= lc $name eq "return"
ABresult
1X1
011
000
201 1 $rest =~ s/\A[-:\s]+//;
202 1 push @{ $section->{returns} }, { %{ _returned($rest) }, text => $text, line => $line };
203 1 return;
204 }
205  
206 178 my $word = q{};
207 178 ▶ if ( $rest =~ m/\A\((.*?)\)/ ) { $word = $1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A\((.*?)\)/aasx) { } 5 173 178
elsif ($rest =~ /\A[-:\s]*(.*?)\s*(?:\s-\s|:\s|\z)/aasx) { } 173 0 173
208 173 elsif ( $rest =~ m/\A[-:\s]*(.*?)\s*(?:\s-\s|:\s|\z)/ ) { $word = $1 }
209 178 $word =~ s/\A\s+|\s+\z//g;
210 178 my $type = type_named($word);
211 178 ▶ my $documented = { word => $type ? lc $word : q{}, type => $type, sigil => $name =~ m/\A([@%])/ ? $1 : undef };
Branch: 2 branches
BranchTrueFalseTotal
$type ? : 171 7 178
$name =~ /\A([\@%])/aasx ? : 11 167 178
212  
213 178 my $bare = _bare($name);
214 178 foreach my $function ( @{ $section->{functions} } ) {
215 190 ▶ my @args = map { _bare($_) } @{ $section->{args}{$function} || [] };
Condition50%1/2
$section->{'args'}{$function} || []
l!l
1900
MC/DC0%0/1
$section->{'args'}{$function} || []
$section->{'args'}{$function}
Truth table50%1/2
A
A= $section->{'args'}{$function}
Aresult
11
00
216 190 ▶ next if !any { $_ eq $bare } @args;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $bare; } , @args) 12 178 190
217 178 $section->{types}{$function}{$bare} = $documented;
218 }
219 178 return;
220 }
221  
222 # C<name> without its sigils and backslashes, in lower case, as arguments in the POD and parameters in the code are compared.
223 # @param name Str an argument or a parameter, such as C<\%Options>
224 # @returns Str the bare name
225 # @example my $bare = Pod::Coverage::More::Documentation->bare_name('\%Options');
226 888 ▶sub bare_name {
Subroutine bare_name: called CC=1 SCAR=0.0
Pod documented
227 888 my ( $class, $name ) = @_;
228 888 return _bare($name);
229 }
230  
231 1608 ▶sub _bare {
Subroutine _bare: called CC=1 SCAR=0.0
232 1608 my ($name) = @_;
233 1608 $name =~ tr/$*\\@%&|//d;
234 1608 return lc $name;
235 }
236  
237 15 our %LITERAL_TYPES = ( undef => Undef, true => Bool, false => Bool, boolean => Bool, 'a boolean' => Bool );
238  
239 # The types that the words after "Returns" name, and whether they say that it returns nothing.
240 118 ▶sub _returned {
Subroutine _returned: called CC=5 SCAR=16.1
241 118 my ($rest) = @_;
242 118 my @types;
243 118 while (1) {
244 125 $rest =~ s/\A\s*(?:an?|the)\s+(?=\S)//i;
245 125 ▶ return { types => [], nothing => 1 } if !@types && $rest =~ m/\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if not @types and $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx 18 107 125
Condition100%3/3
not @types and $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx
!ll&&!rl&&r
710018
MC/DC100%2/2
not @types and $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx
not @types $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx
Truth table100%3/3
not(A) and B
A= not @typesB= $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx
ABresult
0X0
100
111
246 107 ▶ my $type = _type_at_start( \$rest ) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $type = _type_at_start(\$rest) or last 89 18 107
247 89 push @types, $type;
248 89 ▶ last if $rest !~ s/\A\s*(?:,\s*)?(?:or\b|\|)\s*//i;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ s/\A\s*(?:,\s*)?(?:or\b|\|)\s*//aaisx 82 7 89
249 }
250 100 return { types => \@types, nothing => 0 };
251 }
252  
253 107 ▶sub _type_at_start {
Subroutine _type_at_start: called CC=8 SCAR=20.8
254 107 my ($rest) = @_;
255 107 ▶ if ( $$rest =~ s/\A\s*(?:an?\s+)?instance\s+of\s+(?:class\s+)?$CLASS_NAME//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rest =~ s/\A\s*(?:an?\s+)?instance\s+of\s+(?:class\s+)?$Pod::Coverage::More::Documentation::CLASS_NAME//aaisx) 3 104 107
256 3 return InstanceOf [$1];
257 }
258 104 ▶ if ( $$rest =~ s/\A\s*(?:new\s+)?$CLASS_OBJECT//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rest =~ s/\A\s*(?:new\s+)?$Pod::Coverage::More::Documentation::CLASS_OBJECT//aaisx) 8 96 104
259 8 return InstanceOf [$1];
260 }
261 96 my $literals = join q{|}, map { s/[ ]/\\s+/gr } sort keys %LITERAL_TYPES;
262 96 ▶ if ( $$rest =~ s/\A\s*($literals)\b//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rest =~ s/\A\s*($literals)\b//aaisx) 11 85 96
263 11 return $LITERAL_TYPES{ lc( $1 =~ s/\s+/ /gr ) };
264 }
265 85 ▶ return Int if $$rest =~ s/\A\s*-?\d+(?!\.\d)\b//;
Branch: 1 branch
BranchTrueFalseTotal
if $$rest =~ s/\A\s*-?\d+(?!\.\d)\b//aasx 2 83 85
266 83 ▶ return Str if $$rest =~ s/\A\s*(?:'[^']*'|"[^"]*")//;
Branch: 1 branch
BranchTrueFalseTotal
if $$rest =~ s/\A\s*(?:'[^']*'|"[^"]*")//aasx 0 83 83
267  
268 83 my ($word) = $$rest =~ m/\A\s*($TYPE_NAME $TYPE_PARAMS?)/;
269 83 ▶ return if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 83 83
270 83 ▶ my $type = type_named($word) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = type_named($word) or return 65 18 83
271 65 $$rest =~ s/\A\s*\Q$word\E//;
272 65 return $type;
273 }
274  
275 15 1;
276  
277 __END__
« lib/Pod/Coverage/More/Analyzer/Perl5_38.pm ↑ Summary lib/Pod/Coverage/More/Declared/FunctionParameters.pm »