Policy/Documentation/RequireLinkedURLs.pm

filter: Statementstmt 100% 75 / 75 Branchbran 93.7% 15 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 95.8% 114 / 119 CC 11 SCAR 13.6
CC
11
Cov
97%
textblock
9.0
violates
3.0
BEGIN
1.0
CRAP 3.9
« Policy/Documentation/ProhibitParagraphEndComma.pm ↑ Summary Policy/CodeLayout/ProhibitIfIfSameLine.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # This file is part of Perl-Critic-Pulp.
4  
5 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Perl::Critic::Policy::Documentation::RequireLinkedURLs;
19 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # uncomment this to run the ### lines
27 # use Smart::Comments;
28  
29 # perlcritic -s RequireLinkedURLs RequireLinkedURLs.pm
30 # perlcritic -s RequireLinkedURLs /usr/share/perl5/AnyEvent/HTTP.pm
31 # perlcritic -s RequireLinkedURLs /usr/share/perl5/SVG/Rasterize.pm
32  
33 40 our $VERSION = 100;
34  
35 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 40 my $want_perl = version->new('5.008');
41  
42 39 ▶sub violates {
Subroutine violates: called CC=3 SCAR=11.0
Pod documented
43 39 my ($self, $elem, $document) = @_;
44 ### RequireLinkedURLs violates() ...
45  
46 39 my $got_perl = $document->highest_explicit_perl_version;
47 ### highest_explicit_perl_version: defined $got_perl && "$got_perl"
48 39 ▶ if (! $got_perl # undef no use 5.x at all
Branch: 1 branch
BranchTrueFalseTotal
if (not $got_perl or $want_perl > $got_perl) 4 35 39
Condition66%2/3
not $got_perl or $want_perl > $got_perl
l!l&&r!l&&!r
4035
MC/DC50%1/2
not $got_perl or $want_perl > $got_perl
not $got_perl $want_perl > $got_perl
Truth table66%2/3
not(A) or B
A= not $got_perlB= $want_perl > $got_perl
ABresult
1X1
011
000
49 || $want_perl > $got_perl) { # use 5.x too low
50 ### no use 5.008 up, or too low
51 4 return;
52 }
53  
54 35 my $parser = Perl::Critic::Pulp::PodParser::RequireLinkedURLs->new
55 (policy => $self);
56 35 $parser->parse_from_elem ($elem);
57 35 return $parser->violations;
58 }
59  
60 package Perl::Critic::Pulp::PodParser::RequireLinkedURLs;
61 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 41 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
66 41 my $self = shift;
67 41 $self->SUPER::command(@_);
68 41 $self->command_as_textblock(@_);
69 41 return '';
70 }
71  
72 64 ▶sub textblock {
Subroutine textblock: called CC=9 SCAR=22.0
73 64 my ($self, $text, $linenum, $paraobj) = @_;
74 ### textblock ...
75  
76 # process outside =begin, and inside =begin which is ":" markup
77 64 ▶ unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/) 6 58 64
Condition66%2/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
l!l&&r!l&&!r
5806
MC/DC50%1/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq "" $self->{"in_begin"} =~ /^:/
Truth table66%2/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
ABresult
1X1
011
000
78 6 return '';
79 }
80  
81 58 my $expand = $self->interpolate ($text, $linenum);
82  
83 58 my $ptree = $self->parse_text ($text, $linenum);
84 58 my @pending = reverse $ptree->children; # depth first by pop()
85 58 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 77 58 135
86 77 my $obj = pop @pending;
87 77 ▶ if (! ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $obj) { } 65 12 77
88 # plain text
89 # 12 3
90 65 ▶ while ($obj =~ m{(?<!L<)\b((https?|s?ftp|news|nntp)://(\S+))}g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj =~ m[(?<!L<)\b((https?|s?ftp|news|nntp)://(\S+))]g) 30 65 95
91 30 my $pos = pos($obj) - length($1);
92 30 my $part = $3;
93 30 ▶ next if _is_bogus_part($part);
Branch: 1 branch
BranchTrueFalseTotal
if _is_bogus_part($part) 6 24 30
94  
95 24 $self->violation_at_linenum_and_textpos
96 ("URL can helpfully have L<> link markup",
97 $linenum, $obj, $pos);
98 }
99  
100 } else {
101 # a Pod::InteriorSequence
102 12 (undef, $linenum) = $obj->file_line;
103 12 my $cmd = $obj->cmd_name;
104  
105 12 ▶ if ($cmd eq 'L') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'L') { } 3 9 12
106 3 next;
107  
108 } else {
109 # descend into other like C<>
110 # X<> is included, since markup is allowed in it, and maybe even L<>
111 # to make hyperlinks in the index as such
112 # Z<> is included, though it should normally be empty
113 9 ▶ if (my $subtree = $obj->parse_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subtree = $obj->parse_tree) 9 0 9
114 9 push @pending, reverse $subtree->children; # depth first by pop()
115 }
116 }
117 }
118 }
119 58 return '';
120 }
121  
122 30 ▶sub _is_bogus_part {
Subroutine _is_bogus_part: called CC=1 SCAR=0.0
123 30 my ($part) = @_;
124 ### _is_bogus_part(): $part
125 30 return scalar ($part =~ m{^(
126 (foo|bar|quux|xyzzy|example)
127 \.(org|com|co\.[a-z]+)
128 (\.[a-z.]*)?
129 |
130 host(name)?[:/]
131 |
132 \.\. # ellipsis like http://...
133 )}xi);
134 }
135  
136 40 1;
137 __END__
« Policy/Documentation/ProhibitParagraphEndComma.pm ↑ Summary Policy/CodeLayout/ProhibitIfIfSameLine.pm »