Policy/Reneeb/ProhibitGrepToGetFirstFoundElement.pm

filter: Statementstmt 100% 40 / 40 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 98.5% 67 / 68 CC 7 SCAR 12.0
CC
7
Cov
98%
violates
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.3
↑ Summary Policy/Reneeb/Capitalization.pm »
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 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our $VERSION = '2.05';
16  
17 #-----------------------------------------------------------------------------
18  
19 5 Readonly::Scalar my $DESC => q{Use List::Utils 'first' instead of grep if you want to get the first found element};
20 5 Readonly::Scalar my $EXPL => [ ];
21  
22 #-----------------------------------------------------------------------------
23  
24 10 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 1 ▶sub default_themes { return qw<reneeb> }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
26 7 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27 7 return qw<
28 PPI::Token::Word
29 >;
30 }
31  
32 #-----------------------------------------------------------------------------
33  
34 18 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.5
Pod documented
35 18 my ( $self, $elem, $doc ) = @_;
36  
37 # other statements than grep aren't catched
38 18 ▶ return if $elem->content ne 'grep';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "grep" 12 6 18
39  
40 6 my $parent = $elem->parent;
41  
42 # grep in boolean or void context isn't checked
43 6 ▶ return if !$parent->isa('PPI::Statement::Variable');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Statement::Variable") 1 5 6
44  
45 5 ▶ my $list = first{ $_->isa('PPI::Structure::List') } $parent->schildren;
Subroutine __ANON__: called CC=1 SCAR=0.0
46 5 ▶ return if !$list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 2 3 5
47  
48 3 my $symbols = $list->find('PPI::Token::Symbol');
49
50 3 ▶ return if !$symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbols 0 3 3
51 3 ▶ return if 1 != @{ $symbols };
Branch: 1 branch
BranchTrueFalseTotal
if 1 != @{$symbols;} 1 2 3
52  
53 2 ▶ return if '$' ne substr $symbols->[0], 0, 1;
Branch: 1 branch
BranchTrueFalseTotal
if "\$" ne substr($symbols->[0], 0, 1) 1 1 2
54  
55 1 return $self->violation( $DESC, $EXPL, $elem );
56 }
57  
58 5 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.05
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__
↑ Summary Policy/Reneeb/Capitalization.pm »