Pod/Coverage/More/Generator.pm

filter: Statementstmt 99.2% 128 / 129 Branchbran 75.0% 42 / 56 Conditioncond 64.2% 27 / 42 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 100% 18 / 18 Podpod 100% 3 / 3 totaltotal 83.0% 226 / 272 CC 58 SCAR 22.4
CC
58
Cov
87%
_arguments
18.6
_section
11.1
_code_return_types
9.5
CRAP 9.4
« Test/Pod/Coverage/More.pm ↑ Summary Pod/Coverage/More/Analyzer/Perl5_44.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.001;
5  
6 4 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Module::Runtime qw{module_notional_filename};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Pod::Coverage::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Pod::Coverage::More::Blurb;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶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 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
24 4 my ( $class, %args ) = @_;
25 4 my $package = $args{package};
26 4 ▶ my $file = $args{file} // $INC{ module_notional_filename($package) };
Condition66%2/3
$args{"file"} // $INC{&module_notional_filename($package)}
l!l&&r!l&&!r
220
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 4 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 7 ▶sub sections {
Subroutine sections: called CC=6 SCAR=18.0
Pod documented
35 7 my ($self) = @_;
36 7 my $coverage = $self->{coverage};
37 7 ▶ my $blurbs = defined $self->{file} ? Pod::Coverage::More::Blurb->read_file( $self->{file} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 7 0 7
38 7 my $analysis = $coverage->analysis;
39 7 my @sections;
40 7 foreach my $name ( sort $coverage->public_subs ) {
41 44 ▶ my $blurb = $blurbs->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $blurb = $blurbs->{$name} or next 32 12 44
42 32 ▶ 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 31 32
Condition66%2/3
$coverage->section_for($name) or $self->_parent_has_blurb($name)
l!l&&r!l&&!r
0131
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 31 push @sections, { name => $name, pod => _section( $name, $blurb, $analysis->{$name} ) };
44 }
45 7 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 32 ▶sub _parent_has_blurb {
Subroutine _parent_has_blurb: called CC=6 SCAR=18.0
50 32 my ( $self, $name ) = @_;
51 32 foreach my $parent ( @{ mro::get_linear_isa( $self->{package} ) } ) {
52 34 ▶ next if $parent eq $self->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $self->{"package"} 32 2 34
53 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
54 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
55 2 ▶ return 1 if $self->{parent_blurbs}{$parent}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent_blurbs"}{$parent}{$name} 1 1 2
56 }
57 31 return 0;
58 }
59  
60 # The text of all the sections, one after another.
61 # @returns Str the POD, or the empty string when no sub gets a section
62 # @example my $pod = $generator->pod;
63 4 ▶sub pod {
Subroutine pod: called CC=1 SCAR=0.0
Pod documented
64 4 my ($self) = @_;
65 4 return join q{}, map { $_->{pod} } $self->sections;
66 }
67  
68 # The POD of one sub, from its BLURB and the analysis REC of its code.
69 31 ▶sub _section {
Subroutine _section: called CC=11 SCAR=24.0
70 31 my ( $name, $blurb, $rec ) = @_;
71 31 my @args = _arguments( $blurb, $rec );
72 31 ▶ my $pod = "=head2 $name" . ( @args ? '(' . join( ', ', map { $_->{name} } @args ) . ')' : q{} ) . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 25 6 31
73 31 foreach my $paragraph ( @{ $blurb->{description} } ) {
74 31 $pod .= "$paragraph\n\n";
75 }
76  
77 31 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 25 6 31
78 25 $pod .= "=over 4\n\n";
79 25 foreach my $arg (@args) {
80 31 ▶ my @parts = ( $arg->{name}, $arg->{type} ? $arg->{type}->display_name : (), $arg->{description} ne q{} ? $arg->{description} : () );
Branch: 2 branches
BranchTrueFalseTotal
$arg->{'type'} ? : 31 0 31
$arg->{'description'} ne '' ? : 31 0 31
81 31 $pod .= '=item ' . join( ' - ', @parts ) . "\n\n";
82 }
83 25 $pod .= "=back\n\n";
84 }
85  
86 31 my $returns = _returns( $blurb, $rec );
87 31 ▶ $pod .= "$returns\n\n" if defined $returns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $returns 24 7 31
88 31 foreach my $throws ( @{ $blurb->{throws} } ) {
89 6 ▶ $pod .= _sentence( $throws =~ $Pod::Coverage::More::Documentation::TERMINATION ? $throws : "Dies $throws" ) . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$throws =~ /$Pod::Coverage::More::Documentation::TERMINATION/aasx ? : 0 6 6
90 }
91 31 foreach my $example ( @{ $blurb->{examples} } ) {
92 31 $pod .= join( "\n", map { " $_" } @$example ) . "\n\n";
93 }
94 31 return $pod;
95 }
96  
97 # 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.
98 31 ▶sub _arguments {
Subroutine _arguments: called CC=16 SCAR=29.2
99 31 my ( $blurb, $rec ) = @_;
100 31 my %documented;
101 31 foreach my $param ( @{ $blurb->{params} } ) {
102 31 $documented{ Pod::Coverage::More::Documentation->bare_name( $param->{name} ) } = $param;
103 }
104 31 my @names;
105 31 ▶ foreach my $param ( @{ $rec->{params} || [] } ) {
Condition50%1/2
$rec->{'params'} || []
l!l
310
MC/DC0%0/1
$rec->{'params'} || []
$rec->{'params'}
Truth table50%1/2
A
A= $rec->{'params'}
Aresult
11
00
106 25 ▶ 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 6 19 25
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
0619
not defined $param->{"name"} or $param->{"source"} eq "invocant"
l!l&&r!l&&!r
0025
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
107 19 ▶ if ( $param->{name} =~ m/\A%/ && $param->{keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"name"} =~ /\A%/aasx and $param->{"keys"}) 6 13 19
Condition66%2/3
$param->{"name"} =~ /\A%/aasx and $param->{"keys"}
!ll&&!rl&&r
1306
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
108 6 push @names, map { [ $_, undef ] } @{ $param->{keys} };
109 6 next;
110 }
111 13 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), $param ];
112 }
113 31 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 12 19 31
114 12 foreach my $param ( @{ $blurb->{params} } ) {
115 6 push @names, [ Pod::Coverage::More::Documentation->bare_name( $param->{name} ), undef ];
116 }
117 }
118  
119 31 my @args;
120 31 foreach my $entry (@names) {
121 31 my ( $name, $param ) = @$entry;
122 31 ▶ my $documented = $documented{$name} // {};
Condition50%1/2
$documented{$name} // {}
l!l
310
MC/DC0%0/1
$documented{$name} // {}
$documented{$name}
Truth table50%1/2
A
A= $documented{$name}
Aresult
11
00
123 push @args, {
124 name => $name,
125 type => $documented->{type} // ( $param ? $param->{declared} : undef ),
126 31 ▶ description => $documented->{description} // q{},
Branch: 1 branch
BranchTrueFalseTotal
$param ? : 0 0 0
Condition40%2/5
$documented->{"type"} // ($param ? $param->{"declared"} : undef)
l!l&&r!l&&!r
3100
$documented->{"description"} // ""
l!l
310
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 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
127 };
128 }
129 31 return @args;
130 }
131  
132 # The paragraph of the return value, or undef when neither the blurb nor what the code shows for certain says anything.
133 31 ▶sub _returns {
Subroutine _returns: called CC=9 SCAR=22.1
134 31 my ( $blurb, $rec ) = @_;
135 31 ▶ my $stated = $blurb->{returns} // {};
Condition100%2/2
$blurb->{"returns"} // {}
l!l
625
MC/DC100%1/1
$blurb->{"returns"} // {}
$blurb->{"returns"}
Truth table100%2/2
A
A= $blurb->{"returns"}
Aresult
11
00
136 31 ▶ my $description = $stated->{description} // q{};
Condition100%2/2
$stated->{"description"} // ""
l!l
625
MC/DC100%1/1
$stated->{"description"} // ""
$stated->{"description"}
Truth table100%2/2
A
A= $stated->{"description"}
Aresult
11
00
137 31 ▶ my @types = $stated->{type} ? ( $stated->{type} ) : _code_return_types($rec);
Branch: 1 branch
BranchTrueFalseTotal
$stated->{'type'} ? : 6 25 31
138 31 ▶ return if !@types && $description eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not @types and $description eq "" 7 24 31
Condition66%2/3
not @types and $description eq ""
!ll&&!rl&&r
2407
MC/DC50%1/2
not @types and $description eq ""
not @types $description eq ""
Truth table66%2/3
not(A) and B
A= not @typesB= $description eq ""
ABresult
0X0
100
111
139  
140 24 my $text = 'Returns';
141 24 ▶ $text .= q{ } . join( q{ or }, _type_texts(@types) ) if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 24 0 24
142 24 ▶ $text .= ( @types ? q{, } : q{ } ) . $description if $description ne q{};
Branch: 2 branches
BranchTrueFalseTotal
if $description ne "" 6 18 24
@types ? : 6 0 6
143 24 return _sentence($text);
144 }
145  
146 # The return types that the code declares, or else those that its literals and objects show for certain.
147 25 ▶sub _code_return_types {
Subroutine _code_return_types: called CC=9 SCAR=22.5
148 25 my ($rec) = @_;
149 25 ▶ return if !$rec;
Branch: 1 branch
BranchTrueFalseTotal
unless $rec 0 25 25
150 25 ▶ my @declared = @{ $rec->{declared}{returns} || [] };
Condition50%1/2
$rec->{'declared'}{'returns'} || []
l!l
025
MC/DC0%0/1
$rec->{'declared'}{'returns'} || []
$rec->{'declared'}{'returns'}
Truth table50%1/2
A
A= $rec->{'declared'}{'returns'}
Aresult
11
00
151 25 ▶ return @declared if @declared;
Branch: 1 branch
BranchTrueFalseTotal
if @declared 0 25 25
152 25 my ( @types, %seen );
153 25 foreach my $returned ( @{ $rec->{returns} } ) {
154 31 ▶ 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" 7 24 31
Condition83%5/6
$returned->{"weak"} or not $returned->{"constraint"} or $returned->{"type"} eq "empty"
l!l&&r!l&&!r
1624
$returned->{"weak"} or not $returned->{"constraint"}
l!l&&r!l&&!r
1030
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
155 24 ▶ push @types, $returned->{constraint} if !$seen{ $returned->{constraint}->display_name }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$returned->{"constraint"}->display_name}++ 24 0 24
156 }
157 25 return @types;
158 }
159  
160 # Constraints as the POD writes them in a Returns paragraph: an instance of one class in prose, a union as its alternatives,
161 # and any other by its name. Type::Tiny flattens a union of unions, so its members are no unions.
162 24 ▶sub _type_texts {
Subroutine _type_texts: called CC=5 SCAR=16.3
163 24 my (@types) = @_;
164 24 my @texts;
165 24 foreach my $type (@types) {
166 0 ▶ my @members = $type->isa('Type::Tiny::Union') ? @{ $type->type_constraints } : ($type);
Branch: 1 branch
BranchTrueFalseTotal
$type->isa('Type::Tiny::Union') ? : 0 30 30
167 30 foreach my $member (@members) {
168 30 ▶ push @texts, $member->isa('Type::Tiny::Class') ? 'a ' . $member->class . ' object' : $member->display_name;
Branch: 1 branch
BranchTrueFalseTotal
$member->isa('Type::Tiny::Class') ? : 12 18 30
169 }
170 }
171 24 return @texts;
172 }
173  
174 30 ▶sub _sentence {
Subroutine _sentence: called CC=2 SCAR=7.0
175 30 my ($text) = @_;
176 30 $text =~ s/\s+\z//;
177 30 $text = ucfirst $text;
178 30 ▶ return $text =~ m/[.!?]\z/ ? $text : "$text.";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[.!?]\z/aasx ? : 0 30 30
179 }
180  
181 4 1;
182  
183 __END__
« Test/Pod/Coverage/More.pm ↑ Summary Pod/Coverage/More/Analyzer/Perl5_44.pm »