File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/RequireBreakBeforeOperator.pm
Criterion Covered Total %
statement 34 35 97.1
branch 6 8 75.0
condition n/a
subroutine 11 12 91.6
pod 5 6 83.3
total 56 61 91.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::RequireBreakBeforeOperator;
2 2     2   1981 use strict;
  2         5  
  2         61  
3 2     2   10 use warnings;
  2         4  
  2         57  
4 2     2   10 use parent qw[ Perl::Critic::Policy ];
  2         4  
  2         11  
5 2     2   132 use Perl::Critic::Utils qw[ :severities :booleans ];
  2         4  
  2         100  
6              
7 2         164 use constant OPERATORS => qw(
8             ++ -- ** ! ~ + -
9             =~ !~ * / % x
10             << >> lt gt le ge cmp ~~
11             == != <=> . .. ...
12             & | ^ && || //
13             ? : **= += -= .= *= /=
14             %= x= &= |= ^= <<= >>= &&=
15             ||= //= < > <= >= <> => ->
16             and or xor not eq ne
17 2     2   385 );
  2         31  
18 2     2   14 use constant PBP_PAGE => 28;
  2         4  
  2         723  
19              
20 3     3 1 35 sub default_severity { return $SEVERITY_LOW }
21 0     0 1 0 sub default_themes { return qw[ cosmetic pbp ] }
22 3     3 1 22877 sub applies_to { return 'PPI::Token::Operator' }
23              
24             sub supported_parameters {
25             return (
26             {
27 3     3 0 17295 name => 'exclude',
28             description => 'Exempt operators.',
29             behavior => 'enumeration',
30             enumeration_values => [OPERATORS],
31             enumeration_allow_multiple_values => 1,
32             default_string => '',
33             },
34             );
35             }
36              
37             sub initialize_if_enabled {
38 3     3 1 12158 my ($self, $config) = @_;
39              
40 3         12 $self->{_ops_to_check} = { map { $_ => 1 } grep { not $self->{_exclude}{$_} } OPERATORS };
  192         396  
  192         365  
41              
42 3         28 return $TRUE;
43             }
44              
45             sub violates {
46 19     19 1 1159 my ($self, $elem, $doc) = @_;
47 19 100       46 return if not $self->{_ops_to_check}{$elem};
48              
49 14         78 my $whitespace = $elem->next_sibling();
50 14 50       499 return if not ref $whitespace;
51 14 50       73 return if not $whitespace->isa('PPI::Token::Whitespace');
52              
53 14 100       40 if (index($whitespace->content, "\n") != -1) {
54 3         25 return $self->violation('Expression broken after operator', PBP_PAGE, $elem);
55             }
56              
57 11         63 return;
58             }
59              
60             1;
61             __END__
62             =pod
63              
64             =head1 NAME
65              
66             Perl::Critic::Policy::CodeLayout::RequireBreakBeforeOperator - multiline expressions should be broken before operator
67              
68             =head1 AFFILIATION
69              
70             This policy as a part of the L<Perl::Critic::PolicyBundle::SNEZ> distribution.
71              
72             =head1 DESCRIPTION
73              
74             Continuations of multiline expressions are easier to spot when they begin
75             with operators, which is unusual in Perl code. Therefore, in all multiline
76             expressions, newlines should be placed before operators, not after.
77              
78             =head1 CONFIGURATION
79              
80             Operators can be exempt from this rule with the exclude parameter:
81              
82             [Perl::Critic::Policy::CodeLayout::RequireBreakBeforeOperator]
83             exclude = .. +
84              
85             =head1 COPYRIGHT
86              
87             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
88              
89             =cut