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
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitFixedStringMatches.pm ↑ Summary 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.156';
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 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
27 return (
28 {
29 91 name => 'equivalent_modules',
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 85 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 91 ▶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 40 ▶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 37 ▶sub violates {
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
48 37 my ( $self, undef, $doc ) = @_;
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 = _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 37 ▶sub _generate_is_use_strict {
Subroutine _generate_is_use_strict: called CC=1 SCAR=1.5
77 37 my ($self) = @_;
78  
79 325 ▶ return sub {
Subroutine __ANON__: called CC=14 SCAR=39.2
80 325 my (undef, $elem) = @_;
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 37 ▶sub _find_isnt_include_or_package {
Subroutine _find_isnt_include_or_package: called CC=3 SCAR=11.0
118 37 my ($doc) = @_;
119 37 ▶ my $all_statements = $doc->find('PPI::Statement') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $all_statements = $doc->find("PPI::Statement") or return 35 2 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 306 ▶sub _statement_isnt_include_or_package {
Subroutine _statement_isnt_include_or_package: called CC=3 SCAR=11.0
127 306 my ($elem) = @_;
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__
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitFixedStringMatches.pm ↑ Summary lib/Perl/Critic/Utils/PPI.pm »