lib/Perl/Critic/Violation.pm

filter: Statementstmt 98.6% 147 / 149 Branchbran 85.7% 24 / 28 Conditioncond 68.9% 20 / 29 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 95.4% 42 / 44 Podpod 100% 20 / 20 totaltotal 90.5% 260 / 287 CC 28 SCAR 11.2
CC
28
Cov
93%
_line_containing_violation
7.1
sort_by_location
6.1
new
6.0
CRAP 3.1
« lib/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitReturnSort.pm »
1 package Perl::Critic::Violation;
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 File::Basename qw< basename >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use String::Format qw< stringf >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use overload ( q{""} => 'to_string', cmp => '_compare' );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 ▶use Perl::Critic::Utils qw< :characters :internal_lookup >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Perl::Critic::Utils::POD qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
17 get_pod_section_for_module
18 trim_pod_section
19 40 >;
20 40 ▶use Perl::Critic::Exception::Fatal::Internal qw< throw_internal >;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 40 our $VERSION = '1.156';
23  
24 40 Readonly::Scalar my $NO_EXCEPTION_NO_SPLIT_LIMIT => -1;
25 40 Readonly::Scalar my $LOCATION_LINE_NUMBER => 0;
26 40 Readonly::Scalar my $LOCATION_COLUMN_NUMBER => 1;
27 40 Readonly::Scalar my $LOCATION_VISUAL_COLUMN_NUMBER => 2;
28 40 Readonly::Scalar my $LOCATION_LOGICAL_LINE_NUMBER => 3;
29 40 Readonly::Scalar my $LOCATION_LOGICAL_FILENAME => 4;
30  
31 # Class variables...
32 40 my $format = "%m at line %l, column %c. %e.\n"; # Default stringy format
33 40 my %diagnostics;
34  
35 #-----------------------------------------------------------------------------
36  
37 40 Readonly::Scalar my $CONSTRUCTOR_ARG_COUNT => 5;
38  
39 297 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
40 297 my ( $class, $desc, $expl, $elem, $sev ) = @_;
41  
42 # Check arguments to help out developers who might
43 # be creating new Perl::Critic::Policy modules.
44  
45 297 ▶ if ( @_ != $CONSTRUCTOR_ARG_COUNT ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != $CONSTRUCTOR_ARG_COUNT) 1 296 297
46 1 throw_internal 'Wrong number of args to Violation->new()';
47 }
48  
49 296 ▶ if ( eval { $elem->isa( 'Perl::Critic::Document' ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $elem->isa("Perl::Critic::Document") } }) 6 290 296
50 # break the facade, return the real PPI::Document
51 6 $elem = $elem->ppi_document();
52 }
53  
54 296 ▶ if ( not eval { $elem->isa( 'PPI::Element' ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $elem->isa("PPI::Element") } }) 1 295 296
55 1 throw_internal '3rd arg to Violation->new() must be a PPI::Element';
56 }
57  
58 # Strip punctuation. These are controlled by the user via the
59 # formats. He/She can use whatever makes sense to them.
60 295 ($desc, $expl) = _chomp_periods($desc, $expl);
61  
62 # Create object
63 295 my $self = bless {}, $class;
64 295 $self->{_description} = $desc;
65 295 $self->{_explanation} = $expl;
66 295 $self->{_severity} = $sev;
67 295 $self->{_policy} = caller;
68  
69 # PPI eviscerates the Elements in a Document when the Document gets
70 # DESTROY()ed, and thus they aren't useful after it is gone. So we have
71 # to preemptively grab everything we could possibly want.
72 295 $self->{_element_class} = blessed $elem;
73  
74 295 my $top = $elem->top();
75 295 ▶ $self->{_filename} = $top->can('filename') ? $top->filename() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$top->can('filename') ? : 231 64 295
76 295 $self->{_source} = _line_containing_violation( $elem );
77 $self->{_location} =
78 295 ▶ $elem->location() || [ 0, 0, 0, 0, $self->filename() ];
Condition50%1/2
$elem->location || [0, 0, 0, 0, $self->filename]
l!l
2950
MC/DC0%0/1
$elem->location || [0, 0, 0, 0, $self->filename]
$elem->location
Truth table50%1/2
A
A= $elem->location
Aresult
11
00
79  
80 295 return $self;
81 }
82  
83 #-----------------------------------------------------------------------------
84  
85 20 ▶sub set_format { return $format = verbosity_to_format( $_[0] ); } ## no critic(ArgUnpacking)
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
86 6 ▶sub get_format { return $format; }
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
87  
88 #-----------------------------------------------------------------------------
89  
90 86 ▶sub sort_by_location { ## no critic(ArgUnpacking)
Subroutine sort_by_location: called CC=6 SCAR=18.0
Pod documented
91  
92 86 ▶ ref $_[0] || shift; # Can call as object or class method
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or shift() 0 86 86
93 86 ▶ return scalar @_ if ! wantarray; # In case we are called in scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 56 30 86
94  
95 ## TODO: What if $a and $b are not Violation objects?
96 return
97 84 map {$_->[0]}
98 77 ▶ sort { ($a->[1] <=> $b->[1]) || ($a->[2] <=> $b->[2]) }
Condition100%3/3
$Perl::Critic::Violation::a->[1] <=> $Perl::Critic::Violation::b->[1] || $Perl::Critic::Violation::a->[2] <=> $Perl::Critic::Violation::b->[2]
l!l&&r!l&&!r
471515
MC/DC100%2/2
$Perl::Critic::Violation::a->[1] <=> $Perl::Critic::Violation::b->[1] || $Perl::Critic::Violation::a->[2] <=> $Perl::Critic::Violation::b->[2]
$Perl::Critic::Violation::a->[1] <=> $Perl::Critic::Violation::b->[1] $Perl::Critic::Violation::a->[2] <=> $Perl::Critic::Violation::b->[2]
Truth table100%3/3
A || B
A= $Perl::Critic::Violation::a->[1] <=> $Perl::Critic::Violation::b->[1]B= $Perl::Critic::Violation::a->[2] <=> $Perl::Critic::Violation::b->[2]
ABresult
1X1
011
000
99 30 ▶ map {[$_, $_->location->[0] || 0, $_->location->[1] || 0]}
Condition75%3/4
$_->location->[0] || 0
l!l
840
$_->location->[1] || 0
l!l
7212
MC/DC0%0/1
$_->location->[0] || 0
$_->location->[0]
MC/DC100%1/1
$_->location->[1] || 0
$_->location->[1]
Truth table50%1/2
A
A= $_->location->[0]
Aresult
11
00
Truth table100%2/2
A
A= $_->location->[1]
Aresult
11
00
100 @_;
101 }
102  
103 #-----------------------------------------------------------------------------
104  
105 1 ▶sub sort_by_severity { ## no critic(ArgUnpacking)
Subroutine sort_by_severity: called CC=4 SCAR=14.0
Pod documented
106  
107 1 ▶ ref $_[0] || shift; # Can call as object or class method
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or shift() 0 1 1
108 1 ▶ return scalar @_ if ! wantarray; # In case we are called in scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
109  
110 ## TODO: What if $a and $b are not Violation objects?
111 return
112 6 map {$_->[0]}
113 9 sort { $a->[1] <=> $b->[1] }
114 1 ▶ map {[$_, $_->severity() || 0]}
Condition100%2/2
$_->severity || 0
l!l
51
MC/DC100%1/1
$_->severity || 0
$_->severity
Truth table100%2/2
A
A= $_->severity
Aresult
11
00
115 @_;
116 }
117  
118 #-----------------------------------------------------------------------------
119  
120 474 ▶sub location {
Subroutine location: called CC=1 SCAR=0.0
Pod documented
121 474 my $self = shift;
122  
123 474 return $self->{_location};
124 }
125  
126 #-----------------------------------------------------------------------------
127  
128 4 ▶sub line_number {
Subroutine line_number: called CC=1 SCAR=0.0
Pod documented
129 4 my ($self) = @_;
130  
131 4 return $self->location()->[$LOCATION_LINE_NUMBER];
132 }
133  
134 #-----------------------------------------------------------------------------
135  
136 16 ▶sub logical_line_number {
Subroutine logical_line_number: called CC=1 SCAR=0.0
Pod documented
137 16 my ($self) = @_;
138  
139 16 return $self->location()->[$LOCATION_LOGICAL_LINE_NUMBER];
140 }
141  
142 #-----------------------------------------------------------------------------
143  
144 2 ▶sub column_number {
Subroutine column_number: called CC=1 SCAR=0.0
Pod documented
145 2 my ($self) = @_;
146  
147 2 return $self->location()->[$LOCATION_COLUMN_NUMBER];
148 }
149  
150 #-----------------------------------------------------------------------------
151  
152 14 ▶sub visual_column_number {
Subroutine visual_column_number: called CC=1 SCAR=0.0
Pod documented
153 14 my ($self) = @_;
154  
155 14 return $self->location()->[$LOCATION_VISUAL_COLUMN_NUMBER];
156 }
157  
158 #-----------------------------------------------------------------------------
159  
160 7 ▶sub diagnostics {
Subroutine diagnostics: called CC=4 SCAR=14.0
Pod documented
161 7 my ($self) = @_;
162 7 my $policy = $self->policy();
163  
164 7 ▶ if ( not $diagnostics{$policy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diagnostics{$policy}) 3 4 7
165 3 eval { ## no critic (RequireCheckingReturnValueOfEval)
166 3 ▶ my $module_name = ref $policy || $policy;
Condition33%1/3
ref $policy || $policy
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $policy || $policy
ref $policy $policy
Truth table33%1/3
A || B
A= ref $policyB= $policy
ABresult
1X1
011
000
167 3 $diagnostics{$policy} =
168 trim_pod_section(
169 get_pod_section_for_module( $module_name, 'DESCRIPTION' )
170 );
171 };
172 3 ▶ $diagnostics{$policy} ||= " No diagnostics available\n";
Condition100%2/2
$diagnostics{$policy} ||= " No diagnostics available\n"
l!l
21
MC/DC100%1/1
$diagnostics{$policy} ||= " No diagnostics available\n"
$diagnostics{$policy}
Truth table100%2/2
A
A= $diagnostics{$policy}
Aresult
11
00
173 }
174 7 return $diagnostics{$policy};
175 }
176  
177 #-----------------------------------------------------------------------------
178  
179 26 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
180 26 my $self = shift;
181 26 return $self->{_description};
182 }
183  
184 #-----------------------------------------------------------------------------
185  
186 28 ▶sub explanation {
Subroutine explanation: called CC=4 SCAR=13.9
Pod documented
187 28 my $self = shift;
188 28 my $expl = $self->{_explanation};
189 28 ▶ if ( !$expl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expl) 20 8 28
190 20 $expl = '(no explanation)';
191 }
192 28 ▶ if ( ref $expl eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expl eq "ARRAY") 2 26 28
193 2 ▶ my $page = @{$expl} > 1 ? 'pages' : 'page';
Branch: 1 branch
BranchTrueFalseTotal
@{$expl;} > 1 ? : 1 1 2
194 2 $page .= $SPACE . join $COMMA, @{$expl};
195 2 $expl = "See $page of PBP";
196 }
197 28 return $expl;
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 197 ▶sub severity {
Subroutine severity: called CC=1 SCAR=0.0
Pod documented
203 197 my $self = shift;
204 197 return $self->{_severity};
205 }
206  
207 #-----------------------------------------------------------------------------
208  
209 192 ▶sub policy {
Subroutine policy: called CC=1 SCAR=0.0
Pod documented
210 192 my $self = shift;
211 192 return $self->{_policy};
212 }
213  
214 #-----------------------------------------------------------------------------
215  
216 4 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
217 4 my $self = shift;
218 4 return $self->{_filename};
219 }
220  
221 #-----------------------------------------------------------------------------
222  
223 4 ▶sub logical_filename {
Subroutine logical_filename: called CC=1 SCAR=0.0
Pod documented
224 4 my ($self) = @_;
225  
226 4 return $self->location()->[$LOCATION_LOGICAL_FILENAME];
227 }
228  
229 #-----------------------------------------------------------------------------
230  
231 6 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
232 6 my $self = shift;
233 6 return $self->{_source};
234 }
235  
236 #-----------------------------------------------------------------------------
237  
238 2 ▶sub element_class {
Subroutine element_class: called CC=1 SCAR=0.0
Pod documented
239 2 my ($self) = @_;
240  
241 2 return $self->{_element_class};
242 }
243  
244 #-----------------------------------------------------------------------------
245  
246 24 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
247 24 my $self = shift;
248  
249 24 my $long_policy = $self->policy();
250 24 (my $short_policy = $long_policy) =~ s/ \A Perl::Critic::Policy:: //xms;
251  
252 # Wrap the more expensive ones in sub{} to postpone evaluation
253 my %fspec = (
254 2 ▶ 'f' => sub { $self->logical_filename() },
Subroutine __ANON__: called CC=1 SCAR=0.0
255 2 ▶ 'F' => sub { basename( $self->logical_filename() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
256 2 ▶ 'g' => sub { $self->filename() },
Subroutine __ANON__: called CC=1 SCAR=0.0
257 2 ▶ 'G' => sub { basename( $self->filename() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
258 14 ▶ 'l' => sub { $self->logical_line_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
259 2 ▶ 'L' => sub { $self->line_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
260 12 ▶ 'c' => sub { $self->visual_column_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 0 ▶ 'C' => sub { $self->element_class() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 'm' => $self->description(),
263 'e' => $self->explanation(),
264 's' => $self->severity(),
265 4 ▶ 'd' => sub { $self->diagnostics() },
Subroutine __ANON__: called CC=1 SCAR=0.0
266 4 ▶ 'r' => sub { $self->source() },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 24 'P' => $long_policy,
268 'p' => $short_policy,
269 );
270 24 return stringf($format, %fspec);
271 }
272  
273 #-----------------------------------------------------------------------------
274 # Apparently, some perls do not implicitly stringify overloading
275 # objects before doing a comparison. This causes a couple of our
276 # sorting tests to fail. To work around this, we overload C<cmp> to
277 # do it explicitly.
278 #
279 # 20060503 - More information: This problem has been traced to
280 # Test::Simple versions <= 0.60, not perl itself. Upgrading to
281 # Test::Simple v0.62 will fix the problem. But rather than forcing
282 # everyone to upgrade, I have decided to leave this workaround in
283 # place.
284  
285 0 ▶sub _compare { return "$_[0]" cmp "$_[1]" }
Subroutine _compare: not called CC=1 SCAR=6.9
286  
287 #-----------------------------------------------------------------------------
288  
289 295 ▶sub _line_containing_violation {
Subroutine _line_containing_violation: called CC=6 SCAR=19.6
290 295 my ( $elem ) = @_;
291  
292 295 ▶ my $stmnt = $elem->statement() || $elem;
Condition66%2/3
$elem->statement || $elem
l!l&&r!l&&!r
205900
MC/DC0%0/2
$elem->statement || $elem
$elem->statement $elem
Truth table66%2/3
A || B
A= $elem->statementB= $elem
ABresult
1X1
011
000
293 295 ▶ my $code_string = $stmnt->content() || $EMPTY;
Condition33%1/3
$stmnt->content || $Perl::Critic::Violation::EMPTY
l!l&&r!l&&!r
29500
MC/DC0%0/2
$stmnt->content || $Perl::Critic::Violation::EMPTY
$stmnt->content $Perl::Critic::Violation::EMPTY
Truth table33%1/3
A || B
A= $stmnt->contentB= $Perl::Critic::Violation::EMPTY
ABresult
1X1
011
000
294  
295 # Split into individual lines
296 # From `perldoc -f split`:
297 # If LIMIT is negative, it is treated as if it were instead
298 # arbitrarily large; as many fields as possible are produced.
299 #
300 # If it's omitted, it's the same except trailing empty fields, so we need
301 # without a limit for the split and without an exception
302 295 my @lines = split qr{ \n }xms, $code_string, $NO_EXCEPTION_NO_SPLIT_LIMIT;
303  
304 # Take the line containing the element that is in violation
305 295 ▶ my $inx = ( $elem->line_number() || 0 ) -
Condition50%2/4
$elem->line_number || 0
l!l
2950
$stmnt->line_number || 0
l!l
2950
MC/DC0%0/1
$elem->line_number || 0
$elem->line_number
MC/DC0%0/1
$stmnt->line_number || 0
$stmnt->line_number
Truth table50%1/2
A
A= $elem->line_number
Aresult
11
00
Truth table50%1/2
A
A= $stmnt->line_number
Aresult
11
00
306 ( $stmnt->line_number() || 0 );
307 295 ▶ $inx > @lines and return $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
$inx > @lines and return $Perl::Critic::Violation::EMPTY 0 295 295
308 295 return $lines[$inx];
309 }
310  
311 #-----------------------------------------------------------------------------
312  
313 296 ▶sub _chomp_periods {
Subroutine _chomp_periods: called CC=4 SCAR=13.9
314 296 my @args = @_;
315  
316 296 for (@args) {
317 596 ▶ next if not defined or ref;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or ref $_ 199 397 596
Condition100%3/3
not defined $_ or ref $_
l!l&&r!l&&!r
2197397
MC/DC100%2/2
not defined $_ or ref $_
not defined $_ ref $_
Truth table100%3/3
not(A) or B
A= not defined $_B= ref $_
ABresult
1X1
011
000
318 397 s{ [.]+ \z }{}xms;
319 }
320  
321 296 return @args;
322 }
323  
324 #-----------------------------------------------------------------------------
325  
326 40 1;
327  
328 #-----------------------------------------------------------------------------
329  
330 __END__
« lib/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitReturnSort.pm »