| 1 | package Perl::Critic::Policy::ErrorHandling::RequireReturnFromEval; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Perl::Critic::Utils qw{ :severities :ppi }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = '1.00'; | |||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | 0 | ▶ | sub default_themes { return qw(style) } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 13 | 7 | ▶ | sub applies_to { return 'PPI::Structure::Block' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | |||||||||||
| 15 | 6 | ▶ | sub violates { | ||||||||
|
Subroutine
violates: called CC=10 SCAR=24.4
Pod
documented
| |||||||||||
| 16 | 6 | my ($self, $elem, undef) = @_; | |||||||||
| 17 | |||||||||||
| 18 | 6 | my $prev = $elem->sprevious_sibling(); | |||||||||
| 19 | 6 | ▶ | if (!$prev) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | return; | |||||||||
| 21 | } | ||||||||||
| 22 | 6 | ▶ | if (!$prev->isa('PPI::Token::Word')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | return; | |||||||||
| 24 | } | ||||||||||
| 25 | 6 | ▶ | if ($prev->content() ne 'eval') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | return; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 6 | my @children = $elem->children(); | |||||||||
| 30 | 6 | ▶ | if (!@children) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | return; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 6 | my @statements = grep { $_->isa('PPI::Statement') } @children; | |||||||||
| 35 | 6 | ▶ | if (!@statements) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | return; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 5 | foreach my $stmt (@statements) { | |||||||||
| 40 | 7 | my @stmt_children = $stmt->children(); | |||||||||
| 41 | 7 | ▶ | if (!@stmt_children) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | next; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 7 | my $first = $stmt_children[0]; | |||||||||
| 46 | 7 | ▶ | if (!$first->isa('PPI::Token::Word')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | next; | |||||||||
| 48 | } | ||||||||||
| 49 | 6 | ▶ | if ($first->content() eq 'return') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 2 | return; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 3 | return $self->violation('Eval block should use explicit return', '', $elem); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | 1; | |||||||||
| 58 | |||||||||||
| 59 | __END__ | ||||||||||