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
|
|
3425
|
use 5.006001; |
|
5
|
|
|
|
|
22
|
|
6
|
5
|
|
|
5
|
|
27
|
use strict; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
98
|
|
7
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
107
|
|
8
|
5
|
|
|
5
|
|
24
|
use Readonly; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
229
|
|
9
|
5
|
|
|
5
|
|
32
|
use List::Util qw(first); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
381
|
|
10
|
|
|
|
|
|
|
|
11
|
5
|
|
|
5
|
|
37
|
use Perl::Critic::Utils qw{ :severities }; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
264
|
|
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
5
|
|
815
|
use base 'Perl::Critic::Policy'; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
2041
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '2.05'; |
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
|
1744
|
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
|
254488
|
return qw< |
28
|
|
|
|
|
|
|
PPI::Token::Word |
29
|
|
|
|
|
|
|
>; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub violates { |
35
|
18
|
|
|
18
|
1
|
655
|
my ( $self, $elem, $doc ) = @_; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# other statements than grep aren't catched |
38
|
18
|
100
|
|
|
|
48
|
return if $elem->content ne 'grep'; |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
|
|
44
|
my $parent = $elem->parent; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# grep in boolean or void context isn't checked |
43
|
6
|
100
|
|
|
|
49
|
return if !$parent->isa('PPI::Statement::Variable'); |
44
|
|
|
|
|
|
|
|
45
|
5
|
|
|
20
|
|
29
|
my $list = first{ $_->isa('PPI::Structure::List') } $parent->schildren; |
|
20
|
|
|
|
|
174
|
|
46
|
5
|
100
|
|
|
|
42
|
return if !$list; |
47
|
|
|
|
|
|
|
|
48
|
3
|
|
|
|
|
14
|
my $symbols = $list->find('PPI::Token::Symbol'); |
49
|
|
|
|
|
|
|
|
50
|
3
|
50
|
|
|
|
824
|
return if !$symbols; |
51
|
3
|
100
|
|
|
|
7
|
return if 1 != @{ $symbols }; |
|
3
|
|
|
|
|
14
|
|
52
|
|
|
|
|
|
|
|
53
|
2
|
100
|
|
|
|
10
|
return if '$' ne substr $symbols->[0], 0, 1; |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
16
|
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.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__ |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|