blib/lib/Perl/Critic/Policy/logicLAB/ModuleBlacklist.pm

filter: Statementstmt 92.6% 63 / 68 Branchbran 70.0% 14 / 20 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 85.5% 101 / 118 CC 16 SCAR 17.2
CC
16
Cov
85%
violates
11.1
initialize_if_enabled
6.1
_parse_modules
3.0
CRAP 5.6
↑ Summary
1 package Perl::Critic::Policy::logicLAB::ModuleBlacklist;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.04';
13  
14 3 ▶use constant supported_parameters => qw(modules debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant default_severity => $SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use constant default_themes => qw(logiclab);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
19 return (
20 9 qw(
21 PPI::Statement::Include
22 )
23 );
24 }
25  
26 17 ▶sub violates {
Subroutine violates: called CC=10 SCAR=24.1
Pod documented
27 17 my ( $self, $elem ) = @_;
28  
29 #Policy not configured, nothing to assert
30 17 ▶ if ( not $self->{_modules} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_modules"}) 0 17 17
31 0 return;
32 }
33  
34 17 my @children = $elem->children;
35  
36 17 ▶ if ( $children[0]->content eq 'use' or $children[0]->content eq 'require' )
Branch: 2 branches
BranchTrueFalseTotal
if ($children[0]->content eq 'use' or $children[0]->content eq 'require') { } 16 1 17
elsif ($children[0]->content eq 'no') { } 1 0 1
Condition100%3/3
$children[0]->content eq "use" or $children[0]->content eq "require"
l!l&&r!l&&!r
1151
MC/DC100%2/2
$children[0]->content eq "use" or $children[0]->content eq "require"
$children[0]->content eq "use" $children[0]->content eq "require"
Truth table100%3/3
A or B
A= $children[0]->content eq "use"B= $children[0]->content eq "require"
ABresult
1X1
011
000
37 {
38  
39 16 my $package = $children[2]->content;
40  
41 16 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 16 16
42 0 print STDERR "located include: $package\n";
43 }
44  
45 16 foreach my $module ( keys %{ $self->{_modules} } ) {
46  
47 40 ▶ if ( $package eq $module ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq $module) 8 32 40
48  
49 8 ▶ if ( defined $self->{_modules}->{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_modules'}{$module}) { } 4 4 8
50 4 my $recommendation = $self->{_modules}->{$module};
51 4 return $self->violation(
52 "Blacklisted: $package is not recommended by required standard",
53 "Use recommended module: $recommendation instead of $package",
54 $elem,
55 );
56  
57 }
58 else {
59  
60 4 return $self->violation(
61 "Blacklisted: $package is not recommended by required standard",
62 "Use alternative implementation or module instead of $package",
63 $elem,
64 );
65 }
66 }
67 }
68  
69 #we ignore negative use statements, they are for pragma [issue1]
70 }
71 elsif ( $children[0]->content eq 'no' ) {
72 1 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 1 1
73 0 print STDERR "located 'no' use/require statement\n";
74 }
75 }
76 else {
77 0 carp 'Unable to locate package keyword';
78 }
79  
80 9 return;
81 }
82  
83 1 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=5 SCAR=18.1
Pod documented
84 1 my ( $self, $config ) = @_;
85  
86 #debug - order is significant, since we might need debugging
87 1 ▶ $self->{debug} = $config->get('debug') || $FALSE;
Condition33%1/3
$config->get('debug') || $Perl::Critic::Policy::logicLAB::ModuleBlacklist::FALSE
l!l&&r!l&&!r
001
MC/DC0%0/2
$config->get('debug') || $Perl::Critic::Policy::logicLAB::ModuleBlacklist::FALSE
$config->get('debug') $Perl::Critic::Policy::logicLAB::ModuleBlacklist::FALSE
Truth table33%1/3
A || B
A= $config->get('debug')B= $Perl::Critic::Policy::logicLAB::ModuleBlacklist::FALSE
ABresult
1X1
011
000
88  
89 #Names:
90 #fetching list of blacklisted modules
91 1 my $modules = $config->get('modules');
92  
93 1 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 1 1
94 0 carp "Blacklisted modules are: $modules\n";
95 }
96  
97 #parsing blacklisted modules, see also _parse_blacklist
98 1 ▶ if ($modules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modules) 1 0 1
99 1 ▶ $self->{_modules} = $self->_parse_modules($modules) || q{};
Condition50%1/2
$self->_parse_modules($modules) || ''
l!l
10
MC/DC0%0/1
$self->_parse_modules($modules) || ''
$self->_parse_modules($modules)
Truth table50%1/2
A
A= $self->_parse_modules($modules)
Aresult
11
00
100 }
101  
102 1 return $TRUE;
103 }
104  
105 1 ▶sub _parse_modules {
Subroutine _parse_modules: called CC=3 SCAR=11.0
106 1 my ( $self, $config_string ) = @_;
107  
108 #first we split on commas
109 1 my @parameters = split /\s*,\s*/, $config_string;
110 1 my %modules;
111  
112 #then we split on fat commas, to locate recommendations
113 1 foreach my $parameter (@parameters) {
114 3 ▶ if ( $parameter =~ m/\s*=>\s*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter =~ /\s*=>\s*/) { } 2 1 3
115 2 my @p = split /\s*=>\s*/, $parameter;
116  
117 2 $modules{ $p[0] } = $p[1];
118 }
119 else {
120 1 $modules{$parameter} = undef;
121 }
122 }
123  
124 1 return \%modules;
125 }
126  
127 3 1;
128  
129 __END__
↑ Summary