Pod/Coverage/More/Blurb.pm

filter: Statementstmt 98.3% 116 / 118 Branchbran 83.3% 40 / 48 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 91.7% 179 / 195 CC 29 SCAR 13.9
CC
29
Cov
92%
read_string
8.0
_type_and_description
5.2
_untagged
4.1
CRAP 4.0
« Pod/Coverage/More/Declared.pm ↑ Summary 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.001;
5  
6 6 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Types::Standard qw{InstanceOf};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶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 8 ▶sub read_file {
Subroutine read_file: called CC=3 SCAR=11.2
Pod documented
21 8 my ( $class, $file ) = @_;
22 8 ▶ open( my $fh, '<', $file ) or die "Cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot read ${file}: $!\n" 8 0 8
23 8 local $/;
24 8 my $source = <$fh>;
25 8 ▶ close($fh) or die "Cannot close $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file}: $!\n" 8 0 8
26 8 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 6 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 10 ▶sub read_string {
Subroutine read_string: called CC=8 SCAR=20.8
Pod documented
37 10 my ( $class, $source ) = @_;
38 10 my @lines = split m/\n/, $source;
39 10 my %blurbs;
40 my @comment;
41 10 my $in_pod = 0;
42 10 foreach my $index ( 0 .. $#lines ) {
43 560 my $line = $lines[$index];
44 560 ▶ last if $line =~ m/\A__(?:END|DATA)__\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A__(?:END|DATA)__\b/aasx 1 559 560
45 559 ▶ if ( $line =~ m/\A=(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A=(\w+)/aasx) 23 536 559
46 23 $in_pod = $1 ne 'cut';
47 23 @comment = ();
48 23 next;
49 }
50 536 ▶ next if $in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod 60 476 536
51 476 ▶ if ( $line =~ m/\A\s*\#(?!\#)[ ]?(.*)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\#(?!\#)[ ]?(.*)\z/aasx) 154 322 476
52 154 push @comment, $1;
53 154 next;
54 }
55 322 ▶ if ( my ($name) = $line =~ $SUB_LINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $line =~ /$Pod::Coverage::More::Blurb::SUB_LINE/aasx) 56 266 322
56 56 ▶ $blurbs{$name} = { %{ _parse(@comment) }, line => $index + 1 } if @comment;
Branch: 1 branch
BranchTrueFalseTotal
if @comment 40 16 56
57 }
58 322 @comment = ();
59 }
60 10 return \%blurbs;
61 }
62  
63 # The handlers of the tags, which each record what the rest of a tagged line says.
64 6 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 40 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
75 40 my (@lines) = @_;
76 40 my $state = {
77 blurb => { description => [], params => [], returns => undef, throws => [], examples => [] },
78 paragraph => q{},
79 current => undef,
80 last_tag => q{},
81 };
82 40 foreach my $line (@lines) {
83 138 my ( $tag, $rest ) = $line =~ m/\A\s*\@(\w+)\b\s*(.*)\z/;
84 138 ▶ my $handler = defined $tag ? $TAG_HANDLERS{ lc $tag } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 93 45 138
85 138 ▶ if ( !$handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 45 93 138
86 45 _untagged( $state, $line );
87 45 next;
88 }
89 93 $handler->( $state, $rest );
90 93 $state->{last_tag} = lc $tag;
91 }
92 40 _end_paragraph($state);
93 40 my $blurb = $state->{blurb};
94 40 $blurb->{throws} = [ map { $_->{description} } @{ $blurb->{throws} } ];
95 40 return $blurb;
96 }
97  
98 # A line without a tag continues the tag above it, or else the description.
99 45 ▶sub _untagged {
Subroutine _untagged: called CC=4 SCAR=14.0
100 45 my ( $state, $line ) = @_;
101 45 $state->{last_tag} = q{};
102 45 ▶ if ( my $current = $state->{current} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = $state->{"current"}) 2 43 45
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 43 ▶ if ( $line =~ m/\A\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*\z/aasx) 1 42 43
108 1 _end_paragraph($state);
109 1 return;
110 }
111 42 $state->{paragraph} = _join( $state->{paragraph}, $line );
112 42 return;
113 }
114  
115 41 ▶sub _end_paragraph {
Subroutine _end_paragraph: called CC=2 SCAR=7.0
116 41 my ($state) = @_;
117 41 ▶ push @{ $state->{blurb}{description} }, $state->{paragraph} if $state->{paragraph} ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"paragraph"} ne "" 41 0 41
118 41 $state->{paragraph} = q{};
119 41 return;
120 }
121  
122 39 ▶sub _tag_param {
Subroutine _tag_param: called CC=2 SCAR=7.0
123 39 my ( $state, $rest ) = @_;
124 39 my ( $name, $after ) = $rest =~ m/\A(\S+)\s*(.*)\z/;
125 39 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 39 39
126 39 my ( $type, $description ) = _type_and_description($after);
127 39 $state->{current} = { name => $name, type => $type, description => $description };
128 39 push @{ $state->{blurb}{params} }, $state->{current};
129 39 return;
130 }
131  
132 8 ▶sub _tag_returns {
Subroutine _tag_returns: called CC=1 SCAR=0.0
133 8 my ( $state, $rest ) = @_;
134 8 my ( $type, $description ) = _type_and_description($rest);
135 8 $state->{current} = $state->{blurb}{returns} = { type => $type, description => $description };
136 8 return;
137 }
138  
139 8 ▶sub _tag_throws {
Subroutine _tag_throws: called CC=1 SCAR=0.0
140 8 my ( $state, $rest ) = @_;
141 8 $state->{current} = { description => $rest };
142 8 push @{ $state->{blurb}{throws} }, $state->{current};
143 8 return;
144 }
145  
146 # Consecutive @example lines make one example.
147 38 ▶sub _tag_example {
Subroutine _tag_example: called CC=3 SCAR=11.0
148 38 my ( $state, $rest ) = @_;
149 38 my $examples = $state->{blurb}{examples};
150 38 ▶ if ( $state->{last_tag} eq 'example' && @$examples ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'last_tag'} eq 'example' and @$examples) { } 1 37 38
Condition66%2/3
$state->{"last_tag"} eq "example" and @$examples
!ll&&!rl&&r
3701
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 37 push @$examples, [$rest];
155 }
156 38 $state->{current} = { example => $examples->[-1] };
157 38 return;
158 }
159  
160 44 ▶sub _join {
Subroutine _join: called CC=3 SCAR=11.0
161 44 my ( $text, $more ) = @_;
162 44 $more =~ s/\A\s+|\s+\z//g;
163 44 ▶ return $more if $text eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 41 3 44
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 47 ▶sub _type_and_description {
Subroutine _type_and_description: called CC=5 SCAR=16.4
170 47 my ($text) = @_;
171 47 my ( $word, $rest ) = $text =~ m/\A(\S+)\s*(.*)\z/;
172 47 ▶ return ( undef, q{} ) if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 47 47
173 47 my $type = type_named($word);
174 47 ▶ $type //= InstanceOf [$word] if _is_package($word);
Branch: 1 branch
BranchTrueFalseTotal
if _is_package($word) 1 46 47
Condition33%1/3
$type //= &InstanceOf([$word])
l!l&&r!l&&!r
010
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 47 ▶ return ( undef, $text =~ s/\A[-:\s]+//r ) if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 46 47
176 46 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 47 ▶sub _is_package {
Subroutine _is_package: called CC=4 SCAR=13.9
181 47 my ($word) = @_;
182 47 ▶ return 0 if $word !~ m/\A[[:upper:]]\w*(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /\A[[:upper:]]\w*(?:::\w+)*\z/aasx 2 45 47
183 45 ▶ return 1 if index( $word, q{::} ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($word, "::") != -1 1 44 45
184 44 ▶ return exists $main::{"${word}::"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $main::main::{"${word}::"} ? : 0 44 44
185 }
186  
187 6 1;
188  
189 __END__
« Pod/Coverage/More/Declared.pm ↑ Summary Pod/Coverage/More/Declared/ObjectPad.pm »