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 5     5   3275 use 5.006001;
  5         21  
6 5     5   33 use strict;
  5         11  
  5         94  
7 5     5   24 use warnings;
  5         11  
  5         117  
8 5     5   23 use Readonly;
  5         13  
  5         235  
9 5     5   30 use List::Util qw(first);
  5         10  
  5         362  
10              
11 5     5   36 use Perl::Critic::Utils qw{ :severities };
  5         10  
  5         242  
12              
13 5     5   769 use base 'Perl::Critic::Policy';
  5         17  
  5         1981  
14              
15             our $VERSION = '2.04';
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 1838 sub default_severity { return $SEVERITY_MEDIUM }
25 1     1 1 667 sub default_themes { return qw<reneeb> }
26             sub applies_to {
27 7     7 1 267143 return qw<
28             PPI::Token::Word
29             >;
30             }
31              
32             #-----------------------------------------------------------------------------
33              
34             sub violates {
35 18     18 1 828 my ( $self, $elem, $doc ) = @_;
36              
37             # other statements than grep aren't catched
38 18 100       74 return if $elem->content ne 'grep';
39              
40 6         55 my $parent = $elem->parent;
41              
42             # grep in boolean or void context isn't checked
43 6 100       71 return if !$parent->isa('PPI::Statement::Variable');
44              
45 5     20   41 my $list = first{ $_->isa('PPI::Structure::List') } $parent->schildren;
  20         214  
46 5 100       49 return if !$list;
47              
48 3         16 my $symbols = $list->find('PPI::Token::Symbol');
49            
50 3 50       1054 return if !$symbols;
51 3 100       8 return if 1 != @{ $symbols };
  3         15  
52              
53 2 100       11 return if '$' ne substr $symbols->[0], 0, 1;
54              
55 1         17 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.04
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