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

filter: Statementstmt 98.6% 141 / 143 Branchbran 76.5% 49 / 64 Conditioncond 63.0% 41 / 65 MC/DCmcdc 34.2% 12 / 35 Subroutinesub 100% 21 / 21 Podpod 100% 3 / 3 totaltotal 80.6% 267 / 331 CC 71 SCAR 23.2
CC
71
Cov
85%
_arguments
21.4
_parent_has_blurb
11.7
_section
11.1
CRAP 10.1
« 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.005;
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 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
24 7 my ( $class, %args ) = @_;
25 7 my $package = $args{package};
26 7 ▶ my $file = $args{file} // $INC{ module_notional_filename($package) };
Condition66%2/3
$args{"file"} // $INC{&module_notional_filename($package)}
l!l&&r!l&&!r
340
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 7 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 10 ▶sub sections {
Subroutine sections: called CC=8 SCAR=21.4
Pod documented
35 10 my ($self) = @_;
36 10 my $coverage = $self->{coverage};
37 10 ▶ my $blurbs = defined $self->{file} ? Pod::Coverage::More::Blurb->read_file( $self->{file} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 10 0 10
38 10 my $analysis = $coverage->analysis;
39 10 my @sections;
40 10 foreach my $name ( sort $coverage->public_subs ) {
41 64 ▶ my $blurb = $blurbs->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $blurb = $blurbs->{$name} or next 52 12 64
42 52 ▶ next if $coverage->own_section_for($name);
Branch: 1 branch
BranchTrueFalseTotal
if $coverage->own_section_for($name) 0 52 52
43 52 ▶ next if !_has_tag($blurb) && ( $coverage->section_for($name) || $self->_parent_has_blurb($name) );
Branch: 1 branch
BranchTrueFalseTotal
if not _has_tag($blurb) and $coverage->section_for($name) || $self->_parent_has_blurb($name) 2 50 52
Condition50%3/6
not _has_tag($blurb) and $coverage->section_for($name) || $self->_parent_has_blurb($name)
!ll&&!rl&&r
5002
$coverage->section_for($name) or $self->_parent_has_blurb($name)
l!l&&r!l&&!r
020
MC/DC33%1/3
not _has_tag($blurb) and $coverage->section_for($name) || $self->_parent_has_blurb($name)
not _has_tag($blurb) $coverage->section_for($name) $self->_parent_has_blurb($name)
Truth table50%2/4
not(A) and B or C
A= not _has_tag($blurb)B= $coverage->section_for($name)C= $self->_parent_has_blurb($name)
ABCresult
0XX0
1000
11X1
1011
44 50 push @sections, { name => $name, pod => _section( $name, $blurb, $analysis->{$name} ) };
45 }
46 10 return @sections;
47 }
48  
49 # Whether BLURB has a tag, which makes it the documentation of an override rather than a comment on it.
50 52 ▶sub _has_tag {
Subroutine _has_tag: called CC=5 SCAR=16.1
51 52 my ($blurb) = @_;
52 52 ▶ return @{ $blurb->{params} } || $blurb->{returns} || @{ $blurb->{throws} } || @{ $blurb->{examples} } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$blurb->{'params'};} || $blurb->{'returns'} || @{$blurb->{'throws'};} || @{$blurb->{'examples'};} ? : 50 2 52
Condition88%8/9
@{$blurb->{"params"};} or $blurb->{"returns"} or @{$blurb->{"throws"};} or @{$blurb->{"examples"};}
l!l&&r!l&&!r
4282
@{$blurb->{"params"};} or $blurb->{"returns"} or @{$blurb->{"throws"};}
l!l&&r!l&&!r
42010
@{$blurb->{"params"};} or $blurb->{"returns"}
l!l&&r!l&&!r
36610
MC/DC75%3/4
@{$blurb->{"params"};} or $blurb->{"returns"} or @{$blurb->{"throws"};} or @{$blurb->{"examples"};}
@{$blurb->{"params"};} $blurb->{"returns"} @{$blurb->{"throws"};} @{$blurb->{"examples"};}
Truth table80%4/5
A or B or C or D
A= @{$blurb->{"params"};}B= $blurb->{"returns"}C= @{$blurb->{"throws"};}D= @{$blurb->{"examples"};}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
53 }
54  
55 # Whether a parent class has a blurb for the sub NAME, which becomes its section when the parent is woven.
56 # Only a parent from the tree of the package counts: it is woven with the package. A parent from elsewhere, as an
57 # installed one, either was woven already, so that its POD documents the sub, or never was, so that its comment is none.
58 2 ▶sub _parent_has_blurb {
Subroutine _parent_has_blurb: called CC=9 SCAR=24.6
59 2 my ( $self, $name ) = @_;
60 2 ▶ my $tree = _tree_of( $self->{package} ) // return 0;
Condition50%1/2
_tree_of($self->{"package"}) // (return 0)
l!l
20
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
61 2 foreach my $parent ( @{ mro::get_linear_isa( $self->{package} ) } ) {
62 4 ▶ next if $parent eq $self->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $self->{"package"} 2 2 4
63 2 ▶ my $file = $INC{ module_notional_filename($parent) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $INC{&module_notional_filename($parent)} or next 2 0 2
64 2 ▶ next if ( _tree_of($parent) // q{} ) ne $tree;
Branch: 1 branch
BranchTrueFalseTotal
if (_tree_of($parent) // "") ne $tree 0 2 2
Condition50%1/2
_tree_of($parent) // ""
l!l
20
MC/DC0%0/1
_tree_of($parent) // ""
_tree_of($parent)
Truth table50%1/2
A
A= _tree_of($parent)
Aresult
11
00
65 2 ▶ $self->{parent_blurbs}{$parent} //= Pod::Coverage::More::Blurb->read_file($file);
Condition33%1/3
$self->{"parent_blurbs"}{$parent} //= "Pod::Coverage::More::Blurb"->read_file($file)
l!l&&r!l&&!r
020
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 table33%1/3
A //= B
A= $self->{"parent_blurbs"}{$parent}B= "Pod::Coverage::More::Blurb"->read_file($file)
ABresult
1X1
011
000
66 2 ▶ return 1 if $self->{parent_blurbs}{$parent}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent_blurbs"}{$parent}{$name} 2 0 2
67 }
68 0 return 0;
69 }
70  
71 # The directory on @INC that PACKAGE was loaded from, or nothing when it was not loaded from a file.
72 4 ▶sub _tree_of {
Subroutine _tree_of: called CC=2 SCAR=7.0
73 4 my ($package) = @_;
74 4 my $notional = module_notional_filename($package);
75 4 ▶ my $file = $INC{$notional} // return;
Condition50%1/2
$INC{$notional} // (return)
l!l
40
MC/DC0%0/1
$INC{$notional} // (return)
$INC{$notional}
Truth table50%1/2
A
A= $INC{$notional}
Aresult
11
00
76 4 return $file =~ s{/?\Q$notional\E\z}{}r;
77 }
78  
79 # Whether the text of a @throws is a sentence already, as "croaks when", because it starts with a word of termination.
80 # The noun "exception" does not count: "with a Some::Exception" still needs "Dies".
81 12 ▶sub _says_how_it_dies {
Subroutine _says_how_it_dies: called CC=2 SCAR=8.1
82 12 my ($throws) = @_;
83 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
84 }
85  
86 # The text of all the sections, one after another.
87 # @returns Str the POD, or the empty string when no sub gets a section
88 # @example my $pod = $generator->pod;
89 6 ▶sub pod {
Subroutine pod: called CC=1 SCAR=0.0
Pod documented
90 6 my ($self) = @_;
91 6 return join q{}, map { $_->{pod} } $self->sections;
92 }
93  
94 # The POD of one sub, from its BLURB and the analysis REC of its code.
95 50 ▶sub _section {
Subroutine _section: called CC=11 SCAR=24.0
96 50 my ( $name, $blurb, $rec ) = @_;
97 50 my @args = _arguments( $blurb, $rec );
98 50 ▶ my $pod = "=head2 $name" . ( @args ? '(' . join( ', ', map { $_->{name} } @args ) . ')' : q{} ) . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 36 14 50
99 50 foreach my $paragraph ( @{ $blurb->{description} } ) {
100 50 $pod .= "$paragraph\n\n";
101 }
102  
103 50 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 36 14 50
104 36 $pod .= "=over 4\n\n";
105 36 foreach my $arg (@args) {
106 48 ▶ my @parts = ( "$arg->{sigil}$arg->{name}", $arg->{type} ? $arg->{type}->display_name : (), $arg->{description} ne q{} ? $arg->{description} : () );
Branch: 2 branches
BranchTrueFalseTotal
$arg->{'type'} ? : 48 0 48
$arg->{'description'} ne '' ? : 48 0 48
107 48 $pod .= '=item ' . join( ' - ', @parts ) . "\n\n";
108 }
109 36 $pod .= "=back\n\n";
110 }
111  
112 50 my $returns = _returns( $blurb, $rec );
113 50 ▶ $pod .= "$returns\n\n" if defined $returns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $returns 42 8 50
114 50 foreach my $throws ( @{ $blurb->{throws} } ) {
115 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
116 }
117 50 foreach my $example ( @{ $blurb->{examples} } ) {
118 50 $pod .= join( "\n", map { " $_" } @$example ) . "\n\n";
119 }
120 50 return $pod;
121 }
122  
123 # 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.
124 50 ▶sub _arguments {
Subroutine _arguments: called CC=18 SCAR=30.6
125 50 my ( $blurb, $rec ) = @_;
126 50 my %documented;
127 50 foreach my $param ( @{ $blurb->{params} } ) {
128 48 $documented{ Pod::Coverage::More::Documentation->bare_name( $param->{name} ) } = $param;
129 }
130 50 my @names;
131 50 ▶ foreach my $param ( @{ $rec->{params} || [] } ) {
Condition50%1/2
$rec->{'params'} || []
l!l
500
MC/DC0%0/1
$rec->{'params'} || []
$rec->{'params'}
Truth table50%1/2
A
A= $rec->{'params'}
Aresult
11
00
132 42 ▶ 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 35 42
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
0735
not defined $param->{"name"} or $param->{"source"} eq "invocant"
l!l&&r!l&&!r
0042
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
133 35 ▶ if ( $param->{name} =~ m/\A%/ && $param->{keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"name"} =~ /\A%/aasx and $param->{"keys"}) 6 29 35
Condition66%2/3
$param->{"name"} =~ /\A%/aasx and $param->{"keys"}
!ll&&!rl&&r
2906
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
134 6 push @names, map { [ $_, undef ] } @{ $param->{keys} };
135 6 next;
136 }
137 29 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), $param ];
138 }
139 50 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 21 29 50
140 21 foreach my $param ( @{ $blurb->{params} } ) {
141 7 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), undef ];
142 }
143 }
144  
145 50 my @args;
146 50 foreach my $entry (@names) {
147 48 my ( $name, $param ) = @$entry;
148 48 ▶ my $documented = $documented{$name} // {};
Condition50%1/2
$documented{$name} // {}
l!l
480
MC/DC0%0/1
$documented{$name} // {}
$documented{$name}
Truth table50%1/2
A
A= $documented{$name}
Aresult
11
00
149 push @args, {
150 name => $name,
151 sigil => ( $documented->{name} // q{} ) =~ m/\A([@%])/ ? $1 : q{},
152 type => $documented->{type} // ( $param ? $param->{declared} : undef ),
153 48 ▶ description => $documented->{description} // q{},
Branch: 2 branches
BranchTrueFalseTotal
($documented->{'name'} // '') =~ /\A([\@%])/aasx ? : 6 42 48
$param ? : 0 0 0
Condition42%3/7
$documented->{"name"} // ""
l!l
480
$documented->{"type"} // ($param ? $param->{"declared"} : undef)
l!l&&r!l&&!r
4800
$documented->{"description"} // ""
l!l
480
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
154 };
155 }
156 50 return @args;
157 }
158  
159 # The paragraph of the return value, or undef when neither the blurb nor what the code shows for certain says anything.
160 50 ▶sub _returns {
Subroutine _returns: called CC=9 SCAR=22.0
161 50 my ( $blurb, $rec ) = @_;
162 50 ▶ my $stated = $blurb->{returns} // {};
Condition100%2/2
$blurb->{"returns"} // {}
l!l
2426
MC/DC100%1/1
$blurb->{"returns"} // {}
$blurb->{"returns"}
Truth table100%2/2
A
A= $blurb->{"returns"}
Aresult
11
00
163 50 ▶ my $description = $stated->{description} // q{};
Condition100%2/2
$stated->{"description"} // ""
l!l
2426
MC/DC100%1/1
$stated->{"description"} // ""
$stated->{"description"}
Truth table100%2/2
A
A= $stated->{"description"}
Aresult
11
00
164 50 ▶ my @types = $stated->{type} ? ( $stated->{type} ) : _code_return_types($rec);
Branch: 1 branch
BranchTrueFalseTotal
$stated->{'type'} ? : 11 39 50
165 50 ▶ return if !@types && $description eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not @types and $description eq "" 8 42 50
Condition100%3/3
not @types and $description eq ""
!ll&&!rl&&r
3578
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
166  
167 42 my $text = 'Returns';
168 42 ▶ $text .= q{ } . join( q{ or }, _type_texts(@types) ) if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 35 7 42
169 42 ▶ $text .= ( @types ? q{, } : q{ } ) . $description if $description ne q{};
Branch: 2 branches
BranchTrueFalseTotal
if $description ne "" 24 18 42
@types ? : 17 7 24
170 42 return _sentence($text);
171 }
172  
173 # The return types that the code declares, or else those that its literals and objects show for certain.
174 39 ▶sub _code_return_types {
Subroutine _code_return_types: called CC=9 SCAR=22.5
175 39 my ($rec) = @_;
176 39 ▶ return if !$rec;
Branch: 1 branch
BranchTrueFalseTotal
unless $rec 0 39 39
177 39 ▶ my @declared = @{ $rec->{declared}{returns} || [] };
Condition50%1/2
$rec->{'declared'}{'returns'} || []
l!l
039
MC/DC0%0/1
$rec->{'declared'}{'returns'} || []
$rec->{'declared'}{'returns'}
Truth table50%1/2
A
A= $rec->{'declared'}{'returns'}
Aresult
11
00
178 39 ▶ return @declared if @declared;
Branch: 1 branch
BranchTrueFalseTotal
if @declared 0 39 39
179 39 my ( @types, %seen );
180 39 foreach my $returned ( @{ $rec->{returns} } ) {
181 45 ▶ 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" 15 30 45
Condition83%5/6
$returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty"
l!l&&r!l&&!r
6930
$returned->{"weak"} or not $returned->{"constraint"}
l!l&&r!l&&!r
0639
MC/DC66%2/3
$returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty"
$returned->{"weak"} not $returned->{"constraint"} $returned->{"type"} eq "empty"
Truth table75%3/4
A or not(B) or C
A= $returned->{"weak"}B= not $returned->{"constraint"}C= $returned->{"type"} eq "empty"
ABCresult
1XX1
01X1
0011
0000
182 30 ▶ push @types, $returned->{constraint} if !$seen{ $returned->{constraint}->display_name }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$returned->{"constraint"}->display_name}++ 30 0 30
183 }
184 39 return @types;
185 }
186  
187 # Constraints as the POD writes them in a Returns paragraph: an instance of one class in prose, a union as its alternatives,
188 # and any other by its name. Type::Tiny flattens a union of unions, so its members are no unions.
189 35 ▶sub _type_texts {
Subroutine _type_texts: called CC=5 SCAR=16.3
190 35 my (@types) = @_;
191 35 my @texts;
192 35 foreach my $type (@types) {
193 0 ▶ my @members = $type->isa('Type::Tiny::Union') ? @{ $type->type_constraints } : ($type);
Branch: 1 branch
BranchTrueFalseTotal
$type->isa('Type::Tiny::Union') ? : 0 41 41
194 41 foreach my $member (@members) {
195 41 ▶ push @texts, $member->isa('Type::Tiny::Class') ? 'a ' . $member->class . ' object' : $member->display_name;
Branch: 1 branch
BranchTrueFalseTotal
$member->isa('Type::Tiny::Class') ? : 12 29 41
196 }
197 }
198 35 return @texts;
199 }
200  
201 54 ▶sub _sentence {
Subroutine _sentence: called CC=2 SCAR=7.0
202 54 my ($text) = @_;
203 54 $text =~ s/\s+\z//;
204 54 $text = ucfirst $text;
205 54 ▶ return $text =~ m/[.!?]\z/ ? $text : "$text.";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[.!?]\z/aasx ? : 0 54 54
206 }
207  
208 5 1;
209  
210 __END__
« lib/Test/Pod/Coverage/More.pm ↑ Summary lib/Pod/Coverage/More.pm »