File Coverage

blib/lib/Perl/Critic/Policy/OTRS/RequireCamelCase.pm
Criterion Covered Total %
statement 43 45 95.5
branch 14 22 63.6
condition 12 18 66.6
subroutine 12 12 100.0
pod 5 5 100.0
total 86 102 84.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::OTRS::RequireCamelCase;
2              
3             # ABSTRACT: Variable, subroutine, and package names have to be in CamelCase
4              
5 24     24   14452 use strict;
  24         56  
  24         667  
6 24     24   117 use warnings;
  24         93  
  24         679  
7              
8 24     24   128 use Perl::Critic::Utils qw{ :severities :classification :ppi };
  24         53  
  24         1167  
9 24     24   8180 use base 'Perl::Critic::Policy';
  24         52  
  24         2206  
10              
11 24     24   159 use Readonly;
  24         50  
  24         15321  
12              
13             our $VERSION = '0.03';
14              
15             Readonly::Scalar my $DESC => q{Variable, subroutine, and package names have to be in CamelCase};
16             Readonly::Scalar my $EXPL => q{};
17              
18 12     12 1 28576 sub supported_parameters { return; }
19 6     6 1 54 sub default_severity { return $SEVERITY_HIGHEST; }
20 1     1 1 701 sub default_themes { return qw( otrs otrs_lt_3_3 ) }
21              
22             my %dispatcher = (
23             'PPI::Statement::Sub' => \&_is_camelcase,
24             'PPI::Statement::Package' => \&_is_camelcase,
25             'PPI::Token::Symbol' => \&_variable_is_camelcase,
26             );
27              
28             sub applies_to {
29 3     3 1 191576 sort keys %dispatcher,
30             }
31              
32             sub violates {
33 13     13 1 1023 my ( $self, $elem ) = @_;
34              
35 13         27 my $ref = ref $elem;
36 13         17 my $sub = $dispatcher{$ref};
37 13 50       29 return if !$sub;
38              
39 13         27 my $success = $self->$sub( $elem );
40              
41 13 100       35 return if $success;
42 5         19 return $self->violation( $DESC, $EXPL, $elem );
43             }
44              
45             sub _is_camelcase {
46 5     5   11 my ( $self, $elem ) = @_;
47              
48 5         19 my $words = $elem->find( 'PPI::Token::Word' );
49 5         3062 my $name = $words->[1];
50              
51 5 100 100     42 if ( $elem->isa( 'PPI::Statement::Sub' ) and $name eq 'new' ) {
    50 66        
    100 66        
    50 33        
52 1         18 return 1;
53             }
54             elsif ( $elem->isa( 'PPI::Statement::Package' ) and $name eq 'main' ) {
55 0         0 return 1;
56             }
57             elsif ( $elem->isa( 'PPI::Statement::Package' ) and $name =~ m{ \A t:: }x ) {
58 2         60 return 1;
59             }
60             elsif ( $elem->isa( 'PPI::Statement::Package' ) and $name =~ m{ Language :: [a-z]{2,3}_ }xms ) {
61 0         0 return 1;
62             }
63              
64 2 50       53 return 1 if !$name;
65              
66 2   66     5 my $is_camelcase = !( $name !~ m{ \A _* [A-Z][a-z]* }xms || $name =~ m{ [^_]_ }xms );
67              
68 2         23 return $is_camelcase;
69             }
70              
71             sub _variable_is_camelcase {
72 8     8   16 my ( $self, $elem ) = @_;
73              
74 8         17 my $name = "$elem";
75              
76             # Allow Perl builtins.
77 8 50       36 return 1 if $name eq '$a';
78 8 50       18 return 1 if $name eq '$b';
79              
80             # Ignore function calls
81 8 50       18 return 1 if substr($name, 0, 1) eq '&';
82              
83             # Allow short variable names with lowercase characters like $s.
84 8 50       16 return 1 if length $name == 2;
85              
86 8   66     33 my $is_camelcase = !( $name !~ m{ \A [\*\@\$\%]_*[A-Z][a-z]* }xms || $name =~ m{ [^_]_ }xms );
87              
88             #print STDERR "$name" if !$is_camelcase;
89              
90 8         17 return $is_camelcase;
91             }
92              
93             1;
94              
95             __END__
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =head1 NAME
102              
103             Perl::Critic::Policy::OTRS::RequireCamelCase - Variable, subroutine, and package names have to be in CamelCase
104              
105             =head1 VERSION
106              
107             version 0.09
108              
109             =head1 METHODS
110              
111             =head2 supported_parameters
112              
113             There are no supported parameters.
114              
115             =head1 AUTHOR
116              
117             Renee Baecker <info@perl-services.de>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             This software is Copyright (c) 2013 by Renee Baecker.
122              
123             This is free software, licensed under:
124              
125             The Artistic License 2.0 (GPL Compatible)
126              
127             =cut