| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Module::Checkstyle::Check::Variable; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
3669
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
285
|
|
|
4
|
2
|
|
|
2
|
|
14
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
81
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
279
|
|
|
7
|
2
|
|
|
2
|
|
3301
|
use Lingua::EN::Inflect::Number qw(number); |
|
|
2
|
|
|
|
|
78474
|
|
|
|
2
|
|
|
|
|
28
|
|
|
8
|
2
|
|
|
2
|
|
1581
|
use Readonly; |
|
|
2
|
|
|
|
|
3068
|
|
|
|
2
|
|
|
|
|
113
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
616
|
use Module::Checkstyle::Util qw(:args :problem); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
336
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
13
|
use base qw(Module::Checkstyle::Check); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
1406
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# The directives we provide |
|
15
|
|
|
|
|
|
|
Readonly my $MATCHES_NAME => 'matches-name'; |
|
16
|
|
|
|
|
|
|
Readonly my $ARRAYS_IN_PLURAL => 'arrays-in-plural'; |
|
17
|
|
|
|
|
|
|
Readonly my $HASHES_IN_SINGULAR => 'hashes-in-singular'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register { |
|
20
|
|
|
|
|
|
|
return ( |
|
21
|
0
|
|
|
0
|
1
|
0
|
'PPI::Statement::Variable' => \&handle_declaration, |
|
22
|
|
|
|
|
|
|
); |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
|
26
|
2
|
|
|
2
|
1
|
4
|
my ($class, $config) = @_; |
|
27
|
|
|
|
|
|
|
|
|
28
|
2
|
|
|
|
|
18
|
my $self = $class->SUPER::new($config); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Keep configuration local |
|
31
|
2
|
|
|
|
|
9
|
$self->{$MATCHES_NAME} = as_regexp($config->get_directive($MATCHES_NAME)); |
|
32
|
2
|
|
|
|
|
16
|
$self->{$ARRAYS_IN_PLURAL} = as_true($config->get_directive($ARRAYS_IN_PLURAL)); |
|
33
|
2
|
|
|
|
|
17
|
$self->{$HASHES_IN_SINGULAR} = as_true($config->get_directive($HASHES_IN_SINGULAR)); |
|
34
|
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
15
|
return $self; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub handle_declaration { |
|
39
|
17
|
|
|
17
|
1
|
31913
|
my ($self, $declaration, $file) = @_; |
|
40
|
|
|
|
|
|
|
|
|
41
|
17
|
|
|
|
|
65
|
my @variables = $declaration->variables(); |
|
42
|
17
|
|
|
|
|
1141
|
return $self->_check_variables($declaration, $file, @variables); |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _check_variables { |
|
46
|
17
|
|
|
17
|
|
38
|
my ($self, $declaration, $file, @variables) = @_; |
|
47
|
|
|
|
|
|
|
|
|
48
|
17
|
|
|
|
|
27
|
my @problems; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
CHECK_VARIABLE: |
|
51
|
17
|
|
|
|
|
29
|
foreach my $variable (@variables) { |
|
52
|
21
|
|
|
|
|
51
|
my $type = substr($variable, 0, 1); |
|
53
|
21
|
|
|
|
|
40
|
my $name = substr($variable, 1); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Ignore "built-in" arrays and hashes |
|
56
|
21
|
50
|
66
|
|
|
98
|
next CHECK_VARIABLE if $type eq '@' && $name =~ /^ISA|EXPORT|EXPORT_OK$/; |
|
57
|
21
|
50
|
66
|
|
|
77
|
next CHECK_VARIABLE if $type eq '%' && $name =~ /^EXPORT_TAGS$/; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# matches-name |
|
60
|
21
|
100
|
|
|
|
80
|
if ($self->{$MATCHES_NAME}) { |
|
61
|
10
|
100
|
66
|
|
|
89
|
if ($name && $name !~ $self->{$MATCHES_NAME}) { |
|
62
|
3
|
|
|
|
|
47
|
push @problems, new_problem($self->config, $MATCHES_NAME, |
|
63
|
|
|
|
|
|
|
qq(Variable '$variable' does not match '$self->{$MATCHES_NAME}'), |
|
64
|
|
|
|
|
|
|
$declaration, $file); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# arrays-in-plural |
|
69
|
21
|
100
|
100
|
|
|
191
|
if ($type eq '@' && $self->{$ARRAYS_IN_PLURAL}) { |
|
70
|
5
|
|
|
|
|
71
|
my ($last_word) = $name =~ /([A-Z]?(?:[a-z0-9]+|[A-Z0-9]+))$/; |
|
71
|
5
|
100
|
|
|
|
25
|
if (number(lc($last_word)) ne 'p') { |
|
72
|
1
|
|
|
|
|
7594
|
push @problems, new_problem($self->config, $ARRAYS_IN_PLURAL, |
|
73
|
|
|
|
|
|
|
qq(Variable '$variable' is an array and must be named in plural), |
|
74
|
|
|
|
|
|
|
$declaration, $file); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# hashes-in-singular |
|
79
|
21
|
100
|
100
|
|
|
1930
|
if ($type eq '%' && $self->{$HASHES_IN_SINGULAR}) { |
|
80
|
5
|
|
|
|
|
72
|
my ($last_word) = $name =~ /([A-Z]?(?:[a-z0-9]+|[A-Z0-9]+))$/; |
|
81
|
5
|
100
|
|
|
|
20
|
if (number(lc($last_word)) ne 's') { |
|
82
|
1
|
|
|
|
|
374
|
push @problems, new_problem($self->config, $HASHES_IN_SINGULAR, |
|
83
|
|
|
|
|
|
|
qq(Variable '$variable' is an hash and must be named in singular), |
|
84
|
|
|
|
|
|
|
$declaration, $file); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
17
|
|
|
|
|
2477
|
return @problems; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
|
93
|
|
|
|
|
|
|
__END__ |