Policy/CodeLayout/RequireSpaceAroundBinaryOperators.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 92.8% 13 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 94.4% 17 / 18 Podpod 83.3% 5 / 6 totaltotal 94.0% 95 / 101 CC 10 SCAR 11.8
CC
10
Cov
96%
violates
8.0
__ANON__
2.0
default_themes
2.0
CRAP 3.3
« Policy/ValuesAndExpressions/ProhibitListsInMultiConstants.pm ↑ Summary Policy/CodeLayout/RequireBreakBeforeOperator.pm »
1 package Perl::Critic::Policy::CodeLayout::RequireSpaceAroundBinaryOperators;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw[ Perl::Critic::Policy ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Critic::Utils qw[ :severities :data_conversion :booleans ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::MoreUtils qw[ all any ];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant BINARY_OPERATORS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 ** + -
10 =~ !~ * / % x
11 << >> lt gt le ge cmp ~~
12 == != <=> . .. ...
13 & | ^ && || //
14 **= += -= .= *= /=
15 %= x= &= |= ^= <<= >>= &&=
16 ||= //= < > <= >= => ->
17 and or xor eq ne
18 2 ); # comma specifically excluded, even though it's technically a binary operator
19 2 ▶use constant OPERAND_CLASSES => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 PPI::Token::HereDoc
21 PPI::Token::Number
22 PPI::Token::Quote
23 PPI::Token::QuoteLike
24 PPI::Token::Regexp
25 PPI::Token::Symbol
26 PPI::Structure::List
27 2 );
28 2 ▶use constant PBP_PAGE => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
31 0 ▶sub default_themes { return qw[ cosmetic pbp ] }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
32 8 ▶sub applies_to { return 'PPI::Token::Operator' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
33  
34 8 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
35 return (
36 {
37 8 name => 'exclude',
38 description => 'Exempt operators.',
39 behavior => 'enumeration',
40 enumeration_values => [BINARY_OPERATORS],
41 enumeration_allow_multiple_values => 1,
42 default_string => '** ->',
43 },
44 );
45 }
46  
47 8 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
48 8 my ($self, $config) = @_;
49  
50 $self->{_ops_to_check} = {
51 8 map { $_ => 1 } grep { not $self->{_exclude}{$_} } BINARY_OPERATORS
52 };
53  
54 8 return $TRUE;
55 }
56  
57 38 ▶sub violates {
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
58 38 my ($self, $elem, $doc) = @_;
59 38 my $op = $elem->content();
60 38 ▶ return if not $self->{_ops_to_check}{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"_ops_to_check"}{$op} 22 16 38
61  
62 16 ▶ if ($op eq '+' or $op eq '-') { # try to detect unary operators
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" or $op eq "-") 8 8 16
Condition100%3/3
$op eq "+" or $op eq "-"
l!l&&r!l&&!r
448
MC/DC100%2/2
$op eq "+" or $op eq "-"
$op eq "+" $op eq "-"
Truth table100%3/3
A or B
A= $op eq "+"B= $op eq "-"
ABresult
1X1
011
000
63 8 my $next_sig_sib = $elem->snext_sibling();
64 8 my $prev_sig_sib = $elem->sprevious_sibling();
65 8 ▶ return if any { not _is_operand($_) } $next_sig_sib, $prev_sig_sib;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not _is_operand($_); } , $next_sig_sib, $prev_sig_sib) 2 6 8
66 }
67  
68 14 my $next_sib = $elem->next_sibling();
69 14 my $prev_sib = $elem->previous_sibling();
70  
71 # Treat ,=> as a single operator
72 14 ▶ $prev_sib = $prev_sib->previous_sibling()
Branch: 1 branch
BranchTrueFalseTotal
if $elem eq "=>" and $prev_sib eq "," 8 6 14
Condition66%2/3
$elem eq "=>" and $prev_sib eq ","
!ll&&!rl&&r
608
MC/DC50%1/2
$elem eq "=>" and $prev_sib eq ","
$elem eq "=>" $prev_sib eq ","
Truth table66%2/3
A and B
A= $elem eq "=>"B= $prev_sib eq ","
ABresult
0X0
100
111
73 if $elem eq '=>' and $prev_sib eq ',';
74  
75 14 ▶ if (not all { ref and $_->isa('PPI::Token::Whitespace') } $next_sib, $prev_sib) {
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
unless (&all(sub { ref $_ and $_->isa("PPI::Token::Whitespace"); } , $next_sib, $prev_sib)) 5 9 14
ref $_ and $_->isa("PPI::Token::Whitespace") 24 0 24
76 5 return $self->violation("Binary $op operator used without surrounding whitespace",
77 PBP_PAGE, $elem);
78 }
79  
80 9 return;
81 }
82  
83 16 ▶sub _is_operand {
Subroutine _is_operand: called CC=2 SCAR=6.9
84 16 my ($elem) = @_;
85 16 ▶ return if not ref $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem 1 15 16
86 15 ▶ return any { $elem->isa($_) } OPERAND_CLASSES;
Subroutine __ANON__: called CC=1 SCAR=0.0
87 }
88  
89 2 1;
90 __END__
« Policy/ValuesAndExpressions/ProhibitListsInMultiConstants.pm ↑ Summary Policy/CodeLayout/RequireBreakBeforeOperator.pm »