| 1 | ####################################################################### | ||||||||||||||||||||||||||||
| 2 | # $URL: http://perlcritic.tigris.org/svn/perlcritic/tags/criticism-1.02/lib/criticism.pm $ | ||||||||||||||||||||||||||||
| 3 | # $Date: 2008-07-27 16:11:59 -0700 (Sun, 27 Jul 2008) $ | ||||||||||||||||||||||||||||
| 4 | # $Author: thaljef $ | ||||||||||||||||||||||||||||
| 5 | # $Revision: 203 $ | ||||||||||||||||||||||||||||
| 6 | ######################################################################## | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package criticism; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | our $VERSION = 1.02; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | # We could use the SEVERITY constants from Perl::Critic instead of magic | ||||||||||||||||||||||||||||
| 21 | # numbers. That would require us to load Perl::Critic, but this pragma | ||||||||||||||||||||||||||||
| 22 | # must fail gracefully if Perl::Critic is not available. Therefore, we're | ||||||||||||||||||||||||||||
| 23 | # going to tolerate the magic numbers. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | ## no critic (ProhibitMagicNumbers); | ||||||||||||||||||||||||||||
| 26 | 3 | my %SEVERITY_OF = ( | |||||||||||||||||||||||||||
| 27 | gentle => 5, | ||||||||||||||||||||||||||||
| 28 | stern => 4, | ||||||||||||||||||||||||||||
| 29 | harsh => 3, | ||||||||||||||||||||||||||||
| 30 | cruel => 2, | ||||||||||||||||||||||||||||
| 31 | brutal => 1, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | ## use critic; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | my $DEFAULT_MOOD = 'gentle'; | |||||||||||||||||||||||||||
| 36 | 3 | my $DEFAULT_VERBOSE = "%m at %f line %l.\n"; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 9 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 9 | my ($pkg, @args) = @_; | |||||||||||||||||||||||||||
| 43 | 9 | my $file = (caller)[1]; | |||||||||||||||||||||||||||
| 44 | 9 | ▶ | return 1 if not -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 9 | my %pc_args = _make_pc_args( @args ); | |||||||||||||||||||||||||||
| 46 | 8 | return _critique( $file, %pc_args ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 18 | ▶ | sub _make_pc_args { | ||||||||||||||||||||||||||
|
Subroutine
_make_pc_args: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 18 | my (@args) = @_; | |||||||||||||||||||||||||||
| 54 | 18 | my %pc_args = (); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 18 | ▶ | if (@args <= 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 13 | ▶ | my $mood = $args[0] || $DEFAULT_MOOD; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args[0] || $DEFAULT_MOOD
MC/DC0%0/2
$args[0] || $DEFAULT_MOOD
$args[0]
$DEFAULT_MOOD
Truth table66%2/3
A || B
A= $args[0]B= $DEFAULT_MOOD
| |||||||||||||||||||||||||||||
| 58 | 13 | ▶ | my $severity = $SEVERITY_OF{$mood} || _throw_mood_exception( $mood ); | ||||||||||||||||||||||||||
|
Condition66%2/3
$SEVERITY_OF{$mood} || _throw_mood_exception($mood)
MC/DC0%0/2
$SEVERITY_OF{$mood} || _throw_mood_exception($mood)
$SEVERITY_OF{$mood}
_throw_mood_exception($mood)
Truth table33%1/3
A || B
A= $SEVERITY_OF{$mood}B= _throw_mood_exception($mood)
| |||||||||||||||||||||||||||||
| 59 | 12 | %pc_args = (-severity => $severity, -verbose => $DEFAULT_VERBOSE); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 5 | %pc_args = @args; | |||||||||||||||||||||||||||
| 63 | 5 | ▶ | $pc_args{-verbose} ||= $DEFAULT_VERBOSE; | ||||||||||||||||||||||||||
|
Condition66%2/3
$pc_args{"-verbose"} ||= $DEFAULT_VERBOSE
MC/DC0%0/2
$pc_args{"-verbose"} ||= $DEFAULT_VERBOSE
$pc_args{"-verbose"}
$DEFAULT_VERBOSE
Truth table66%2/3
A ||= B
A= $pc_args{"-verbose"}B= $DEFAULT_VERBOSE
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 17 | return %pc_args; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | sub _critique { | ||||||||||||||||||||||||||
|
Subroutine
_critique: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 8 | my ($file, %pc_args) = @_; | |||||||||||||||||||||||||||
| 74 | 8 | my @violations = (); | |||||||||||||||||||||||||||
| 75 | 8 | my $critic = undef; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | eval { | ||||||||||||||||||||||||||||
| 78 | 8 | require Perl::Critic; | |||||||||||||||||||||||||||
| 79 | 8 | require Perl::Critic::Violation; | |||||||||||||||||||||||||||
| 80 | 8 | $critic = Perl::Critic->new( %pc_args ); | |||||||||||||||||||||||||||
| 81 | 8 | my $verbose = $critic->config->verbose(); | |||||||||||||||||||||||||||
| 82 | 8 | Perl::Critic::Violation::set_format($verbose); | |||||||||||||||||||||||||||
| 83 | 8 | @violations = $critic->critique($file); | |||||||||||||||||||||||||||
| 84 | 8 | print {*STDERR} @violations; | |||||||||||||||||||||||||||
| 85 | 8 | 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 8 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($ENV{DEBUG} || $PERLDB) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{"DEBUG"} or $criticism::PERLDB
MC/DC0%0/2
$ENV{"DEBUG"} or $criticism::PERLDB
$ENV{"DEBUG"}
$criticism::PERLDB
Truth table0%0/3
A or B
A= $ENV{"DEBUG"}B= $criticism::PERLDB
| |||||||||||||||||||||||||||||
| 89 | 0 | carp qq{'criticism' failed to load: $EVAL_ERROR}; | |||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 8 | ▶ | die "Refusing to continue due to Perl::Critic violations.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@violations and $critic->config->criticism_fatal
MC/DC100%2/2
@violations and $critic->config->criticism_fatal
@violations
$critic->config->criticism_fatal
Truth table100%3/3
A and B
A= @violationsB= $critic->config->criticism_fatal
| |||||||||||||||||||||||||||||
| 95 | if @violations && $critic->config->criticism_fatal(); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 7 | ▶ | return @violations ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub _throw_mood_exception { | ||||||||||||||||||||||||||
|
Subroutine
_throw_mood_exception: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 1 | my ($mood) = @_; | |||||||||||||||||||||||||||
| 104 | 1 | my @moods = keys %SEVERITY_OF; | |||||||||||||||||||||||||||
| 105 | 1 | @moods = reverse sort { $SEVERITY_OF{$a} <=> $SEVERITY_OF{$b} } @moods; | |||||||||||||||||||||||||||
| 106 | 1 | croak qq{"$mood" criticism not supported. Choose from: @moods}; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||