« lib/Perl/Critic/Config.pm
↑ Summary
lib/Perl/Critic/Policy/Variables/ProhibitPerl4PackageNames.pm »
| 1 | package Perl::Critic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use List::SomeUtils qw( firstidx ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use Perl::Critic::Exception::Configuration::Generic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use Perl::Critic::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 40 | ▶ | use Perl::Critic::Violation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 40 | ▶ | use Perl::Critic::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 40 | ▶ | use Perl::Critic::Statistics; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 40 | Readonly::Array our @EXPORT_OK => qw(critique); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #============================================================================= | ||||||||||||||||||||||||||||
| 27 | # PUBLIC methods | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 90 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 90 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 31 | 90 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 32 | 90 | ▶ | $self->{_config} = $args{-config} || Perl::Critic::Config->new( %args ); | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{'-config'} || 'Perl::Critic::Config'->new(%args)
MC/DC0%0/2
$args{'-config'} || 'Perl::Critic::Config'->new(%args)
$args{'-config'}
'Perl::Critic::Config'->new(%args)
Truth table33%1/3
A || B
A= $args{'-config'}B= 'Perl::Critic::Config'->new(%args)
| |||||||||||||||||||||||||||||
| 33 | 89 | $self->{_stats} = Perl::Critic::Statistics->new(); | |||||||||||||||||||||||||||
| 34 | 89 | return $self; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 457 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 457 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 457 | return $self->{_config}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 50 | ▶ | sub add_policy { | ||||||||||||||||||||||||||
|
Subroutine
add_policy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 50 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 48 | #Delegate to Perl::Critic::Config | ||||||||||||||||||||||||||||
| 49 | 50 | return $self->config()->add_policy( @args ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 87 | ▶ | sub policies { | ||||||||||||||||||||||||||
|
Subroutine
policies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 87 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #Delegate to Perl::Critic::Config | ||||||||||||||||||||||||||||
| 58 | 87 | return $self->config()->policies(); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 86 | ▶ | sub statistics { | ||||||||||||||||||||||||||
|
Subroutine
statistics: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 86 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 86 | return $self->{_stats}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 89 | ▶ | sub critique { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
critique: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 73 | # This subroutine can be called as an object method or as a static | ||||||||||||||||||||||||||||
| 74 | # function. In the latter case, the first argument can be a | ||||||||||||||||||||||||||||
| 75 | # hashref of configuration parameters that shall be used to create | ||||||||||||||||||||||||||||
| 76 | # an object behind the scenes. Note that this object does not | ||||||||||||||||||||||||||||
| 77 | # persist. In other words, it is not a singleton. Here are some | ||||||||||||||||||||||||||||
| 78 | # of the ways this subroutine might get called: | ||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||
| 80 | # #Object style... | ||||||||||||||||||||||||||||
| 81 | # $critic->critique( $code ); | ||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||
| 83 | # #Functional style... | ||||||||||||||||||||||||||||
| 84 | # critique( $code ); | ||||||||||||||||||||||||||||
| 85 | # critique( {}, $code ); | ||||||||||||||||||||||||||||
| 86 | # critique( {-foo => bar}, $code ); | ||||||||||||||||||||||||||||
| 87 | #------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 89 | ▶ | my ( $self, $source_code ) = @_ >= 2 ? @_ : ( {}, $_[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 89 | ▶ | $self = ref $self eq 'HASH' ? __PACKAGE__->new(%{ $self }) : $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 89 | ▶ | return if not defined $source_code; # If no code, then nothing to do. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 87 | my $config = $self->config(); | |||||||||||||||||||||||||||
| 94 | 87 | ▶ | my $doc = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($source_code) and $source_code->isa("Perl::Critic::Document")
MC/DC50%1/2
builtin::blessed($source_code) and $source_code->isa("Perl::Critic::Document")
builtin::blessed($source_code)
$source_code->isa("Perl::Critic::Document")
Truth table66%2/3
A and B
A= builtin::blessed($source_code)B= $source_code->isa("Perl::Critic::Document")
| |||||||||||||||||||||||||||||
| 95 | blessed($source_code) && $source_code->isa('Perl::Critic::Document') | ||||||||||||||||||||||||||||
| 96 | ? $source_code | ||||||||||||||||||||||||||||
| 97 | : Perl::Critic::Document->new( | ||||||||||||||||||||||||||||
| 98 | '-source' => $source_code, | ||||||||||||||||||||||||||||
| 99 | '-program-extensions' => [$config->program_extensions_as_regexes()], | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 87 | ▶ | if ( 0 == $self->policies() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 2 | Perl::Critic::Exception::Configuration::Generic->throw( | |||||||||||||||||||||||||||
| 104 | message => 'There are no enabled policies.', | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 85 | return $self->_gather_violations($doc); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #============================================================================= | ||||||||||||||||||||||||||||
| 112 | # PRIVATE methods | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 85 | ▶ | sub _gather_violations { | ||||||||||||||||||||||||||
|
Subroutine
_gather_violations: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 115 | 85 | my ($self, $doc) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Disable exempt code lines, if desired | ||||||||||||||||||||||||||||
| 118 | 85 | ▶ | if ( not $self->config->force() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 82 | $doc->process_annotations(); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Evaluate each policy | ||||||||||||||||||||||||||||
| 123 | 85 | my @policies = $self->config->policies(); | |||||||||||||||||||||||||||
| 124 | 85 | my @ordered_policies = _futz_with_policy_order(@policies); | |||||||||||||||||||||||||||
| 125 | 85 | my @violations = map { _critique($_, $doc) } @ordered_policies; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Accumulate statistics | ||||||||||||||||||||||||||||
| 128 | 85 | $self->statistics->accumulate( $doc, \@violations ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # If requested, rank violations by their severity and return the top N. | ||||||||||||||||||||||||||||
| 131 | 85 | ▶ | if ( @violations && (my $top = $self->config->top()) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@violations and my $top = $self->config->top
MC/DC0%0/2
@violations and my $top = $self->config->top
@violations
my $top = $self->config->top
Truth table66%2/3
A and B
A= @violationsB= my $top = $self->config->top
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $limit = @violations < $top ? $#violations : $top-1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | @violations = Perl::Critic::Violation::sort_by_severity(@violations); | |||||||||||||||||||||||||||
| 134 | 0 | @violations = ( reverse @violations )[ 0 .. $limit ]; #Slicing... | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Always return violations sorted by location | ||||||||||||||||||||||||||||
| 138 | 85 | return Perl::Critic::Violation->sort_by_location(@violations); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | #============================================================================= | ||||||||||||||||||||||||||||
| 142 | # PRIVATE functions | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 4494 | ▶ | sub _critique { | ||||||||||||||||||||||||||
|
Subroutine
_critique: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 145 | 4494 | my ($policy, $doc) = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4494 | ▶ | return if not $policy->prepare_to_scan_document($doc); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4490 | my $maximum_violations = $policy->get_maximum_violations_per_document(); | |||||||||||||||||||||||||||
| 150 | 4490 | ▶ | return if defined $maximum_violations && $maximum_violations == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $maximum_violations and $maximum_violations == 0
MC/DC0%0/2
defined $maximum_violations and $maximum_violations == 0
defined $maximum_violations
$maximum_violations == 0
Truth table66%2/3
A and B
A= defined $maximum_violationsB= $maximum_violations == 0
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4490 | my @violations; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | TYPE: | ||||||||||||||||||||||||||||
| 155 | 4490 | for my $type ( $policy->applies_to() ) { | |||||||||||||||||||||||||||
| 156 | 5949 | my @elements; | |||||||||||||||||||||||||||
| 157 | 5949 | ▶ | if ($type eq 'PPI::Document') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 494 | @elements = ($doc); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 5455 | ▶ | @elements = @{ $doc->find($type) || [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$doc->find($type) || []
MC/DC100%1/1
$doc->find($type) || []
$doc->find($type)
Truth table100%2/2
A
A= $doc->find($type)
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | ELEMENT: | ||||||||||||||||||||||||||||
| 165 | 5949 | for my $element (@elements) { | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Evaluate the policy on this $element. A policy may | ||||||||||||||||||||||||||||
| 168 | # return zero or more violations. We only want the | ||||||||||||||||||||||||||||
| 169 | # violations that occur on lines that have not been | ||||||||||||||||||||||||||||
| 170 | # disabled. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | VIOLATION: | ||||||||||||||||||||||||||||
| 173 | 23535 | for my $violation ( $policy->violates( $element, $doc ) ) { | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 266 | my $line = $violation->location()->[0]; | |||||||||||||||||||||||||||
| 176 | 266 | ▶ | if ( $doc->line_is_disabled_for_policy($line, $policy) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 107 | $doc->add_suppressed_violation($violation); | |||||||||||||||||||||||||||
| 178 | 107 | next VIOLATION; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 159 | push @violations, $violation; | |||||||||||||||||||||||||||
| 182 | 159 | ▶ | last TYPE if defined $maximum_violations and @violations >= $maximum_violations; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $maximum_violations and @violations >= $maximum_violations
MC/DC50%1/2
defined $maximum_violations and @violations >= $maximum_violations
defined $maximum_violations
@violations >= $maximum_violations
Truth table66%2/3
A and B
A= defined $maximum_violationsB= @violations >= $maximum_violations
| |||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 4490 | return @violations; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 85 | ▶ | sub _futz_with_policy_order { | ||||||||||||||||||||||||||
|
Subroutine
_futz_with_policy_order: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | # The ProhibitUselessNoCritic policy is another special policy. It | ||||||||||||||||||||||||||||
| 194 | # deals with the violations that *other* Policies produce. Therefore | ||||||||||||||||||||||||||||
| 195 | # it needs to be run *after* all the other Policies. TODO: find | ||||||||||||||||||||||||||||
| 196 | # a way for Policies to express an ordering preference somehow. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 85 | my @policy_objects = @_; | |||||||||||||||||||||||||||
| 199 | 85 | my $magical_policy_name = 'Perl::Critic::Policy::Miscellanea::ProhibitUselessNoCritic'; | |||||||||||||||||||||||||||
| 200 | 85 | ▶ | my $idx = firstidx {ref $_ eq $magical_policy_name} @policy_objects; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 201 | 85 | push @policy_objects, splice @policy_objects, $idx, 1; | |||||||||||||||||||||||||||
| 202 | 85 | return @policy_objects; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 40 | 1; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||