blib/lib/Pod/Coverage/More/Generator.pm

filter: Statementstmt 99.2% 136 / 137 Branchbran 80.0% 48 / 60 Conditioncond 64.1% 34 / 53 MC/DCmcdc 33.3% 10 / 30 Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 82.8% 251 / 303 CC 65 SCAR 23.1
CC
65
Cov
87%
_arguments
21.4
_section
11.1
_parent_has_blurb
9.3
CRAP 10.0
« lib/Test/Pod/Coverage/More.pm ↑ Summary lib/Pod/Coverage/More.pm »
1 # ABSTRACT: Write the POD of each sub from its blurb and the analysis of its code
2 # PODNAME: Pod::Coverage::More::Generator
3  
4 package Pod::Coverage::More::Generator 0.003;
5  
6 5 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Module::Runtime qw{module_notional_filename};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Pod::Coverage::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Pod::Coverage::More::Blurb;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Pod::Coverage::More::Documentation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 # A generator for the package C<package>, which must be loaded.
19 # @param package Str the name of the package
20 # @param file Str optional: the file to read the blurbs from, by default the file that the package was loaded from
21 # @returns Pod::Coverage::More::Generator the generator
22 # @example my $generator = Pod::Coverage::More::Generator->new( package => 'Some::Module', file => 'lib/Some/Module.pm' );
23 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
24 6 my ( $class, %args ) = @_;
25 6 my $package = $args{package};
26 6 ▶ my $file = $args{file} // $INC{ module_notional_filename($package) };
Condition66%2/3
$args{"file"} // $INC{&module_notional_filename($package)}
l!l&&r!l&&!r
330
MC/DC0%0/2
$args{"file"} // $INC{&module_notional_filename($package)}
$args{"file"} $INC{&module_notional_filename($package)}
Truth table66%2/3
A // B
A= $args{"file"}B= $INC{&module_notional_filename($package)}
ABresult
1X1
011
000
27 6 return bless( { package => $package, file => $file, coverage => Pod::Coverage::More->new( package => $package ) }, $class );
28 }
29  
30 # One section per sub that gets one, in the order of the names of the subs:
31 # a HASHREF with C<name>, the name of the sub, and C<pod>, the text of its section, which starts with a C<=head2>.
32 # @returns the sections, as a list
33 # @example my @sections = $generator->sections;
34 9 ▶sub sections {
Subroutine sections: called CC=6 SCAR=18.0
Pod documented
35 9 my ($self) = @_;
36 9 my $coverage = $self->{coverage};
37 9 ▶ my $blurbs = defined $self->{file} ? Pod::Coverage::More::Blurb->read_file( $self->{file} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 9 0 9
38 9 my $analysis = $coverage->analysis;
39 9 my @sections;
40 9 foreach my $name ( sort $coverage->public_subs ) {
41 52 ▶ my $blurb = $blurbs->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $blurb = $blurbs->{$name} or next 40 12 52
42 40 ▶ next if $coverage->section_for($name) || $self->_parent_has_blurb($name);
Branch: 1 branch
BranchTrueFalseTotal
if $coverage->section_for($name) or $self->_parent_has_blurb($name) 1 39 40
Condition66%2/3
$coverage->section_for($name) or $self->_parent_has_blurb($name)
l!l&&r!l&&!r
0139
MC/DC50%1/2
$coverage->section_for($name) or $self->_parent_has_blurb($name)
$coverage->section_for($name) $self->_parent_has_blurb($name)
Truth table66%2/3
A or B
A= $coverage->section_for($name)B= $self->_parent_has_blurb($name)
ABresult
1X1
011
000
43 39 push @sections, { name => $name, pod => _section( $name, $blurb, $analysis->{$name} ) };
44 }
45 9 return @sections;
46 }
47  
48 # Whether a parent class has a blurb for the sub NAME, which becomes its section when the parent is woven.
49 # Only a parent from the tree of the package counts: it is woven with the package. A parent from elsewhere, as an
50 # installed one, either was woven already, so that its POD documents the sub, or never was, so that its comment is none.
51 40 ▶sub _parent_has_blurb {
Subroutine _parent_has_blurb: called CC=9 SCAR=22.3
52 40 my ( $self, $name ) = @_;
53 40 ▶ my $tree = _tree_of( $self->{package} ) // return 0;
Condition50%1/2
_tree_of($self->{"package"}) // (return 0)
l!l
400
MC/DC0%0/1
_tree_of($self->{"package"}) // (return 0)
_tree_of($self->{"package"})
Truth table50%1/2
A
A= _tree_of($self->{"package"})
Aresult
11
00
54 40 foreach my $parent ( @{ mro::get_linear_isa( $self->{package} ) } ) {
55 43 ▶ next if $parent eq $self->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $self->{"package"} 40 3 43
56 3 ▶ my $file = $INC{ module_notional_filename($parent) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $INC{&module_notional_filename($parent)} or next 3 0 3
57 3 ▶ next if ( _tree_of($parent) // q{} ) ne $tree;
Branch: 1 branch
BranchTrueFalseTotal
if (_tree_of($parent) // "") ne $tree 1 2 3
Condition50%1/2
_tree_of($parent) // ""
l!l
30
MC/DC0%0/1
_tree_of($parent) // ""
_tree_of($parent)
Truth table50%1/2
A
A= _tree_of($parent)
Aresult
11
00
58 2 ▶ $self->{parent_blurbs}{$parent} //= Pod::Coverage::More::Blurb->read_file($file);
Condition66%2/3
$self->{"parent_blurbs"}{$parent} //= "Pod::Coverage::More::Blurb"->read_file($file)
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"parent_blurbs"}{$parent} //= "Pod::Coverage::More::Blurb"->read_file($file)
$self->{"parent_blurbs"}{$parent} "Pod::Coverage::More::Blurb"->read_file($file)
Truth table66%2/3
A //= B
A= $self->{"parent_blurbs"}{$parent}B= "Pod::Coverage::More::Blurb"->read_file($file)
ABresult
1X1
011
000
59 2 ▶ return 1 if $self->{parent_blurbs}{$parent}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent_blurbs"}{$parent}{$name} 1 1 2
60 }
61 39 return 0;
62 }
63  
64 # The directory on @INC that PACKAGE was loaded from, or nothing when it was not loaded from a file.
65 43 ▶sub _tree_of {
Subroutine _tree_of: called CC=2 SCAR=7.0
66 43 my ($package) = @_;
67 43 my $notional = module_notional_filename($package);
68 43 ▶ my $file = $INC{$notional} // return;
Condition50%1/2
$INC{$notional} // (return)
l!l
430
MC/DC0%0/1
$INC{$notional} // (return)
$INC{$notional}
Truth table50%1/2
A
A= $INC{$notional}
Aresult
11
00
69 43 return $file =~ s{/?\Q$notional\E\z}{}r;
70 }
71  
72 # Whether the text of a @throws is a sentence already, as "croaks when", because it starts with a word of termination.
73 # The noun "exception" does not count: "with a Some::Exception" still needs "Dies".
74 12 ▶sub _says_how_it_dies {
Subroutine _says_how_it_dies: called CC=2 SCAR=8.1
75 12 my ($throws) = @_;
76 12 ▶ return $throws =~ m/\A$Pod::Coverage::More::Documentation::TERMINATION/ && $throws !~ m/\Aexceptions?\b/i;
Condition33%1/3
$throws =~ /\A$Pod::Coverage::More::Documentation::TERMINATION/aasx && !($throws =~ /\Aexceptions?\b/aaisx)
!ll&&!rl&&r
1200
MC/DC0%0/2
$throws =~ /\A$Pod::Coverage::More::Documentation::TERMINATION/aasx && !($throws =~ /\Aexceptions?\b/aaisx)
$throws =~ /\A$Pod::Coverage::More::Documentation::TERMINATION/aasx !($throws =~ /\Aexceptions?\b/aaisx)
Truth table33%1/3
A && not(B)
A= $throws =~ /\A$Pod::Coverage::More::Documentation::TERMINATION/aasxB= !($throws =~ /\Aexceptions?\b/aaisx)
ABresult
0X0
100
111
77 }
78  
79 # The text of all the sections, one after another.
80 # @returns Str the POD, or the empty string when no sub gets a section
81 # @example my $pod = $generator->pod;
82 5 ▶sub pod {
Subroutine pod: called CC=1 SCAR=0.0
Pod documented
83 5 my ($self) = @_;
84 5 return join q{}, map { $_->{pod} } $self->sections;
85 }
86  
87 # The POD of one sub, from its BLURB and the analysis REC of its code.
88 39 ▶sub _section {
Subroutine _section: called CC=11 SCAR=24.0
89 39 my ( $name, $blurb, $rec ) = @_;
90 39 my @args = _arguments( $blurb, $rec );
91 39 ▶ my $pod = "=head2 $name" . ( @args ? '(' . join( ', ', map { $_->{name} } @args ) . ')' : q{} ) . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 33 6 39
92 39 foreach my $paragraph ( @{ $blurb->{description} } ) {
93 39 $pod .= "$paragraph\n\n";
94 }
95  
96 39 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 33 6 39
97 33 $pod .= "=over 4\n\n";
98 33 foreach my $arg (@args) {
99 45 ▶ my @parts = ( "$arg->{sigil}$arg->{name}", $arg->{type} ? $arg->{type}->display_name : (), $arg->{description} ne q{} ? $arg->{description} : () );
Branch: 2 branches
BranchTrueFalseTotal
$arg->{'type'} ? : 45 0 45
$arg->{'description'} ne '' ? : 45 0 45
100 45 $pod .= '=item ' . join( ' - ', @parts ) . "\n\n";
101 }
102 33 $pod .= "=back\n\n";
103 }
104  
105 39 my $returns = _returns( $blurb, $rec );
106 39 ▶ $pod .= "$returns\n\n" if defined $returns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $returns 32 7 39
107 39 foreach my $throws ( @{ $blurb->{throws} } ) {
108 12 ▶ $pod .= _sentence( _says_how_it_dies($throws) ? $throws : "Dies $throws" ) . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
_says_how_it_dies($throws) ? : 0 12 12
109 }
110 39 foreach my $example ( @{ $blurb->{examples} } ) {
111 39 $pod .= join( "\n", map { " $_" } @$example ) . "\n\n";
112 }
113 39 return $pod;
114 }
115  
116 # The arguments of the heading: the parameters, or the keys of a hash of named arguments, with what the blurb and the code say of each.
117 39 ▶sub _arguments {
Subroutine _arguments: called CC=18 SCAR=30.6
118 39 my ( $blurb, $rec ) = @_;
119 39 my %documented;
120 39 foreach my $param ( @{ $blurb->{params} } ) {
121 45 $documented{ Pod::Coverage::More::Documentation->bare_name( $param->{name} ) } = $param;
122 }
123 39 my @names;
124 39 ▶ foreach my $param ( @{ $rec->{params} || [] } ) {
Condition50%1/2
$rec->{'params'} || []
l!l
390
MC/DC0%0/1
$rec->{'params'} || []
$rec->{'params'}
Truth table50%1/2
A
A= $rec->{'params'}
Aresult
11
00
125 39 ▶ next if !defined $param->{name} || $param->{source} eq 'invocant' || $param->{name} =~ m/\A\$(?:self|class|proto)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $param->{"name"} or $param->{"source"} eq "invocant" or $param->{"name"} =~ /\A\$(?:self|class|proto)\z/aasx 7 32 39
Condition50%3/6
not defined $param->{"name"} or $param->{"source"} eq "invocant" or $param->{"name"} =~ /\A\$(?:self|class|proto)\z/aasx
l!l&&r!l&&!r
0732
not defined $param->{"name"} or $param->{"source"} eq "invocant"
l!l&&r!l&&!r
0039
MC/DC33%1/3
not defined $param->{"name"} or $param->{"source"} eq "invocant" or $param->{"name"} =~ /\A\$(?:self|class|proto)\z/aasx
not defined $param->{"name"} $param->{"source"} eq "invocant" $param->{"name"} =~ /\A\$(?:self|class|proto)\z/aasx
Truth table50%2/4
not(A) or B or C
A= not defined $param->{"name"}B= $param->{"source"} eq "invocant"C= $param->{"name"} =~ /\A\$(?:self|class|proto)\z/aasx
ABCresult
1XX1
01X1
0011
0000
126 32 ▶ if ( $param->{name} =~ m/\A%/ && $param->{keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"name"} =~ /\A%/aasx and $param->{"keys"}) 6 26 32
Condition66%2/3
$param->{"name"} =~ /\A%/aasx and $param->{"keys"}
!ll&&!rl&&r
2606
MC/DC50%1/2
$param->{"name"} =~ /\A%/aasx and $param->{"keys"}
$param->{"name"} =~ /\A%/aasx $param->{"keys"}
Truth table66%2/3
A and B
A= $param->{"name"} =~ /\A%/aasxB= $param->{"keys"}
ABresult
0X0
100
111
127 6 push @names, map { [ $_, undef ] } @{ $param->{keys} };
128 6 next;
129 }
130 26 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), $param ];
131 }
132 39 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 13 26 39
133 13 foreach my $param ( @{ $blurb->{params} } ) {
134 7 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), undef ];
135 }
136 }
137  
138 39 my @args;
139 39 foreach my $entry (@names) {
140 45 my ( $name, $param ) = @$entry;
141 45 ▶ my $documented = $documented{$name} // {};
Condition50%1/2
$documented{$name} // {}
l!l
450
MC/DC0%0/1
$documented{$name} // {}
$documented{$name}
Truth table50%1/2
A
A= $documented{$name}
Aresult
11
00
142 push @args, {
143 name => $name,
144 sigil => ( $documented->{name} // q{} ) =~ m/\A([@%])/ ? $1 : q{},
145 type => $documented->{type} // ( $param ? $param->{declared} : undef ),
146 45 ▶ description => $documented->{description} // q{},
Branch: 2 branches
BranchTrueFalseTotal
($documented->{'name'} // '') =~ /\A([\@%])/aasx ? : 6 39 45
$param ? : 0 0 0
Condition42%3/7
$documented->{"name"} // ""
l!l
450
$documented->{"type"} // ($param ? $param->{"declared"} : undef)
l!l&&r!l&&!r
4500
$documented->{"description"} // ""
l!l
450
MC/DC0%0/1
$documented->{"name"} // ""
$documented->{"name"}
MC/DC0%0/2
$documented->{"type"} // ($param ? $param->{"declared"} : undef)
$documented->{"type"} ($param ? $param->{"declared"} : undef)
MC/DC0%0/1
$documented->{"description"} // ""
$documented->{"description"}
Truth table50%1/2
A
A= $documented->{"name"}
Aresult
11
00
Truth table33%1/3
A // B
A= $documented->{"type"}B= ($param ? $param->{"declared"} : undef)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $documented->{"description"}
Aresult
11
00
147 };
148 }
149 39 return @args;
150 }
151  
152 # The paragraph of the return value, or undef when neither the blurb nor what the code shows for certain says anything.
153 39 ▶sub _returns {
Subroutine _returns: called CC=9 SCAR=22.0
154 39 my ( $blurb, $rec ) = @_;
155 39 ▶ my $stated = $blurb->{returns} // {};
Condition100%2/2
$blurb->{"returns"} // {}
l!l
1425
MC/DC100%1/1
$blurb->{"returns"} // {}
$blurb->{"returns"}
Truth table100%2/2
A
A= $blurb->{"returns"}
Aresult
11
00
156 39 ▶ my $description = $stated->{description} // q{};
Condition100%2/2
$stated->{"description"} // ""
l!l
1425
MC/DC100%1/1
$stated->{"description"} // ""
$stated->{"description"}
Truth table100%2/2
A
A= $stated->{"description"}
Aresult
11
00
157 39 ▶ my @types = $stated->{type} ? ( $stated->{type} ) : _code_return_types($rec);
Branch: 1 branch
BranchTrueFalseTotal
$stated->{'type'} ? : 7 32 39
158 39 ▶ return if !@types && $description eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not @types and $description eq "" 7 32 39
Condition100%3/3
not @types and $description eq ""
!ll&&!rl&&r
2577
MC/DC100%2/2
not @types and $description eq ""
not @types $description eq ""
Truth table100%3/3
not(A) and B
A= not @typesB= $description eq ""
ABresult
0X0
100
111
159  
160 32 my $text = 'Returns';
161 32 ▶ $text .= q{ } . join( q{ or }, _type_texts(@types) ) if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 25 7 32
162 32 ▶ $text .= ( @types ? q{, } : q{ } ) . $description if $description ne q{};
Branch: 2 branches
BranchTrueFalseTotal
if $description ne "" 14 18 32
@types ? : 7 7 14
163 32 return _sentence($text);
164 }
165  
166 # The return types that the code declares, or else those that its literals and objects show for certain.
167 32 ▶sub _code_return_types {
Subroutine _code_return_types: called CC=9 SCAR=22.3
168 32 my ($rec) = @_;
169 32 ▶ return if !$rec;
Branch: 1 branch
BranchTrueFalseTotal
unless $rec 0 32 32
170 32 ▶ my @declared = @{ $rec->{declared}{returns} || [] };
Condition50%1/2
$rec->{'declared'}{'returns'} || []
l!l
032
MC/DC0%0/1
$rec->{'declared'}{'returns'} || []
$rec->{'declared'}{'returns'}
Truth table50%1/2
A
A= $rec->{'declared'}{'returns'}
Aresult
11
00
171 32 ▶ return @declared if @declared;
Branch: 1 branch
BranchTrueFalseTotal
if @declared 0 32 32
172 32 my ( @types, %seen );
173 32 foreach my $returned ( @{ $rec->{returns} } ) {
174 38 ▶ next if $returned->{weak} || !$returned->{constraint} || $returned->{type} eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty" 14 24 38
Condition100%6/6
$returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty"
l!l&&r!l&&!r
7724
$returned->{"weak"} or not $returned->{"constraint"}
l!l&&r!l&&!r
1631
MC/DC100%3/3
$returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty"
$returned->{"weak"} not $returned->{"constraint"} $returned->{"type"} eq "empty"
Truth table100%4/4
A or not(B) or C
A= $returned->{"weak"}B= not $returned->{"constraint"}C= $returned->{"type"} eq "empty"
ABCresult
1XX1
01X1
0011
0000
175 24 ▶ push @types, $returned->{constraint} if !$seen{ $returned->{constraint}->display_name }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$returned->{"constraint"}->display_name}++ 24 0 24
176 }
177 32 return @types;
178 }
179  
180 # Constraints as the POD writes them in a Returns paragraph: an instance of one class in prose, a union as its alternatives,
181 # and any other by its name. Type::Tiny flattens a union of unions, so its members are no unions.
182 25 ▶sub _type_texts {
Subroutine _type_texts: called CC=5 SCAR=16.3
183 25 my (@types) = @_;
184 25 my @texts;
185 25 foreach my $type (@types) {
186 0 ▶ my @members = $type->isa('Type::Tiny::Union') ? @{ $type->type_constraints } : ($type);
Branch: 1 branch
BranchTrueFalseTotal
$type->isa('Type::Tiny::Union') ? : 0 31 31
187 31 foreach my $member (@members) {
188 31 ▶ push @texts, $member->isa('Type::Tiny::Class') ? 'a ' . $member->class . ' object' : $member->display_name;
Branch: 1 branch
BranchTrueFalseTotal
$member->isa('Type::Tiny::Class') ? : 12 19 31
189 }
190 }
191 25 return @texts;
192 }
193  
194 44 ▶sub _sentence {
Subroutine _sentence: called CC=2 SCAR=7.0
195 44 my ($text) = @_;
196 44 $text =~ s/\s+\z//;
197 44 $text = ucfirst $text;
198 44 ▶ return $text =~ m/[.!?]\z/ ? $text : "$text.";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[.!?]\z/aasx ? : 0 44 44
199 }
200  
201 5 1;
202  
203 __END__
« lib/Test/Pod/Coverage/More.pm ↑ Summary lib/Pod/Coverage/More.pm »