blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm

filter: Statementstmt 93.4% 71 / 76 Branchbran 60.0% 24 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 19 / 19 Podpod 83.3% 5 / 6 totaltotal 79.4% 124 / 156 CC 25 SCAR 29.3
CC
25
Cov
78%
__ANON__
50.6
violates
8.0
_find_isnt_include_or_package
3.0
CRAP 18.7
« blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitFixedStringMatches.pm ↑ Summary blib/lib/Perl/Critic/Utils/PPI.pm »
1 package Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Scalar::Util qw{ blessed };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Perl::Critic::Utils qw{ :severities $EMPTY };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Perl::Critic::Utils::Constants qw{ :equivalent_modules };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 our $VERSION = '1.150';
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $DESC => q{Code before strictures are enabled};
20 40 Readonly::Scalar my $EXPL => [ 429 ];
21  
22 40 Readonly::Scalar my $PERL_VERSION_WHICH_IMPLIES_STRICTURE => qv('v5.11.0');
23  
24 #-----------------------------------------------------------------------------
25  
26 sub supported_parameters {
27 return (
28 {
29 91 ▶ name => 'equivalent_modules',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
30 description =>
31 q<The additional modules to treat as equivalent to "strict".>,
32 default_string => $EMPTY,
33 behavior => 'string list',
34 list_always_present_values => ['strict', @STRICT_EQUIVALENT_MODULES],
35 },
36 );
37 }
38  
39 86 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 92 ▶sub default_themes { return qw( core pbp bugs certrule certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
41 37 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42  
43 41 ▶sub default_maximum_violations_per_document { return 1; }
Subroutine default_maximum_violations_per_document: called CC=1 SCAR=0.0
Pod documented
44  
45 #-----------------------------------------------------------------------------
46  
47 sub violates {
48 37 ▶ my ( $self, undef, $doc ) = @_;
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
49  
50 # Find the first 'use strict' statement
51 37 my $strict_stmnt = $doc->find_first( $self->_generate_is_use_strict() );
52 37 ▶ my $strict_line = $strict_stmnt ? $strict_stmnt->location()->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$strict_stmnt ? : 30 7 37
53  
54 # Find all statements that aren't 'use', 'require', or 'package'
55 37 my $stmnts_ref = $self->_find_isnt_include_or_package($doc);
56 37 ▶ return if not $stmnts_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnts_ref 2 35 37
57  
58 # If the 'use strict' statement is not defined, or the other
59 # statement appears before the 'use strict', then it violates.
60  
61 35 my @viols;
62 35 for my $stmnt ( @{ $stmnts_ref } ) {
63 212 ▶ last if $stmnt->isa('PPI::Statement::End');
Branch: 1 branch
BranchTrueFalseTotal
if $stmnt->isa("PPI::Statement::End") 0 212 212
64 212 ▶ last if $stmnt->isa('PPI::Statement::Data');
Branch: 1 branch
BranchTrueFalseTotal
if $stmnt->isa("PPI::Statement::Data") 0 212 212
65  
66 212 my $stmnt_line = $stmnt->location()->[0];
67 212 ▶ if ( (! defined $strict_line) || ($stmnt_line < $strict_line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $strict_line or $stmnt_line < $strict_line) 11 201 212
Condition100%3/3
not defined $strict_line or $stmnt_line < $strict_line
l!l&&r!l&&!r
92201
MC/DC100%2/2
not defined $strict_line or $stmnt_line < $strict_line
not defined $strict_line $stmnt_line < $strict_line
Truth table100%3/3
not(A) or B
A= not defined $strict_lineB= $stmnt_line < $strict_line
ABresult
1X1
011
000
68 11 push @viols, $self->violation( $DESC, $EXPL, $stmnt );
69 }
70 }
71 35 return @viols;
72 }
73  
74 #-----------------------------------------------------------------------------
75  
76 sub _generate_is_use_strict {
77 37 ▶ my ($self) = @_;
Subroutine _generate_is_use_strict: called CC=1 SCAR=1.5
78  
79 return sub {
80 325 ▶ my (undef, $elem) = @_;
Subroutine __ANON__: called CC=14 SCAR=39.2
81  
82 325 ▶ return 0 if !$elem->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Statement::Include") 294 31 325
83 31 ▶ return 0 if $elem->type() ne 'use';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "use" 0 31 31
84  
85 # We only want file-scoped pragmas
86 31 my $parent = $elem->parent();
87 31 ▶ return 0 if !$parent->isa('PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Document") 0 31 31
88  
89 31 ▶ if ( my $pragma = $elem->pragma() ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $pragma = $elem->pragma) { } 30 1 31
elsif (my $module = $elem->module) { } 1 0 1
elsif (my $version = $elem->version) { } 0 0 0
90 30 ▶ return 1 if $self->{_equivalent_modules}{$pragma};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_equivalent_modules"}{$pragma} 30 0 30
91 }
92 elsif ( my $module = $elem->module() ) {
93 1 ▶ return 1 if $self->{_equivalent_modules}{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_equivalent_modules"}{$module} 0 1 1
94 }
95 elsif ( my $version = $elem->version() ) {
96 # Currently Adam returns a string here. He has said he may return
97 # a version object in the future, so best be prepared.
98 0 ▶ if ( not blessed( $version ) or not $version->isa( 'version' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($version) and $version->isa("version")) 0 0 0
Condition0%0/3
builtin::blessed($version) and $version->isa("version")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($version) and $version->isa("version")
builtin::blessed($version) $version->isa("version")
Truth table0%0/3
A and B
A= builtin::blessed($version)B= $version->isa("version")
ABresult
0X0
100
111
99 0 ▶ if ( 'v' ne substr $version, 0, 1
Branch: 1 branch
BranchTrueFalseTotal
if ("v" ne substr($version, 0, 1) and $version =~ tr/.// > 1) 0 0 0
Condition0%0/3
"v" ne substr($version, 0, 1) and $version =~ tr/.// > 1
!ll&&!rl&&r
000
MC/DC0%0/2
"v" ne substr($version, 0, 1) and $version =~ tr/.// > 1
"v" ne substr($version, 0, 1) $version =~ tr/.// > 1
Truth table0%0/3
A and B
A= "v" ne substr($version, 0, 1)B= $version =~ tr/.// > 1
ABresult
0X0
100
111
100 and ( $version =~ tr/././ ) > 1 ) {
101 0 $version = 'v' . $version;
102 }
103 0 $version = version->parse( $version );
104 }
105 0 ▶ return 1 if $PERL_VERSION_WHICH_IMPLIES_STRICTURE <= $version;
Branch: 1 branch
BranchTrueFalseTotal
if $PERL_VERSION_WHICH_IMPLIES_STRICTURE <= $version 0 0 0
106 }
107  
108 1 return 0;
109 37 };
110 }
111  
112 #-----------------------------------------------------------------------------
113 # Here, we're using the fact that Perl::Critic::Document::find() is optimized
114 # to search for elements based on their type. This is faster than using the
115 # native PPI::Node::find() method with a custom callback function.
116  
117 sub _find_isnt_include_or_package {
118 37 ▶ my ($self, $doc) = @_;
Subroutine _find_isnt_include_or_package: called CC=3 SCAR=11.0
119 37 ▶ my $all_statements = $doc->find('PPI::Statement') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $all_statements = $doc->find("PPI::Statement") or return 2 35 37
120 35 my @wanted_statements = grep { _statement_isnt_include_or_package($_) } @{$all_statements};
121 35 ▶ return @wanted_statements ? \@wanted_statements : ();
Branch: 1 branch
BranchTrueFalseTotal
@wanted_statements ? : 35 0 35
122 }
123  
124 #-----------------------------------------------------------------------------
125  
126 sub _statement_isnt_include_or_package {
127 306 ▶ my ($elem) = @_;
Subroutine _statement_isnt_include_or_package: called CC=3 SCAR=11.0
128 306 ▶ return 0 if $elem->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Package") 31 275 306
129 275 ▶ return 0 if $elem->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Include") 63 212 275
130 212 return 1;
131 }
132  
133 40 1;
134  
135 __END__
136  
137 #-----------------------------------------------------------------------------
138  
139 =pod
140  
141 =head1 NAME
142  
143 Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict - Always C<use strict>.
144  
145  
146 =head1 AFFILIATION
147  
148 This Policy is part of the core L<Perl::Critic|Perl::Critic>
149 distribution.
150  
151  
152 =head1 DESCRIPTION
153  
154 Using strictures is probably the single most effective way to improve
155 the quality of your code. This policy requires that the C<'use
156 strict'> statement must come before any other statements except
157 C<package>, C<require>, and other C<use> statements. Thus, all the
158 code in the entire package will be affected.
159  
160 There are special exemptions for L<Moose|Moose>,
161 L<Moose::Role|Moose::Role>, and
162 L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints> because
163 they enforces strictness; e.g. C<'use Moose'> is treated as
164 equivalent to C<'use strict'>.
165  
166 The maximum number of violations per document for this policy defaults
167 to 1.
168  
169  
170 =head1 CONFIGURATION
171  
172 If you make use of things like
173 L<Moose::Exporter|Moose::Exporter>, you can create your own modules
174 that import the L<strict|strict> pragma into the code that is
175 C<use>ing them. There is an option to add to the default set of
176 pragmata and modules in your F<.perlcriticrc>: C<equivalent_modules>.
177  
178 [TestingAndDebugging::RequireUseStrict]
179 equivalent_modules = MooseX::My::Sugar
180  
181  
182 =head1 SEE ALSO
183  
184 L<Perl::Critic::Policy::TestingAndDebugging::ProhibitNoStrict|Perl::Critic::Policy::TestingAndDebugging::ProhibitNoStrict>
185  
186  
187 =head1 AUTHOR
188  
189 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
190  
191  
192 =head1 COPYRIGHT
193  
194 Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
195  
196 This program is free software; you can redistribute it and/or modify it under
197 the same terms as Perl itself. The full text of this license can be found in
198 the LICENSE file included with this module
199  
200 =cut
201  
202 ##############################################################################
203 # Local Variables:
204 # mode: cperl
205 # cperl-indent-level: 4
206 # fill-column: 78
207 # indent-tabs-mode: nil
208 # c-indentation-style: bsd
209 # End:
210 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitFixedStringMatches.pm ↑ Summary blib/lib/Perl/Critic/Utils/PPI.pm »