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

filter: Statementstmt 98.3% 116 / 118 Branchbran 85.4% 41 / 48 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 92.3% 180 / 195 CC 29 SCAR 13.9
CC
29
Cov
93%
read_string
8.0
_type_and_description
5.2
_untagged
4.1
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.003;
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 10 ▶sub read_file {
Subroutine read_file: called CC=3 SCAR=11.2
Pod documented
21 10 my ( $class, $file ) = @_;
22 10 ▶ open( my $fh, '<', $file ) or die "Cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot read ${file}: $!\n" 10 0 10
23 10 local $/;
24 10 my $source = <$fh>;
25 10 ▶ close($fh) or die "Cannot close $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file}: $!\n" 10 0 10
26 10 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 12 ▶sub read_string {
Subroutine read_string: called CC=8 SCAR=20.8
Pod documented
37 12 my ( $class, $source ) = @_;
38 12 my @lines = split m/\n/, $source;
39 12 my %blurbs;
40 my @comment;
41 12 my $in_pod = 0;
42 12 foreach my $index ( 0 .. $#lines ) {
43 656 my $line = $lines[$index];
44 656 ▶ last if $line =~ m/\A__(?:END|DATA)__\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A__(?:END|DATA)__\b/aasx 1 655 656
45 655 ▶ if ( $line =~ m/\A=(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A=(\w+)/aasx) 23 632 655
46 23 $in_pod = $1 ne 'cut';
47 23 @comment = ();
48 23 next;
49 }
50 632 ▶ next if $in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod 60 572 632
51 572 ▶ if ( $line =~ m/\A\s*\#(?!\#)[ ]?(.*)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\#(?!\#)[ ]?(.*)\z/aasx) 200 372 572
52 200 push @comment, $1;
53 200 next;
54 }
55 372 ▶ if ( my ($name) = $line =~ $SUB_LINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $line =~ /$Pod::Coverage::More::Blurb::SUB_LINE/aasx) 65 307 372
56 65 ▶ $blurbs{$name} = { %{ _parse(@comment) }, line => $index + 1 } if @comment;
Branch: 1 branch
BranchTrueFalseTotal
if @comment 49 16 65
57 }
58 372 @comment = ();
59 }
60 12 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 49 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
75 49 my (@lines) = @_;
76 49 my $state = {
77 blurb => { description => [], params => [], returns => undef, throws => [], examples => [] },
78 paragraph => q{},
79 current => undef,
80 last_tag => q{},
81 };
82 49 foreach my $line (@lines) {
83 188 my ( $tag, $rest ) = $line =~ m/\A\s*\@(\w+)\b\s*(.*)\z/;
84 188 ▶ my $handler = defined $tag ? $TAG_HANDLERS{ lc $tag } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 134 54 188
85 188 ▶ if ( !$handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 54 134 188
86 54 _untagged( $state, $line );
87 54 next;
88 }
89 134 $handler->( $state, $rest );
90 134 $state->{last_tag} = lc $tag;
91 }
92 49 _end_paragraph($state);
93 49 my $blurb = $state->{blurb};
94 49 $blurb->{throws} = [ map { $_->{description} } @{ $blurb->{throws} } ];
95 49 return $blurb;
96 }
97  
98 # A line without a tag continues the tag above it, or else the description.
99 54 ▶sub _untagged {
Subroutine _untagged: called CC=4 SCAR=14.0
100 54 my ( $state, $line ) = @_;
101 54 $state->{last_tag} = q{};
102 54 ▶ if ( my $current = $state->{current} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = $state->{"current"}) 2 52 54
103 0 ▶ if ( $current->{example} ) { push @{ $current->{example} }, $line }
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{'example'}) { } 0 2 2
104 2 else { $current->{description} = _join( $current->{description}, $line ) }
105 2 return;
106 }
107 52 ▶ if ( $line =~ m/\A\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\z/aasx) 1 51 52
108 1 _end_paragraph($state);
109 1 return;
110 }
111 51 $state->{paragraph} = _join( $state->{paragraph}, $line );
112 51 return;
113 }
114  
115 50 ▶sub _end_paragraph {
Subroutine _end_paragraph: called CC=2 SCAR=7.0
116 50 my ($state) = @_;
117 50 ▶ push @{ $state->{blurb}{description} }, $state->{paragraph} if $state->{paragraph} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"paragraph"} ne "" 50 0 50
118 50 $state->{paragraph} = q{};
119 50 return;
120 }
121  
122 55 ▶sub _tag_param {
Subroutine _tag_param: called CC=2 SCAR=7.0
123 55 my ( $state, $rest ) = @_;
124 55 my ( $name, $after ) = $rest =~ m/\A(\S+)\s*(.*)\z/;
125 55 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 55 55
126 55 my ( $type, $description ) = _type_and_description($after);
127 55 $state->{current} = { name => $name, type => $type, description => $description };
128 55 push @{ $state->{blurb}{params} }, $state->{current};
129 55 return;
130 }
131  
132 17 ▶sub _tag_returns {
Subroutine _tag_returns: called CC=1 SCAR=0.0
133 17 my ( $state, $rest ) = @_;
134 17 my ( $type, $description ) = _type_and_description($rest);
135 17 $state->{current} = $state->{blurb}{returns} = { type => $type, description => $description };
136 17 return;
137 }
138  
139 15 ▶sub _tag_throws {
Subroutine _tag_throws: called CC=1 SCAR=0.0
140 15 my ( $state, $rest ) = @_;
141 15 $state->{current} = { description => $rest };
142 15 push @{ $state->{blurb}{throws} }, $state->{current};
143 15 return;
144 }
145  
146 # Consecutive @example lines make one example.
147 47 ▶sub _tag_example {
Subroutine _tag_example: called CC=3 SCAR=11.0
148 47 my ( $state, $rest ) = @_;
149 47 my $examples = $state->{blurb}{examples};
150 47 ▶ if ( $state->{last_tag} eq 'example' && @$examples ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'last_tag'} eq 'example' and @$examples) { } 1 46 47
Condition66%2/3
$state->{"last_tag"} eq "example" and @$examples
!ll&&!rl&&r
4601
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 1 push @{ $examples->[-1] }, $rest;
152 }
153 else {
154 46 push @$examples, [$rest];
155 }
156 47 $state->{current} = { example => $examples->[-1] };
157 47 return;
158 }
159  
160 53 ▶sub _join {
Subroutine _join: called CC=3 SCAR=11.0
161 53 my ( $text, $more ) = @_;
162 53 $more =~ s/\A\s+|\s+\z//g;
163 53 ▶ return $more if $text eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 50 3 53
164 3 ▶ return $text if $more eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $more eq "" 0 3 3
165 3 return "$text $more";
166 }
167  
168 # The type that the first word of TEXT names, if it names one, and the rest of TEXT as the description.
169 72 ▶sub _type_and_description {
Subroutine _type_and_description: called CC=5 SCAR=16.4
170 72 my ($text) = @_;
171 72 my ( $word, $rest ) = $text =~ m/\A(\S+)\s*(.*)\z/;
172 72 ▶ return ( undef, q{} ) if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 72 72
173 72 my $type = type_named($word);
174 72 ▶ $type //= InstanceOf [$word] if _is_package($word);
Branch: 1 branch
BranchTrueFalseTotal
if _is_package($word) 2 70 72
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
175 72 ▶ return ( undef, $text =~ s/\A[-:\s]+//r ) if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 9 63 72
176 63 return ( $type, $rest =~ s/\A[-:\s]+//r );
177 }
178  
179 # Whether WORD names a loaded package, as a class name in place of a type means an instance of that class.
180 72 ▶sub _is_package {
Subroutine _is_package: called CC=4 SCAR=13.9
181 72 my ($word) = @_;
182 72 ▶ return 0 if $word !~ m/\A[[:upper:]]\w*(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /\A[[:upper:]]\w*(?:::\w+)*\z/aasx 10 62 72
183 62 ▶ return 1 if index( $word, q{::} ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($word, "::") != -1 1 61 62
184 61 ▶ return exists $main::{"${word}::"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $main::main::{"${word}::"} ? : 1 60 61
185 }
186  
187 7 1;
188  
189 __END__
« lib/Pod/Coverage/More/Declared.pm ↑ Summary lib/Pod/Coverage/More/Declared/ObjectPad.pm »