File Coverage

blib/lib/Perl/Critic/Policy/Compatibility/PodMinimumVersion.pm
Criterion Covered Total %
statement 59 59 100.0
branch 5 6 83.3
condition 6 7 85.7
subroutine 15 15 100.0
pod 2 2 100.0
total 87 89 97.7


line stmt bran cond sub pod time code
1             # Copyright 2009, 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              
19             package Perl::Critic::Policy::Compatibility::PodMinimumVersion;
20 40     40   412658 use 5.006;
  40         202  
21 40     40   239 use strict;
  40         89  
  40         1040  
22 40     40   191 use warnings;
  40         75  
  40         2399  
23              
24             # 1.084 for Perl::Critic::Document highest_explicit_perl_version()
25 40     40   905 use Perl::Critic::Policy 1.084;
  40         205821  
  40         1346  
26 40     40   209 use base 'Perl::Critic::Policy';
  40         75  
  40         5788  
27 40     40   308 use Perl::Critic::Utils;
  40         84  
  40         825  
28 40     40   42199 use Perl::Critic::Pulp::Utils;
  40         101  
  40         3986  
29              
30             # uncomment this to run the ### lines
31             # use Smart::Comments;
32              
33             our $VERSION = 100;
34              
35 40         3335 use constant supported_parameters =>
36             ({ name => 'above_version',
37             description => 'Check only things above this version of Perl.',
38             behavior => 'string',
39             parser => \&Perl::Critic::Pulp::Utils::parameter_parse_version,
40 40     40   301 });
  40         106  
41 40     40   275 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         88  
  40         2584  
42 40     40   223 use constant default_themes => qw(pulp compatibility);
  40         86  
  40         2833  
43 40     40   238 use constant applies_to => 'PPI::Document';
  40         87  
  40         17586  
44              
45              
46             # but actually Pod::MinimumVersion is a hard dependency at the moment ...
47             sub initialize_if_enabled {
48 1     1 1 475392 my ($self, $config) = @_;
49             # when Pod::MinimumVersion is available
50 1   50     3 return (eval { require Pod::MinimumVersion; 1 } || 0);
51             }
52              
53             sub violates {
54 19     19 1 103795 my ($self, $document) = @_;
55             ### $self
56              
57             # whichever of highest_explicit_perl_version() or "above_version" is greater
58 19         204 my $above_version = $self->{'_above_version'};
59 19 100       105 if (defined (my $doc_version = $document->highest_explicit_perl_version)) {
60 7 100 100     2747 if (! defined $above_version || $doc_version > $above_version) {
61 4         13 $above_version = $doc_version;
62             }
63             }
64              
65 19         1063 my $str = $document->serialize;
66 19         2163 my $pmv = Pod::MinimumVersion->new (string => $str,
67             above_version => $above_version,
68             one_report_per_version => 1,
69             );
70 19         435 my @reports = $pmv->reports;
71 19         25925 @reports = sort {$a->{'version'} <=> $b->{'version'}} @reports;
  1         40  
72             return map {
73 19         104 my $report = $_;
  11         25  
74 11         372 my $violation = $self->violation
75             ("Pod requires perl $report->{'version'} due to: $report->{'why'}.",
76             '',
77             $document);
78             Perl::Critic::Pulp::Utils::_violation_override_linenum
79 11         4529 ($violation, $str, $report->{'linenum'});
80              
81             } @reports;
82             }
83              
84             package Perl::Critic::Pulp::PodMinimumVersionViolation;
85 40     40   319 use base 'Perl::Critic::Violation';
  40         87  
  40         9725  
86             sub location {
87 637     637   99480 my ($self) = @_;
88 637   100     2010 my $offset = ($self->{_Pulp_linenum_offset} || 0);
89              
90 637         1104 my @location = @{$self->SUPER::location()};
  637         2228  
91 637         4237 $location[0] += $offset; # line
92 637 50       1814 if ($#location >= 3) {
93 637         1166 $location[3] += $offset; # logical line, new in ppi 1.205
94             }
95 637         1879 return \@location;
96             }
97              
98             1;
99             __END__
100              
101             =for stopwords CPAN config Ryde
102              
103             =head1 NAME
104              
105             Perl::Critic::Policy::Compatibility::PodMinimumVersion - check Perl version declared against POD features used
106              
107             =head1 DESCRIPTION
108              
109             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
110             add-on. It checks that the POD features you use don't exceed your target
111             Perl version as indicated by C<use 5.008> etc.
112              
113             =for ProhibitVerbatimMarkup allow next 3
114              
115             use 5.005;
116              
117             =pod
118              
119             C<< something >> # bad, double angles needs 5.006
120              
121             POD doesn't affect how the code runs, so this policy is low severity, and
122             under the "compatibility" theme (see L<Perl::Critic/POLICY THEMES>).
123              
124             See L<C<Pod::MinimumVersion>|Pod::MinimumVersion> for the POD version checks
125             applied. The key idea is for example when targeting Perl 5.005 you avoid
126             things like double-angles S<C<CE<lt>E<lt> E<gt>E<gt>>>, since C<pod2man> in
127             5.005 didn't support them. It may be possible to get newer versions of the
128             POD translators from CPAN, but whether they run on an older Perl and whether
129             you want to require that of users is another matter.
130              
131             Adding the sort of C<use 5.006> etc to declare a target Perl can be a bit
132             tedious. The config option below lets you set a base version you use. As
133             always if you don't care at all about this sort of thing you can disable the
134             policy from your F<.perlcriticrc> in the usual way (see
135             L<Perl::Critic/CONFIGURATION>),
136              
137             [-Compatibility::PodMinimumVersion]
138              
139             =head2 C<RequirePodLinksIncludeText> Policy
140              
141             The C<Documentation::RequirePodLinksIncludeText> policy asks you to use the
142             C<LE<lt>target|displayE<gt>> style always. That feature is new in Perl
143             5.005 and will be reported by C<PodMinimumVersion> unless you've got C<use
144             5.005> or higher or set C<above_version> below.
145              
146             =head1 CONFIGURATION
147              
148             =over 4
149              
150             =item C<above_version> (version string, default none)
151              
152             Report only things about Perl versions above this. The string is anything
153             the L<C<version.pm>|version> module understands. For example if you always
154             use Perl 5.6 or higher then set
155              
156             [Compatibility::PodMinimumVersion]
157             above_version = 5.006
158              
159             The effect is that all POD features up to and including Perl 5.6 are
160             allowed, only things above that will be reported (and still only those
161             exceeding any C<use 5.xxx> in the file).
162              
163             =back
164              
165             =head1 SEE ALSO
166              
167             L<Perl::Critic::Pulp>, L<Pod::MinimumVersion>, L<Perl::Critic>
168              
169             L<Perl::Critic::Policy::Documentation::RequirePodLinksIncludeText>,
170             L<Perl::Critic::Policy::Compatibility::PerlMinimumVersionAndWhy>,
171             L<Perl::Critic::Policy::Modules::PerlMinimumVersion>
172              
173             =head1 HOME PAGE
174              
175             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
176              
177             =head1 COPYRIGHT
178              
179             Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
180              
181             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
182             under the terms of the GNU General Public License as published by the Free
183             Software Foundation; either version 3, or (at your option) any later
184             version.
185              
186             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
187             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
188             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
189             more details.
190              
191             You should have received a copy of the GNU General Public License along with
192             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
193              
194             =cut