File Coverage

blib/lib/Perl/Critic/Community/Utils.pm
Criterion Covered Total %
statement 27 27 100.0
branch 10 14 71.4
condition 13 23 56.5
subroutine 7 7 100.0
pod 2 2 100.0
total 59 73 80.8


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