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

filter: Statementstmt 73.8% 62 / 84 Branchbran 66.6% 12 / 18 Conditioncond 50.0% 8 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 70.3% 102 / 145 CC 16 SCAR 24.4
CC
16
Cov
69%
violates
27.7
initialize_if_enabled
4.0
prepare_to_scan_document
3.8
CRAP 11.5
↑ Summary
1 package Perl::Critic::Policy::logicLAB::RequireSheBang;
2  
3 # $Id$
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 $Data::Dumper::Useqq = 1;
14  
15 5 our $VERSION = '0.07';
16  
17 5 Readonly::Scalar my $EXPL => q{she-bang line should adhere to requirement};
18 5 Readonly::Scalar my $DEBUG => q{DEBUG logicLAB::RequireSheBang};
19  
20 5 ▶use constant default_severity => $SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use constant default_themes => qw(logiclab);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use constant supported_parameters => qw(formats debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 18 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=3 SCAR=13.3
Pod documented
25 18 my ( $self, $document ) = @_;
26 18 ▶ if ( $self->{exempt_modules} && $document->is_module() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exempt_modules"} and $document->is_module) 0 18 18
Condition33%1/3
$self->{"exempt_modules"} and $document->is_module
!ll&&!rl&&r
0180
MC/DC0%0/2
$self->{"exempt_modules"} and $document->is_module
$self->{"exempt_modules"} $document->is_module
Truth table33%1/3
A and B
A= $self->{"exempt_modules"}B= $document->is_module
ABresult
0X0
100
111
27 0 return 0;
28 }
29  
30 18 return $document->is_program();
31 }
32  
33 138 ▶sub violates {
Subroutine violates: called CC=11 SCAR=33.2
Pod documented
34 138 my ( $self, $element, $doc ) = @_;
35  
36 138 my $statement = $doc->find_first('PPI::Token::Comment');
37  
38 138 ▶ if ( not $statement->location()->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($statement->location->[0]) 0 138 138
39 0 return $self->violation( q{she-bang line not located as first line},
40 $EXPL, $statement );
41 }
42  
43 138 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 138 138
44 0 print {*STDERR} "$DEBUG: we got statement:\n";
45 0 print {*STDERR} Dumper $statement;
46 }
47  
48 138 my ( $shebang, $cli ) = $element =~ m{
49 \A #beginning of string
50 (\#!) #actual she-bang
51 #([^\r\n]*?) #the path and possible flags
52 ([/\-\w ]+?) #the path and possible flags, note the space character
53 \s* #possible left over whitespace (PPI?)
54 \Z #indication of end of string to assist above capture
55 }xsm;
56  
57 138 ▶ if ($cli) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli) 18 120 138
58 18 $cli =~ s{
59 \s+ #one or more whitespace character, PCPLRSB-9 / http://logiclab.jira.com/browse/PCPLRSB-9
60 $ #end of string
61 }{}xsm;
62 }
63  
64 138 ▶ if ( $self->{debug} && $shebang && $cli ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'debug'} and $shebang and $cli) { } 0 138 138
elsif ($self->{'debug'}) { } 0 138 138
Condition33%2/6
$self->{"debug"} and $shebang and $cli
!ll&&!rl&&r
13800
$self->{"debug"} and $shebang
!ll&&!rl&&r
13800
MC/DC0%0/3
$self->{"debug"} and $shebang and $cli
$self->{"debug"} $shebang $cli
Truth table25%1/4
A and B and C
A= $self->{"debug"}B= $shebangC= $cli
ABCresult
0XX0
10X0
1100
1111
65 0 print {*STDERR} "$DEBUG: we got a shebang line:\n";
66 0 print {*STDERR} '>' . $shebang . $cli . "<\n";
67  
68 0 print {*STDERR} "$DEBUG: comparing against formats:\n";
69 0 print {*STDERR} Dumper $self->{_formats};
70 0 print {*STDERR} "\n";
71  
72 }
73 elsif ( $self->{debug} ) {
74 0 print {*STDERR} "$DEBUG: not a shebang, ignoring...\n";
75 }
76  
77 138 ▶ if ( $shebang && none { ( $shebang . $cli ) eq $_ } @{ $self->{_formats} } )
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($shebang and &none(sub { $shebang . $cli eq $_; } , @{$$self{"_formats"};})) 9 129 138
Condition100%3/3
$shebang and &none(sub { $shebang . $cli eq $_; } , @{$$self{"_formats"};})
!ll&&!rl&&r
12099
MC/DC100%2/2
$shebang and &none(sub { $shebang . $cli eq $_; } , @{$$self{"_formats"};})
$shebang &none(sub { $shebang . $cli eq $_; } , @{$$self{"_formats"};})
Truth table100%3/3
A and B
A= $shebangB= &none(sub { $shebang . $cli eq $_; } , @{$$self{"_formats"};})
ABresult
0X0
100
111
78 {
79  
80 9 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 9 9
81 0 print {*STDERR} "$DEBUG: we got a violation:\n";
82 0 print {*STDERR} '>' . $shebang . $cli . "<\n";
83 }
84  
85 9 return $self->violation(
86 q{she-bang line not conforming with requirement},
87 $EXPL, $element );
88 }
89  
90 129 return;
91 }
92  
93 4 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=4 SCAR=14.0
Pod documented
94 4 my ( $self, $config ) = @_;
95  
96 #Formats:
97 #Setting the default
98 4 $self->{_formats} = [ ('#!/usr/local/bin/perl') ];
99  
100 #fetching configured formats
101 4 my $formats = $config->get('formats');
102  
103 #parsing configured formats, see also _parse_formats
104 4 ▶ if ($formats) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formats) 3 1 4
105 3 $self->{_formats} = $self->_parse_formats($formats);
106 }
107  
108 #debug
109 4 ▶ $self->{debug} = $config->get('debug') || 0;
Condition50%1/2
$config->get('debug') || 0
l!l
04
MC/DC0%0/1
$config->get('debug') || 0
$config->get('debug')
Truth table50%1/2
A
A= $config->get('debug')
Aresult
11
00
110  
111 #exempt_modules
112 4 ▶ $self->{exempt_modules} = $config->get('exempt_modules') || 1;
Condition50%1/2
$config->get('exempt_modules') || 1
l!l
04
MC/DC0%0/1
$config->get('exempt_modules') || 1
$config->get('exempt_modules')
Truth table50%1/2
A
A= $config->get('exempt_modules')
Aresult
11
00
113  
114 4 return $TRUE;
115 }
116  
117 3 ▶sub _parse_formats {
Subroutine _parse_formats: called CC=1 SCAR=0.0
118 3 my ( $self, $config_string ) = @_;
119  
120 3 my @formats = split m{ \s* [||]+ \s* }xsm, $config_string;
121  
122 3 return \@formats;
123 }
124  
125 5 1;
126  
127 __END__
↑ Summary