Pod/Coverage/More/Documentation.pm

filter: Statementstmt 100% 183 / 183 Branchbran 90.9% 60 / 66 Conditioncond 84.2% 16 / 19 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 26 / 26 Podpod 100% 7 / 7 totaltotal 96.1% 300 / 312 CC 46 SCAR 18.2
CC
46
Cov
97%
_item
11.2
_read
11.0
_type_at_start
8.0
CRAP 6.2
« Pod/Coverage/More/Analyzer/Perl5_38.pm ↑ Summary 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.001;
5  
6 11 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use List::Util qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Types::Standard qw{Bool InstanceOf Int Str Undef};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶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 36 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 36 my ( $class, %args ) = @_;
23 36 ▶ return bless( { subs => $args{subs} ? { map { $_ => 1 } @{ $args{subs} } } : undef, sections => [] }, $class );
Branch: 1 branch
BranchTrueFalseTotal
$args{'subs'} ? : 21 15 36
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 20 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
31 20 my ( $self, $file ) = @_;
32 20 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 16 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
40 16 my ( $self, $pod ) = @_;
41 16 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 200 ▶sub section_for {
Subroutine section_for: called CC=3 SCAR=11.0
Pod documented
57 200 my ( $self, $name ) = @_;
58 200 foreach my $section ( @{ $self->{sections} } ) {
59 1318 ▶ 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"};}) 168 1150 1318
60 }
61 32 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 11 ▶sub identifiers {
Subroutine identifiers: called CC=1 SCAR=0.0
Pod documented
68 11 my ($self) = @_;
69 11 return map { @{ $_->{functions} } } @{ $self->{sections} };
70 }
71  
72 36 ▶sub _parser {
Subroutine _parser: called CC=1 SCAR=0.0
73 36 my $parser = Pod::Simple::SimpleTree->new;
74 36 $parser->accept_directive_as_processed(qw{method func attr});
75 36 return $parser;
76 }
77  
78 # An argument list in parentheses, captured without them.
79 11 our $ARGLIST = qr/\(([^()]*)\)/;
80  
81 # A type word or a parameterized type expression, such as Str, Types::Standard::Int or ArrayRef[HashRef[Str]].
82 11 our $TYPE_NAME = qr/[[:alpha:]_]\w*(?:::\w+)*/;
83 11 our $TYPE_PARAMS = qr/(?<params>\[(?:[^\[\]]++|(?&params))*\])/;
84  
85 # The words of a paragraph that says how a sub terminates.
86 11 our @TERMINATION_WORDS = qw{die dies died croak croaks croaked confess confesses confessed throw throws exception exceptions exit exits exited};
87 11 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 11 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 11 our $CLASS_OBJECT = qr/(?-i:(?=['"]?[[:upper:]]))$CLASS_NAME\s+(?:object|instance)\b/i;
94  
95 11 our %LEVEL = ( head1 => 1, head2 => 2, head3 => 3, head4 => 4, method => 2, func => 2, attr => 2 );
96  
97 36 ▶sub _read {
Subroutine _read: called CC=11 SCAR=24.0
98 36 my ( $self, $root ) = @_;
99 36 my ( undef, undef, @nodes ) = @$root;
100 36 my $current;
101 36 foreach my $node (@nodes) {
102 460 ▶ next if !ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 460 460
103 460 my $type = $node->[0];
104 460 my $level = $LEVEL{$type};
105 460 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 210 250 460
106 210 my $text = _flatten($node);
107 210 ▶ my $section = $level > 1 ? $self->_heading( $text, $node->[1]{start_line} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 186 24 210
108 210 ▶ 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"};})) 180 30 210
Condition100%6/6
$section and !$self->{"subs"} || &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
!ll&&!rl&&r
246180
not $self->{"subs"} or &any(sub { $self->{"subs"}{$_}; } , @{$$section{"functions"};})
l!l&&r!l&&!r
191616
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 180 $section->{level} = $level;
110 180 push @{ $self->{sections} }, $section;
111 180 $current = $section;
112 180 next;
113 }
114 30 ▶ undef $current if !$current || $level <= $current->{level};
Branch: 1 branch
BranchTrueFalseTotal
if not $current or $level <= $current->{"level"} 25 5 30
Condition100%3/3
not $current or $level <= $current->{"level"}
l!l&&r!l&&!r
2415
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 30 next;
116 }
117 250 ▶ $self->_content( $current, $node ) if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 240 10 250
118 }
119 36 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 1020 ▶sub _flatten {
Subroutine _flatten: called CC=3 SCAR=11.0
127 1020 my ($node) = @_;
128 1020 ▶ return $node if !ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 514 506 1020
129 506 my ( undef, undef, @kids ) = @$node;
130 506 my $text = q{};
131 506 foreach my $kid (@kids) {
132 522 $text .= _flatten($kid);
133 }
134 506 return $text;
135 }
136  
137 186 ▶sub _heading {
Subroutine _heading: called CC=4 SCAR=13.9
138 186 my ( $self, $text, $line ) = @_;
139 186 $text =~ s/[;#]//g;
140 186 $text =~ s/\$\w+->//g;
141 186 $text =~ s/\w+(?:::\w+)*::(?=\w)//g;
142  
143 186 my %args;
144 186 ▶ while ( $text =~ m/(\w+)\s*$ARGLIST/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\w+)\s*$Pod::Coverage::More::Documentation::ARGLIST/aagsx) 134 186 320
145 134 my ( $function, $list ) = ( $1, $2 );
146 134 $args{$function} = [ grep { $_ ne q{} } map { s/\A\s+|\s+\z//gr } split m/,/, $list ];
147 }
148 186 ( my $names = $text ) =~ s/\([^()]*\)//g;
149 186 ▶ my @functions = index( $names, q{,} ) >= 0 ? split( m/\s*,\s*/, $names ) : split( m/\s+/, $names );
Branch: 1 branch
BranchTrueFalseTotal
index($names, ',') != -1 ? : 14 172 186
150 186 @functions = grep { m/\A\w+\z/ } map { s/\A\s+|\s+\z//gr } @functions;
151 186 ▶ return if !@functions;
Branch: 1 branch
BranchTrueFalseTotal
unless @functions 0 186 186
152  
153 return {
154 186 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 358 ▶sub _content {
Subroutine _content: called CC=7 SCAR=19.5
166 358 my ( $self, $section, $node ) = @_;
167 358 my $type = $node->[0];
168 358 my $line = $node->[1]{start_line};
169  
170 358 ▶ if ( $type =~ m/\Aover-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\Aover-/aasx) 70 288 358
171 70 my ( undef, undef, @kids ) = @$node;
172 70 foreach my $kid ( grep { ref } @kids ) {
173 118 $self->_content( $section, $kid );
174 }
175 70 return;
176 }
177 288 my $text = _flatten($node);
178 288 ▶ if ( $type eq 'Verbatim' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Verbatim") 18 270 288
179 18 push @{ $section->{verbatim} }, $text;
180 18 return;
181 }
182 270 ▶ if ( $type =~ m/\Aitem-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\Aitem-/aasx) { } 118 152 270
elsif ($text =~ /\A\s*returns?\b\s*(.*)/aaisx) { } 84 68 152
183 118 $self->_item( $section, $text, $line );
184 }
185 elsif ( $text =~ m/\A\s*returns?\b\s*(.*)/i ) {
186 84 push @{ $section->{returns} }, { %{ _returned($1) }, text => $text, line => $line };
187 }
188 270 ▶ push @{ $section->{terminations} }, $text if $text =~ $TERMINATION;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$Pod::Coverage::More::Documentation::TERMINATION/aasx 6 264 270
189 270 return;
190 }
191  
192 ## use critic
193  
194 118 ▶sub _item {
Subroutine _item: called CC=11 SCAR=24.1
195 118 my ( $self, $section, $text, $line ) = @_;
196 118 my ( $name, $rest ) = $text =~ m/\A\s*(\S+?)[:,]?(?:\s+(.*))?\z/;
197 118 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 118 118
198 118 ▶ $rest //= q{};
Condition50%1/2
$rest //= ""
l!l
1180
MC/DC0%0/1
$rest //= ""
$rest
Truth table50%1/2
A
A= $rest
Aresult
11
00
199  
200 118 ▶ if ( lc $name eq 'returns' || lc $name eq 'return' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "returns" or lc $name eq "return") 1 117 118
Condition66%2/3
lc $name eq "returns" or lc $name eq "return"
l!l&&r!l&&!r
10117
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 117 my $word = q{};
207 117 ▶ if ( $rest =~ m/\A\((.*?)\)/ ) { $word = $1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A\((.*?)\)/aasx) { } 5 112 117
elsif ($rest =~ /\A[-:\s]*(.*?)\s*(?:\s-\s|:\s|\z)/aasx) { } 112 0 112
208 112 elsif ( $rest =~ m/\A[-:\s]*(.*?)\s*(?:\s-\s|:\s|\z)/ ) { $word = $1 }
209 117 $word =~ s/\A\s+|\s+\z//g;
210 117 my $type = type_named($word);
211 117 ▶ my $documented = { word => $type ? lc $word : q{}, type => $type };
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 110 7 117
212  
213 117 my $bare = _bare($name);
214 117 foreach my $function ( @{ $section->{functions} } ) {
215 129 ▶ my @args = map { _bare($_) } @{ $section->{args}{$function} || [] };
Condition50%1/2
$section->{'args'}{$function} || []
l!l
1290
MC/DC0%0/1
$section->{'args'}{$function} || []
$section->{'args'}{$function}
Truth table50%1/2
A
A= $section->{'args'}{$function}
Aresult
11
00
216 129 ▶ 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 117 129
217 117 $section->{types}{$function}{$bare} = $documented;
218 }
219 117 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 462 ▶sub bare_name {
Subroutine bare_name: called CC=1 SCAR=0.0
Pod documented
227 462 my ( $class, $name ) = @_;
228 462 return _bare($name);
229 }
230  
231 1028 ▶sub _bare {
Subroutine _bare: called CC=1 SCAR=0.0
232 1028 my ($name) = @_;
233 1028 $name =~ tr/$*\\@%&|//d;
234 1028 return lc $name;
235 }
236  
237 11 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 85 ▶sub _returned {
Subroutine _returned: called CC=5 SCAR=16.1
241 85 my ($rest) = @_;
242 85 my @types;
243 85 while (1) {
244 92 $rest =~ s/\A\s*(?:an?|the)\s+(?=\S)//i;
245 92 ▶ 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 6 86 92
Condition100%3/3
not @types and $rest =~ /\A\s*(?:nothing|no\s+value|an?\s+empty\s+list)\b/aaisx
!ll&&!rl&&r
7796
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 86 ▶ my $type = _type_at_start( \$rest ) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $type = _type_at_start(\$rest) or last 77 9 86
247 77 push @types, $type;
248 77 ▶ 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 70 7 77
249 }
250 79 return { types => \@types, nothing => 0 };
251 }
252  
253 86 ▶sub _type_at_start {
Subroutine _type_at_start: called CC=8 SCAR=20.8
254 86 my ($rest) = @_;
255 86 ▶ 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 83 86
256 3 return InstanceOf [$1];
257 }
258 83 ▶ 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 75 83
259 8 return InstanceOf [$1];
260 }
261 75 my $literals = join q{|}, map { s/[ ]/\\s+/gr } sort keys %LITERAL_TYPES;
262 75 ▶ if ( $$rest =~ s/\A\s*($literals)\b//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rest =~ s/\A\s*($literals)\b//aaisx) 11 64 75
263 11 return $LITERAL_TYPES{ lc( $1 =~ s/\s+/ /gr ) };
264 }
265 64 ▶ return Int if $$rest =~ s/\A\s*-?\d+(?!\.\d)\b//;
Branch: 1 branch
BranchTrueFalseTotal
if $$rest =~ s/\A\s*-?\d+(?!\.\d)\b//aasx 2 62 64
266 62 ▶ return Str if $$rest =~ s/\A\s*(?:'[^']*'|"[^"]*")//;
Branch: 1 branch
BranchTrueFalseTotal
if $$rest =~ s/\A\s*(?:'[^']*'|"[^"]*")//aasx 0 62 62
267  
268 62 my ($word) = $$rest =~ m/\A\s*($TYPE_NAME $TYPE_PARAMS?)/;
269 62 ▶ return if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 62 62
270 62 ▶ my $type = type_named($word) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = type_named($word) or return 53 9 62
271 53 $$rest =~ s/\A\s*\Q$word\E//;
272 53 return $type;
273 }
274  
275 11 1;
276  
277 __END__
« Pod/Coverage/More/Analyzer/Perl5_38.pm ↑ Summary Pod/Coverage/More/Declared/FunctionParameters.pm »