File Coverage

blib/lib/Perl/Critic/Freenode/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             package Perl::Critic::Freenode::Utils;
2              
3 1     1   7 use strict;
  1         2  
  1         28  
4 1     1   5 use warnings;
  1         2  
  1         24  
5 1     1   5 use Carp 'croak';
  1         2  
  1         42  
6 1     1   6 use Exporter 'import';
  1         1  
  1         41  
7 1     1   7 use Scalar::Util 'blessed';
  1         3  
  1         374  
8              
9             our $VERSION = '0.030';
10              
11             our @EXPORT_OK = qw(is_empty_return is_structural_block);
12              
13             my %modifiers = map { ($_ => 1) } qw(if unless while until for foreach when);
14             my %compound = map { ($_ => 1) } qw(if unless while until for foreach given);
15              
16             sub is_empty_return {
17 38     38 1 63 my $elem = shift;
18 38 50 33     227 croak 'is_empty_return must be called with a PPI::Token::Word return element'
      33        
19             unless blessed $elem and $elem->isa('PPI::Token::Word') and $elem eq 'return';
20            
21 38   100     473 my $next = $elem->snext_sibling || return 1;
22 36 100 66     791 return 1 if $next->isa('PPI::Token::Structure') and $next eq ';';
23 31 100 100     118 return 1 if $next->isa('PPI::Token::Word') and exists $modifiers{$next};
24            
25 23         118 return 0;
26             }
27              
28             sub is_structural_block {
29 10     10 1 17 my $elem = shift;
30 10 50 33     60 croak 'is_structural_block must be called with a PPI::Structure::Block element'
31             unless blessed $elem and $elem->isa('PPI::Structure::Block');
32            
33 10 50       32 if (my $parent = $elem->parent) {
34 10 100 66     77 if ($parent->isa('PPI::Statement::Compound') and my $first = $parent->schild(0)) {
35 9 50 33     127 return 1 if $first->isa('PPI::Token::Word') and exists $compound{$first};
36             }
37             }
38            
39             # TODO: Allow bare blocks or blocks with labels
40            
41 1         5 return 0;
42             }
43              
44             1;
45              
46             =head1 NAME
47              
48             Perl::Critic::Freenode::Utils - Utility functions for the Freenode policy set
49              
50             =head1 DESCRIPTION
51              
52             This module contains utility functions for use in L<Perl::Critic::Freenode>
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>