lib/Perl/Critic/Policy/Modules/RequireExplicitPackage.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 80.0% 16 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 14 / 14 Podpod 85.7% 6 / 7 totaltotal 92.1% 94 / 102 CC 14 SCAR 14.6
CC
14
Cov
92%
_is_statement_of_interest
7.4
violates
7.0
prepare_to_scan_document
2.0
CRAP 4.3
« lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm »
1 package Perl::Critic::Policy::Modules::RequireExplicitPackage;
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 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :booleans :severities :classification };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.156';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $EXPL => q{Violates encapsulation};
16 40 Readonly::Scalar my $DESC => q{Code not contained in explicit package};
17  
18 #-----------------------------------------------------------------------------
19  
20 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
21 return (
22 {
23 92 name => 'exempt_scripts',
24 description => q{Don't require programs to contain a package statement.},
25 default_string => '1',
26 behavior => 'boolean',
27 },
28 {
29 name => 'allow_import_of',
30 description => q{Allow the specified modules to be imported outside a package},
31 behavior => 'string list',
32 },
33 );
34 }
35  
36 79 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
37 73 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
38 32 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
39  
40 35 ▶sub default_maximum_violations_per_document { return 1; }
Subroutine default_maximum_violations_per_document: called CC=1 SCAR=0.0
Pod documented
41  
42 #-----------------------------------------------------------------------------
43  
44 33 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=2 SCAR=7.1
Pod documented
45 33 my ( $self, $document ) = @_;
46  
47 33 ▶ return ! $self->{_exempt_scripts} || $document->is_module();
Condition66%2/3
!$self->{'_exempt_scripts'} || $document->is_module
l!l&&r!l&&!r
0321
MC/DC50%1/2
!$self->{'_exempt_scripts'} || $document->is_module
!$self->{'_exempt_scripts'} $document->is_module
Truth table66%2/3
not(A) || B
A= !$self->{'_exempt_scripts'}B= $document->is_module
ABresult
1X1
011
000
48 }
49  
50 32 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.5
Pod documented
51 32 my ( $self, undef, $doc ) = @_;
52  
53 # Find the first 'package' statement
54 32 my $package_stmnt = $doc->find_first( 'PPI::Statement::Package' );
55 32 ▶ my $package_line = $package_stmnt ? $package_stmnt->location()->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$package_stmnt ? : 29 3 32
56  
57 # Find all statements that aren't 'package' statements
58 32 my $stmnts_ref = $doc->find( 'PPI::Statement' );
59 32 ▶ return if !$stmnts_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnts_ref 2 30 32
60 my @non_packages = grep {
61 291 $self->_is_statement_of_interest( $_ )
62 30 } @{$stmnts_ref};
63 30 ▶ return if !@non_packages;
Branch: 1 branch
BranchTrueFalseTotal
unless @non_packages 0 30 30
64  
65 # If the 'package' statement is not defined, or the other
66 # statements appear before the 'package', then it violates.
67  
68 30 my @viols;
69 30 for my $stmnt ( @non_packages ) {
70 262 my $stmnt_line = $stmnt->location()->[0];
71 262 ▶ if ( (! defined $package_line) || ($stmnt_line < $package_line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $package_line or $stmnt_line < $package_line) 5 257 262
Condition100%3/3
not defined $package_line or $stmnt_line < $package_line
l!l&&r!l&&!r
23257
MC/DC100%2/2
not defined $package_line or $stmnt_line < $package_line
not defined $package_line $stmnt_line < $package_line
Truth table100%3/3
not(A) or B
A= not defined $package_lineB= $stmnt_line < $package_line
ABresult
1X1
011
000
72 5 push @viols, $self->violation( $DESC, $EXPL, $stmnt );
73 }
74 }
75  
76 30 return @viols;
77 }
78  
79 291 ▶sub _is_statement_of_interest {
Subroutine _is_statement_of_interest: called CC=7 SCAR=20.0
80 291 my ( $self, $elem ) = @_;
81  
82 291 ▶ $elem
Branch: 1 branch
BranchTrueFalseTotal
$elem or return $Perl::Critic::Policy::Modules::RequireExplicitPackage::FALSE 291 0 291
83 or return $FALSE;
84  
85 291 ▶ $elem->isa( 'PPI::Statement::Package' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Package") and return $Perl::Critic::Policy::Modules::RequireExplicitPackage::FALSE 29 262 291
86 and return $FALSE;
87  
88 262 ▶ if ( $elem->isa( 'PPI::Statement::Include' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Include")) 59 203 262
89 59 ▶ if ( my $module = $elem->module() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $module = $elem->module) { } 57 2 59
elsif ($elem->version) { } 0 2 2
90 57 ▶ $self->{_allow_import_of}{$module}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_allow_import_of"}{$module} and return $Perl::Critic::Policy::Modules::RequireExplicitPackage::FALSE 0 57 57
91 and return $FALSE;
92 }
93 elsif ( $elem->version ) {
94 0 return $FALSE;
95 }
96 }
97  
98 262 return $TRUE;
99 }
100  
101 40 1;
102  
103 __END__
« lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm »