| 1 | package Perl::Critic::Community::Utils; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(is_empty_return is_structural_block); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my %modifiers = map { ($_ => 1) } qw(if unless while until for foreach when); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my %compound = map { ($_ => 1) } qw(if unless while until for foreach given); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 38 | ▶ | sub is_empty_return { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_empty_return: called CC=9 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 38 | my $elem = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 38 | ▶ | croak 'is_empty_return must be called with a PPI::Token::Word return element' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
builtin::blessed($elem) and $elem->isa("PPI::Token::Word") and $elem eq "return"
builtin::blessed($elem) and $elem->isa("PPI::Token::Word")
MC/DC0%0/3
builtin::blessed($elem) and $elem->isa("PPI::Token::Word") and $elem eq "return"
builtin::blessed($elem)
$elem->isa("PPI::Token::Word")
$elem eq "return"
Truth table25%1/4
A and B and C
A= builtin::blessed($elem)B= $elem->isa("PPI::Token::Word")C= $elem eq "return"
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | unless blessed $elem and $elem->isa('PPI::Token::Word') and $elem eq 'return'; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 38 | ▶ | my $next = $elem->snext_sibling || return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$elem->snext_sibling || (return 1)
MC/DC100%1/1
$elem->snext_sibling || (return 1)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 36 | ▶ | return 1 if $next->isa('PPI::Token::Structure') and $next eq ';'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next->isa("PPI::Token::Structure") and $next eq ";"
MC/DC50%1/2
$next->isa("PPI::Token::Structure") and $next eq ";"
$next->isa("PPI::Token::Structure")
$next eq ";"
Truth table66%2/3
A and B
A= $next->isa("PPI::Token::Structure")B= $next eq ";"
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 31 | ▶ | return 1 if $next->isa('PPI::Token::Word') and exists $modifiers{$next}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$next->isa("PPI::Token::Word") and exists $modifiers{$next}
MC/DC100%2/2
$next->isa("PPI::Token::Word") and exists $modifiers{$next}
$next->isa("PPI::Token::Word")
exists $modifiers{$next}
Truth table100%3/3
A and B
A= $next->isa("PPI::Token::Word")B= exists $modifiers{$next}
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 10 | ▶ | sub is_structural_block { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_structural_block: called CC=8 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | my $elem = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | ▶ | croak 'is_structural_block must be called with a PPI::Structure::Block element' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($elem) and $elem->isa("PPI::Structure::Block")
MC/DC0%0/2
builtin::blessed($elem) and $elem->isa("PPI::Structure::Block")
builtin::blessed($elem)
$elem->isa("PPI::Structure::Block")
Truth table33%1/3
A and B
A= builtin::blessed($elem)B= $elem->isa("PPI::Structure::Block")
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | unless blessed $elem and $elem->isa('PPI::Structure::Block'); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 10 | ▶ | if (my $parent = $elem->parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 10 | ▶ | if ($parent->isa('PPI::Statement::Compound') and my $first = $parent->schild(0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parent->isa("PPI::Statement::Compound") and my $first = $parent->schild(0)
MC/DC50%1/2
$parent->isa("PPI::Statement::Compound") and my $first = $parent->schild(0)
$parent->isa("PPI::Statement::Compound")
my $first = $parent->schild(0)
Truth table66%2/3
A and B
A= $parent->isa("PPI::Statement::Compound")B= my $first = $parent->schild(0)
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | return 1 if $first->isa('PPI::Token::Word') and exists $compound{$first}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$first->isa("PPI::Token::Word") and exists $compound{$first}
MC/DC0%0/2
$first->isa("PPI::Token::Word") and exists $compound{$first}
$first->isa("PPI::Token::Word")
exists $compound{$first}
Truth table33%1/3
A and B
A= $first->isa("PPI::Token::Word")B= exists $compound{$first}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # TODO: Allow bare blocks or blocks with labels | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | Perl::Critic::Community::Utils - Utility functions for the Community policy set | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | This module contains utility functions for use in L<Perl::Critic::Community> | ||||||||||||||||||||||||||||||||||||||||||
| 53 | policies. All functions are exportable on demand. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head2 is_empty_return | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | my $bool = is_empty_return($elem); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | Tests whether a L<PPI::Token::Word> C<return> element represents an empty | ||||||||||||||||||||||||||||||||||||||||||
| 62 | C<return> statement. This function returns false for C<return()>. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head2 is_structural_block | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | my $bool = is_structural_block($elem); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | Tests whether a L<PPI::Structure::Block> element is structural, and does not | ||||||||||||||||||||||||||||||||||||||||||
| 69 | introduce a new calling context. This function currently only returns true for | ||||||||||||||||||||||||||||||||||||||||||
| 70 | blocks in compound statements such as C<if> and C<foreach>, but may be extended | ||||||||||||||||||||||||||||||||||||||||||
| 71 | to cover more cases in the future. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 82 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | L<Perl::Critic>, L<Perl::Critic::Community> | ||||||||||||||||||||||||||||||||||||||||||