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

filter: Statementstmt 100% 120 / 120 Branchbran 89.5% 43 / 48 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 94.4% 186 / 197 CC 29 SCAR 13.9
CC
29
Cov
95%
read_string
8.0
_type_and_description
5.2
_parse
4.0
CRAP 4.0
« lib/Pod/Coverage/More/Declared.pm ↑ Summary lib/Pod/Coverage/More/Declared/ObjectPad.pm »
1 # ABSTRACT: Read the comment above each sub, which says what POD cannot be inferred from its code
2 # PODNAME: Pod::Coverage::More::Blurb
3  
4 package Pod::Coverage::More::Blurb 0.005;
5  
6 7 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Types::Standard qw{InstanceOf};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Pod::Coverage::More::Types qw{type_named};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # The blurbs of the subs in C<file>, as BLURBS describes them.
16 # @param file Str the path of the file
17 # @returns HashRef from the name of each sub with a blurb to its blurb
18 # @throws when the file cannot be read
19 # @example my $blurbs = Pod::Coverage::More::Blurb->read_file('lib/Some/Module.pm');
20 12 ▶sub read_file {
Subroutine read_file: called CC=3 SCAR=11.2
Pod documented
21 12 my ( $class, $file ) = @_;
22 12 ▶ open( my $fh, '<', $file ) or die "Cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot read ${file}: $!\n" 12 0 12
23 12 local $/;
24 12 my $source = <$fh>;
25 12 ▶ close($fh) or die "Cannot close $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file}: $!\n" 12 0 12
26 12 return $class->read_string($source);
27 }
28  
29 # The line that starts a sub, which a blurb sits above, with the name of the sub.
30 7 our $SUB_LINE = qr/\A\s*(?:sub|method|fun|func)\s+(?:\w+::)*(\w+)\b/;
31  
32 # The blurbs of the subs in the perl source C<source>, as BLURBS describes them.
33 # @param source Str the perl source
34 # @returns HashRef from the name of each sub with a blurb to its blurb
35 # @example my $blurbs = Pod::Coverage::More::Blurb->read_string($source);
36 16 ▶sub read_string {
Subroutine read_string: called CC=8 SCAR=20.8
Pod documented
37 16 my ( $class, $source ) = @_;
38 16 my @lines = split m/\n/, $source;
39 16 my %blurbs;
40 my @comment;
41 16 my $in_pod = 0;
42 16 foreach my $index ( 0 .. $#lines ) {
43 873 my $line = $lines[$index];
44 873 ▶ last if $line =~ m/\A__(?:END|DATA)__\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A__(?:END|DATA)__\b/aasx 1 872 873
45 872 ▶ if ( $line =~ m/\A=(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A=(\w+)/aasx) 26 846 872
46 26 $in_pod = $1 ne 'cut';
47 26 @comment = ();
48 26 next;
49 }
50 846 ▶ next if $in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod 68 778 846
51 778 ▶ if ( $line =~ m/\A\s*\#(?!\#)[ ]?(.*)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\#(?!\#)[ ]?(.*)\z/aasx) 281 497 778
52 281 push @comment, $1;
53 281 next;
54 }
55 497 ▶ if ( my ($name) = $line =~ $SUB_LINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $line =~ /$Pod::Coverage::More::Blurb::SUB_LINE/aasx) 92 405 497
56 92 ▶ $blurbs{$name} = { %{ _parse(@comment) }, line => $index + 1 } if @comment;
Branch: 1 branch
BranchTrueFalseTotal
if @comment 74 18 92
57 }
58 497 @comment = ();
59 }
60 16 return \%blurbs;
61 }
62  
63 # The handlers of the tags, which each record what the rest of a tagged line says.
64 7 our %TAG_HANDLERS = (
65 param => \&_tag_param,
66 returns => \&_tag_returns,
67 return => \&_tag_returns,
68 throws => \&_tag_throws,
69 dies => \&_tag_throws,
70 example => \&_tag_example,
71 );
72  
73 # The blurb that the comment LINES make, without their #.
74 74 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
75 74 my (@lines) = @_;
76 74 my $state = {
77 blurb => { description => [], params => [], returns => undef, throws => [], examples => [] },
78 paragraph => q{},
79 current => undef,
80 last_tag => q{},
81 };
82 74 foreach my $line (@lines) {
83 265 my ( $tag, $after ) = $line =~ m/\A\s*\@(\w+)\b(.*)\z/;
84 265 ▶ my $handler = defined $tag ? $TAG_HANDLERS{ lc $tag } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 189 76 265
85 265 ▶ if ( !$handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 76 189 265
86 76 _untagged( $state, $line );
87 76 next;
88 }
89 189 $handler->( $state, $after =~ s/\A\s+//r, $after );
90 189 $state->{last_tag} = lc $tag;
91 }
92 74 _end_paragraph($state);
93 74 my $blurb = $state->{blurb};
94 74 $blurb->{throws} = [ map { $_->{description} } @{ $blurb->{throws} } ];
95 74 return $blurb;
96 }
97  
98 # A line without a tag continues the tag above it, or else the description.
99 76 ▶sub _untagged {
Subroutine _untagged: called CC=4 SCAR=13.9
100 76 my ( $state, $line ) = @_;
101 76 $state->{last_tag} = q{};
102 76 ▶ if ( my $current = $state->{current} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = $state->{"current"}) 4 72 76
103 4 ▶ if ( $current->{example} ) { push @{ $current->{example} }, $line }
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{'example'}) { } 2 2 4
104 2 else { $current->{description} = _join( $current->{description}, $line ) }
105 4 return;
106 }
107 72 ▶ if ( $line =~ m/\A\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\z/aasx) 1 71 72
108 1 _end_paragraph($state);
109 1 return;
110 }
111 71 $state->{paragraph} = _join( $state->{paragraph}, $line );
112 71 return;
113 }
114  
115 75 ▶sub _end_paragraph {
Subroutine _end_paragraph: called CC=2 SCAR=6.9
116 75 my ($state) = @_;
117 75 ▶ push @{ $state->{blurb}{description} }, $state->{paragraph} if $state->{paragraph} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"paragraph"} ne "" 70 5 75
118 75 $state->{paragraph} = q{};
119 75 return;
120 }
121  
122 65 ▶sub _tag_param {
Subroutine _tag_param: called CC=2 SCAR=7.0
123 65 my ( $state, $rest ) = @_;
124 65 my ( $name, $after ) = $rest =~ m/\A(\S+)\s*(.*)\z/;
125 65 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 65 65
126 65 my ( $type, $description ) = _type_and_description($after);
127 65 $state->{current} = { name => $name, type => $type, description => $description };
128 65 push @{ $state->{blurb}{params} }, $state->{current};
129 65 return;
130 }
131  
132 33 ▶sub _tag_returns {
Subroutine _tag_returns: called CC=1 SCAR=0.0
133 33 my ( $state, $rest ) = @_;
134 33 my ( $type, $description ) = _type_and_description($rest);
135 33 $state->{current} = $state->{blurb}{returns} = { type => $type, description => $description };
136 33 return;
137 }
138  
139 17 ▶sub _tag_throws {
Subroutine _tag_throws: called CC=1 SCAR=0.0
140 17 my ( $state, $rest ) = @_;
141 17 $state->{current} = { description => $rest };
142 17 push @{ $state->{blurb}{throws} }, $state->{current};
143 17 return;
144 }
145  
146 # Consecutive @example lines make one example, and each keeps its indentation relative to the first, from AFTER the tag.
147 74 ▶sub _tag_example {
Subroutine _tag_example: called CC=3 SCAR=11.0
148 74 my ( $state, $rest, $after ) = @_;
149 74 my $examples = $state->{blurb}{examples};
150 74 ▶ if ( $state->{last_tag} eq 'example' && @$examples ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'last_tag'} eq 'example' and @$examples) { } 6 68 74
Condition66%2/3
$state->{"last_tag"} eq "example" and @$examples
!ll&&!rl&&r
6806
MC/DC50%1/2
$state->{"last_tag"} eq "example" and @$examples
$state->{"last_tag"} eq "example" @$examples
Truth table66%2/3
A and B
A= $state->{"last_tag"} eq "example"B= @$examples
ABresult
0X0
100
111
151 6 my $indent = $state->{example_indent};
152 6 push @{ $examples->[-1] }, $after =~ s/\A[ ]{0,$indent}//r;
153 }
154 else {
155 68 $state->{example_indent} = length( $after =~ s/\S.*\z//r );
156 68 push @$examples, [$rest];
157 }
158 74 $state->{current} = { example => $examples->[-1] };
159 74 return;
160 }
161  
162 73 ▶sub _join {
Subroutine _join: called CC=3 SCAR=11.0
163 73 my ( $text, $more ) = @_;
164 73 $more =~ s/\A\s+|\s+\z//g;
165 73 ▶ return $more if $text eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 70 3 73
166 3 ▶ return $text if $more eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $more eq "" 0 3 3
167 3 return "$text $more";
168 }
169  
170 # The type that the first word of TEXT names, if it names one, and the rest of TEXT as the description.
171 98 ▶sub _type_and_description {
Subroutine _type_and_description: called CC=5 SCAR=16.4
172 98 my ($text) = @_;
173 98 my ( $word, $rest ) = $text =~ m/\A(\S+)\s*(.*)\z/;
174 98 ▶ return ( undef, q{} ) if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 98 98
175 98 my $type = type_named($word);
176 98 ▶ $type //= InstanceOf [$word] if _is_package($word);
Branch: 1 branch
BranchTrueFalseTotal
if _is_package($word) 2 96 98
Condition33%1/3
$type //= &InstanceOf([$word])
l!l&&r!l&&!r
020
MC/DC0%0/2
$type //= &InstanceOf([$word])
$type &InstanceOf([$word])
Truth table33%1/3
A //= B
A= $typeB= &InstanceOf([$word])
ABresult
1X1
011
000
177 98 ▶ return ( undef, $text =~ s/\A[-:\s]+//r ) if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 20 78 98
178 78 return ( $type, $rest =~ s/\A[-:\s]+//r );
179 }
180  
181 # Whether WORD names a loaded package, as a class name in place of a type means an instance of that class.
182 98 ▶sub _is_package {
Subroutine _is_package: called CC=4 SCAR=13.9
183 98 my ($word) = @_;
184 98 ▶ return 0 if $word !~ m/\A[[:upper:]]\w*(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /\A[[:upper:]]\w*(?:::\w+)*\z/aasx 21 77 98
185 77 ▶ return 1 if index( $word, q{::} ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($word, "::") != -1 1 76 77
186 76 ▶ return exists $main::{"${word}::"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $main::main::{"${word}::"} ? : 1 75 76
187 }
188  
189 7 1;
190  
191 __END__
« lib/Pod/Coverage/More/Declared.pm ↑ Summary lib/Pod/Coverage/More/Declared/ObjectPad.pm »