Community/Utils.pm

filter: Statementstmt 100% 34 / 34 Branchbran 71.4% 10 / 14 Conditioncond 56.5% 13 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 75.5% 71 / 94 CC 16 SCAR 21.0
CC
16
Cov
80%
is_structural_block
10.7
is_empty_return
10.0
BEGIN
1.0
CRAP 8.2
« Policy/Community/Prototypes.pm ↑ Summary Policy/Community/SplitQuotedPattern.pm »
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
BranchTrueFalseTotal
unless builtin::blessed($elem) and $elem->isa("PPI::Token::Word") and $elem eq "return" 0 38 38
Condition33%2/6
builtin::blessed($elem) and $elem->isa("PPI::Token::Word") and $elem eq "return"
!ll&&!rl&&r
0038
builtin::blessed($elem) and $elem->isa("PPI::Token::Word")
!ll&&!rl&&r
0038
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"
ABCresult
0XX0
10X0
1100
1111
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)
l!l
362
MC/DC100%1/1
$elem->snext_sibling || (return 1)
$elem->snext_sibling
Truth table100%2/2
A
A= $elem->snext_sibling
Aresult
11
00
22 36 ▶ return 1 if $next->isa('PPI::Token::Structure') and $next eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if $next->isa("PPI::Token::Structure") and $next eq ";" 5 31 36
Condition66%2/3
$next->isa("PPI::Token::Structure") and $next eq ";"
!ll&&!rl&&r
3105
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 ";"
ABresult
0X0
100
111
23 31 ▶ return 1 if $next->isa('PPI::Token::Word') and exists $modifiers{$next};
Branch: 1 branch
BranchTrueFalseTotal
if $next->isa("PPI::Token::Word") and exists $modifiers{$next} 8 23 31
Condition100%3/3
$next->isa("PPI::Token::Word") and exists $modifiers{$next}
!ll&&!rl&&r
2128
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}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless builtin::blessed($elem) and $elem->isa("PPI::Structure::Block") 0 10 10
Condition33%1/3
builtin::blessed($elem) and $elem->isa("PPI::Structure::Block")
!ll&&!rl&&r
0010
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")
ABresult
0X0
100
111
31 unless blessed $elem and $elem->isa('PPI::Structure::Block');
32
33 10 ▶ if (my $parent = $elem->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $elem->parent) 10 0 10
34 10 ▶ if ($parent->isa('PPI::Statement::Compound') and my $first = $parent->schild(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Statement::Compound") and my $first = $parent->schild(0)) 9 1 10
Condition66%2/3
$parent->isa("PPI::Statement::Compound") and my $first = $parent->schild(0)
!ll&&!rl&&r
109
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)
ABresult
0X0
100
111
35 9 ▶ return 1 if $first->isa('PPI::Token::Word') and exists $compound{$first};
Branch: 1 branch
BranchTrueFalseTotal
if $first->isa("PPI::Token::Word") and exists $compound{$first} 9 0 9
Condition33%1/3
$first->isa("PPI::Token::Word") and exists $compound{$first}
!ll&&!rl&&r
009
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}
ABresult
0X0
100
111
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>
« Policy/Community/Prototypes.pm ↑ Summary Policy/Community/SplitQuotedPattern.pm »