| 1 | #!/bin/false | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Prohibit method calls in string concatenation | ||||||||||||||||||||||||||||
| 3 | # PODNAME: Perl::Critic::Policy::Misc::ProhibitMethodsInStringConcat | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Perl::Critic::Policy::Misc::ProhibitMethodsInStringConcat; | ||||||||||||||||||||||||||||
| 9 | 2 | $Perl::Critic::Policy::Misc::ProhibitMethodsInStringConcat::VERSION = '0.02'; | |||||||||||||||||||||||||||
| 10 | 2 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Perl::Critic::Utils qw(:severities); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | sub default_severity { return $SEVERITY_MEDIUM; } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub default_themes { return qw(misc); } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 12 | ▶ | sub applies_to { return 'PPI::Token::Operator'; } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 25 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 25 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 25 | ▶ | return if $elem->content() ne q{.}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 12 | my $stmt = $elem->parent(); | |||||||||||||||||||||||||||
| 26 | 12 | ▶ | while ( $stmt && !$stmt->isa('PPI::Statement') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$stmt and not $stmt->isa("PPI::Statement")
MC/DC0%0/2
$stmt and not $stmt->isa("PPI::Statement")
$stmt
not $stmt->isa("PPI::Statement")
Truth table33%1/3
A and not(B)
A= $stmtB= not $stmt->isa("PPI::Statement")
| |||||||||||||||||||||||||||||
| 27 | 0 | $stmt = $stmt->parent(); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 12 | ▶ | return if !$stmt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $found = $stmt->find( | ||||||||||||||||||||||||||||
| 32 | 157 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 33 | 157 | my $node = $_[1]; | |||||||||||||||||||||||||||
| 34 | 157 | ▶ | return 0 if !$node->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 29 | ▶ | return 0 if $node->content() ne '->'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 11 | my $next = $node->snext_sibling(); | |||||||||||||||||||||||||||
| 37 | 11 | ▶ | return 0 if !$next || !$next->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$next and $next->isa("PPI::Token::Word")
MC/DC0%0/2
$next and $next->isa("PPI::Token::Word")
$next
$next->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= $nextB= $next->isa("PPI::Token::Word")
| |||||||||||||||||||||||||||||
| 38 | 11 | return 1; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 12 | ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | ▶ | return if !$found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 9 | return $self->violation( | |||||||||||||||||||||||||||
| 45 | 'Method call used in string concatenation', | ||||||||||||||||||||||||||||
| 46 | 'Assign the method result to a variable first. ' | ||||||||||||||||||||||||||||
| 47 | . 'Method calls in string concatenation lose the variable name in undef warnings - ' | ||||||||||||||||||||||||||||
| 48 | . 'Perl can only say "uninitialized value" without naming which call returned undef, ' | ||||||||||||||||||||||||||||
| 49 | . 'making debugging harder.', | ||||||||||||||||||||||||||||
| 50 | $elem, | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | 1; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | __END__ | ||||||||||||||||||||||||||||