File Coverage

blib/lib/Perl/Critic/Policy/Reneeb/ProhibitGrepToGetFirstFoundElement.pm
Criterion Covered Total %
statement 36 36 100.0
branch 11 12 91.6
condition n/a
subroutine 12 12 100.0
pod 4 4 100.0
total 63 64 98.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Reneeb::ProhibitGrepToGetFirstFoundElement;
2              
3             # ABSTRACT: Use List::Utils 'first' instead of grep if you want to get the first found element
4              
5 4     4   3354 use 5.006001;
  4         17  
6 4     4   24 use strict;
  4         9  
  4         150  
7 4     4   26 use warnings;
  4         9  
  4         126  
8 4     4   28 use Readonly;
  4         9  
  4         237  
9 4     4   27 use List::Util qw(first);
  4         9  
  4         471  
10              
11 4     4   31 use Perl::Critic::Utils qw{ :severities };
  4         8  
  4         227  
12              
13 4     4   557 use base 'Perl::Critic::Policy';
  4         9  
  4         1959  
14              
15             our $VERSION = '2.03';
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $DESC => q{Use List::Utils 'first' instead of grep if you want to get the first found element};
20             Readonly::Scalar my $EXPL => [ ];
21              
22             #-----------------------------------------------------------------------------
23              
24 10     10 1 1739 sub default_severity { return $SEVERITY_MEDIUM }
25 1     1 1 735 sub default_themes { return qw<reneeb> }
26             sub applies_to {
27 7     7 1 248899 return qw<
28             PPI::Token::Word
29             >;
30             }
31              
32             #-----------------------------------------------------------------------------
33              
34             sub violates {
35 18     18 1 631 my ( $self, $elem, $doc ) = @_;
36              
37             # other statements than grep aren't catched
38 18 100       49 return if $elem->content ne 'grep';
39              
40 6         42 my $parent = $elem->parent;
41              
42             # grep in boolean or void context isn't checked
43 6 100       103 return if !$parent->isa('PPI::Statement::Variable');
44              
45 5     20   36 my $list = first{ $_->isa('PPI::Structure::List') } $parent->schildren;
  20         194  
46 5 100       44 return if !$list;
47              
48 3         14 my $symbols = $list->find('PPI::Token::Symbol');
49            
50 3 50       837 return if !$symbols;
51 3 100       7 return if 1 != @{ $symbols };
  3         14  
52              
53 2 100       9 return if '$' ne substr $symbols->[0], 0, 1;
54              
55 1         21 return $self->violation( $DESC, $EXPL, $elem );
56             }
57              
58             1;
59              
60             =pod
61              
62             =encoding UTF-8
63              
64             =head1 NAME
65              
66             Perl::Critic::Policy::Reneeb::ProhibitGrepToGetFirstFoundElement - Use List::Utils 'first' instead of grep if you want to get the first found element
67              
68             =head1 VERSION
69              
70             version 2.03
71              
72             =head1 DESCRIPTION
73              
74             To get the first element of a list that matches some criteria, List::Util's C<first>
75             should be used instead of C<grep>.
76              
77             =head1 AUTHOR
78              
79             Renee Baecker <reneeb@cpan.org>
80              
81             =head1 COPYRIGHT AND LICENSE
82              
83             This software is Copyright (c) 2015 by Renee Baecker.
84              
85             This is free software, licensed under:
86              
87             The Artistic License 2.0 (GPL Compatible)
88              
89             =cut
90              
91             __END__
92              
93             #-----------------------------------------------------------------------------
94              
95