| 1 | package Perl::Critic::Policy::CodeLayout::RequireKRBracing; | ||||||||||||||||||||||||||||||||||||||||||
| 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 ]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use List::MoreUtils qw[ any ]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use constant PBP_PAGE => 9; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | my %affected_conditional = map { $_ => 1 } qw( if while unless until ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | my %affected_list_loop = map { $_ => 1 } qw( for foreach ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | my %affected_followup = map { $_ => 1 } qw( else elsif continue ); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub default_themes { return qw[ cosmetic pbp ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | sub applies_to { return 'PPI::Structure::Block' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 20 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 20 | my ($self, $elem, $doc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 20 | my ($keyword, $parens) = _affected_construct($elem); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 20 | ▶ | return if any { not defined } $keyword, $parens; # no affected construct detected | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 14 | my $inter_whitespace = $elem->previous_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 14 | ▶ | if (not $inter_whitespace->isa('PPI::Token::Whitespace')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | return $self->violation("No whitespace between closing '$keyword' parenthesis and opening brace", | |||||||||||||||||||||||||||||||||||||||||
| 27 | PBP_PAGE, $elem); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | my $pre_whitespace = $parens->previous_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | ▶ | if (not $pre_whitespace->isa('PPI::Token::Whitespace')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | return $self->violation("No whitespace before opening '$keyword' parenthesis", PBP_PAGE, $parens); | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | if (index($inter_whitespace->content, "\n") != -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | return $self->violation("Opening brace of '$keyword' block is not on the same line", PBP_PAGE, $elem); | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | my $next_keyword = $elem->snext_sibling(); # else / elsif / continue | |||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | if ( ref $next_keyword | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $next_keyword and $next_keyword->isa("PPI::Token::Word") and $affected_followup{$next_keyword}
ref $next_keyword and $next_keyword->isa("PPI::Token::Word")
MC/DC33%1/3
ref $next_keyword and $next_keyword->isa("PPI::Token::Word") and $affected_followup{$next_keyword}
ref $next_keyword
$next_keyword->isa("PPI::Token::Word")
$affected_followup{$next_keyword}
Truth table50%2/4
A and B and C
A= ref $next_keywordB= $next_keyword->isa("PPI::Token::Word")C= $affected_followup{$next_keyword}
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | and $next_keyword->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||
| 42 | and $affected_followup{$next_keyword}) | ||||||||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | my $follow_whitespace = $elem->next_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | my $no_whitespace = "No whitespace between closing brace and '$next_keyword'"; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | return $self->violation($no_whitespace, PBP_PAGE, $next_keyword) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | if not $follow_whitespace->isa('PPI::Token::Whitespace'); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | my $no_newline = "'$next_keyword' is on the same line as closing brace"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | return $self->violation($no_newline, PBP_PAGE, $next_keyword) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | if index($follow_whitespace->content, "\n") == -1; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | ▶ | sub _affected_construct { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_affected_construct: called CC=15 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 20 | my ($elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | my $parens = $elem->sprevious_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 20 | ▶ | return if not ref $parens; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 20 | my $is_conditional = $parens->isa('PPI::Structure::Condition'); # if / while / unless / until | |||||||||||||||||||||||||||||||||||||||||
| 65 | 20 | my $is_list_loop = $parens->isa('PPI::Structure::List'); # for / foreach | |||||||||||||||||||||||||||||||||||||||||
| 66 | 20 | ▶ | return if not($is_conditional or $is_list_loop); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_conditional or $is_list_loop
MC/DC100%2/2
$is_conditional or $is_list_loop
$is_conditional
$is_list_loop
Truth table100%3/3
A or B
A= $is_conditionalB= $is_list_loop
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | my $keyword = $parens->sprevious_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | ▶ | if ($keyword->isa('PPI::Token::Word')) { # a conditional or foreach without an explicit iterator | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | return if $is_conditional and not $affected_conditional{$keyword}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_conditional and not $affected_conditional{$keyword}
MC/DC100%2/2
$is_conditional and not $affected_conditional{$keyword}
$is_conditional
not $affected_conditional{$keyword}
Truth table100%3/3
A and not(B)
A= $is_conditionalB= not $affected_conditional{$keyword}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 12 | ▶ | return if $is_list_loop and not $affected_list_loop{$keyword}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_list_loop and not $affected_list_loop{$keyword}
MC/DC0%0/2
$is_list_loop and not $affected_list_loop{$keyword}
$is_list_loop
not $affected_list_loop{$keyword}
Truth table66%2/3
A and not(B)
A= $is_list_loopB= not $affected_list_loop{$keyword}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | ▶ | elsif ($is_list_loop and $keyword->isa('PPI::Token::Symbol')) { # foreach with an explicit iterator | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$is_list_loop and $keyword->isa("PPI::Token::Symbol")
MC/DC0%0/2
$is_list_loop and $keyword->isa("PPI::Token::Symbol")
$is_list_loop
$keyword->isa("PPI::Token::Symbol")
Truth table33%1/3
A and B
A= $is_list_loopB= $keyword->isa("PPI::Token::Symbol")
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | $keyword = $keyword->sprevious_sibling(); # go back to either 'my' or 'for'/'foreach' | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | $keyword = $keyword->sprevious_sibling # go back one more token in case of 'my' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
MC/DC0%0/2
$keyword->isa("PPI::Token::Word") and $keyword->content eq "my"
$keyword->isa("PPI::Token::Word")
$keyword->content eq "my"
Truth table33%1/3
A and B
A= $keyword->isa("PPI::Token::Word")B= $keyword->content eq "my"
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | if $keyword->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||
| 77 | and $keyword->content eq 'my'; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Should have found the actual keyword by now | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | return if not $keyword->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | return if not $affected_list_loop{$keyword}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 14 | return ($keyword, $parens); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||||||||||||