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

filter: Statementstmt 89.3% 67 / 75 Branchbran 68.1% 15 / 22 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 78.3% 105 / 134 CC 17 SCAR 16.3
CC
17
Cov
80%
violates
9.8
initialize_if_enabled
7.3
prepare_to_scan_document
3.8
CRAP 5.1
↑ Summary
1 package Perl::Critic::Policy::logicLAB::RequirePackageNamePattern;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.05';
13  
14 4 ▶use constant supported_parameters => qw(names debug exempt_programs);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use constant default_severity => $SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use constant default_themes => qw(logiclab);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=3 SCAR=13.3
Pod documented
19 13 my ( $self, $document ) = @_;
20 13 ▶ if ( $self->{exempt_programs} && $document->is_program() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exempt_programs"} and $document->is_program) 0 13 13
Condition33%1/3
$self->{"exempt_programs"} and $document->is_program
!ll&&!rl&&r
0130
MC/DC0%0/2
$self->{"exempt_programs"} and $document->is_program
$self->{"exempt_programs"} $document->is_program
Truth table33%1/3
A and B
A= $self->{"exempt_programs"}B= $document->is_program
ABresult
0X0
100
111
21 0 return $FALSE;
22 }
23  
24 13 return $document->is_module();
25 }
26  
27 13 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28 return (
29 13 qw(
30 PPI::Statement::Package
31 )
32 );
33 }
34  
35 13 ▶sub violates {
Subroutine violates: called CC=9 SCAR=22.8
Pod documented
36 13 my ( $self, $elem ) = @_;
37  
38 13 ▶ if ( not $self->{_names} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_names"}) 1 12 13
39 1 return;
40 }
41  
42 12 my @children = $elem->children;
43  
44 12 ▶ if ( $children[0]->content eq 'package' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[0]->content eq 'package') { } 12 0 12
45 #TODO we might have to look for words here instead of using an array index
46 #TODO and we should add exception in the case an actual package is not located
47 12 my $package = $children[2]->content;
48  
49 12 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 12 12
50 0 print STDERR "located package: $package\n";
51 }
52  
53 12 my $no_of_patterns = scalar @{$self->{_names}};
54 12 my $no_of_violations = 0;
55  
56 12 foreach my $name (@{$self->{_names}}) {
57 #TODO investigate wht this is a regular expression and so are
58 #actual evaluation in line 67, at least according to Perl::Critic
59 #[RegularExpressions::RequireExtendedFormatting]
60 14 my $regex = qr/$name/x;
61  
62 14 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 14 14
63 0 print STDERR "Regex: $regex\n";
64 }
65  
66 14 ▶ if ( $package !~ m/$regex/xs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package =~ /$regex/sx) 6 8 14
67 6 ▶ if ($no_of_patterns > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_of_patterns > 1) { } 1 5 6
68 1 $no_of_violations++;
69  
70 1 ▶ if ($no_of_patterns == $no_of_violations) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_of_patterns == $no_of_violations) 0 1 1
71 0 return $self->violation(
72 "Package name: $package is not complying with required standard",
73 "Use specified requirement for package naming for $package",
74 $elem
75 );
76 }
77  
78 } else {
79  
80 5 return $self->violation(
81 "Package name: $package is not complying with required standard",
82 "Use specified requirement for package naming for $package",
83 $elem
84 );
85 }
86 }
87 }
88  
89 } else {
90 0 carp 'Unable to locate package keyword';
91 }
92  
93 7 return;
94 }
95  
96 13 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=6 SCAR=19.9
Pod documented
97 13 my ( $self, $config ) = @_;
98  
99 #debug - order is significant, since we might need debugging
100 13 ▶ $self->{debug} = $config->get('debug') || $FALSE;
Condition33%1/3
$config->get('debug') || $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::FALSE
l!l&&r!l&&!r
0013
MC/DC0%0/2
$config->get('debug') || $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::FALSE
$config->get('debug') $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::FALSE
Truth table33%1/3
A || B
A= $config->get('debug')B= $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::FALSE
ABresult
1X1
011
000
101  
102 #Names:
103 #fetching configured names
104 13 my $names = $config->get('names');
105  
106 13 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 13 13
107 0 warn "Requirements for package names are: $names\n";
108 }
109  
110 #parsing configured names, see also _parse_names
111 13 ▶ if ($names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($names) 12 1 13
112 12 ▶ $self->{_names} = $self->_parse_names($names) || q{};
Condition50%1/2
$self->_parse_names($names) || ''
l!l
120
MC/DC0%0/1
$self->_parse_names($names) || ''
$self->_parse_names($names)
Truth table50%1/2
A
A= $self->_parse_names($names)
Aresult
11
00
113 }
114  
115 #exempt_programs
116 13 ▶ $self->{exempt_programs} = $config->get('exempt_programs') || $TRUE;
Condition33%1/3
$config->get('exempt_programs') || $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::TRUE
l!l&&r!l&&!r
0130
MC/DC0%0/2
$config->get('exempt_programs') || $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::TRUE
$config->get('exempt_programs') $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::TRUE
Truth table33%1/3
A || B
A= $config->get('exempt_programs')B= $Perl::Critic::Policy::logicLAB::RequirePackageNamePattern::TRUE
ABresult
1X1
011
000
117  
118 13 return $TRUE;
119 }
120  
121 12 ▶sub _parse_names {
Subroutine _parse_names: called CC=2 SCAR=7.9
122 12 my ( $self, $config_string ) = @_;
123  
124 12 my @names = split /\s*\|\|\s*/x, $config_string;
125  
126 12 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 12 12
127 0 print STDERR "our split line:\n";
128 0 print STDERR Dumper \@names;
129 }
130  
131 12 return \@names;
132 }
133  
134 4 1;
135  
136 __END__
↑ Summary