| 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
Condition33%1/3
$self->{"exempt_programs"} and $document->is_program
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 39 | 1 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | my @children = $elem->children; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 12 | ▶ | if ( $children[0]->content eq 'package' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 0 | print STDERR "Regex: $regex\n"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 14 | ▶ | if ( $package !~ m/$regex/xs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | if ($no_of_patterns > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1 | $no_of_violations++; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | if ($no_of_patterns == $no_of_violations) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | #Names: | ||||||||||||||||||||||||||||
| 103 | #fetching configured names | ||||||||||||||||||||||||||||
| 104 | 13 | my $names = $config->get('names'); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 13 | ▶ | if ($self->{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | $self->{_names} = $self->_parse_names($names) || q{}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_parse_names($names) || ''
MC/DC0%0/1
$self->_parse_names($names) || ''
$self->_parse_names($names)
Truth table50%1/2
A
A= $self->_parse_names($names)
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||