/usr/share/perl5/Perl/Critic/Violation.pm is in libperl-critic-perl 1.126-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | package Perl::Critic::Violation;
use 5.006001;
use strict;
use warnings;
use English qw< -no_match_vars >;
use Readonly;
use File::Basename qw< basename >;
use IO::String qw< >;
use Pod::PlainText qw< >;
use Scalar::Util qw< blessed >;
use String::Format qw< stringf >;
use overload ( q{""} => 'to_string', cmp => '_compare' );
use Perl::Critic::Utils qw< :characters :internal_lookup >;
use Perl::Critic::Utils::POD qw<
get_pod_section_for_module
trim_pod_section
>;
use Perl::Critic::Exception::Fatal::Internal qw< throw_internal >;
our $VERSION = '1.126';
Readonly::Scalar my $LOCATION_LINE_NUMBER => 0;
Readonly::Scalar my $LOCATION_COLUMN_NUMBER => 1;
Readonly::Scalar my $LOCATION_VISUAL_COLUMN_NUMBER => 2;
Readonly::Scalar my $LOCATION_LOGICAL_LINE_NUMBER => 3;
Readonly::Scalar my $LOCATION_LOGICAL_FILENAME => 4;
# Class variables...
my $format = "%m at line %l, column %c. %e.\n"; # Default stringy format
my %diagnostics = (); # Cache of diagnostic messages
#-----------------------------------------------------------------------------
Readonly::Scalar my $CONSTRUCTOR_ARG_COUNT => 5;
sub new {
my ( $class, $desc, $expl, $elem, $sev ) = @_;
# Check arguments to help out developers who might
# be creating new Perl::Critic::Policy modules.
if ( @_ != $CONSTRUCTOR_ARG_COUNT ) {
throw_internal 'Wrong number of args to Violation->new()';
}
if ( eval { $elem->isa( 'Perl::Critic::Document' ) } ) {
# break the facade, return the real PPI::Document
$elem = $elem->ppi_document();
}
if ( not eval { $elem->isa( 'PPI::Element' ) } ) {
throw_internal '3rd arg to Violation->new() must be a PPI::Element';
}
# Strip punctuation. These are controlled by the user via the
# formats. He/She can use whatever makes sense to them.
($desc, $expl) = _chomp_periods($desc, $expl);
# Create object
my $self = bless {}, $class;
$self->{_description} = $desc;
$self->{_explanation} = $expl;
$self->{_severity} = $sev;
$self->{_policy} = caller;
# PPI eviscerates the Elements in a Document when the Document gets
# DESTROY()ed, and thus they aren't useful after it is gone. So we have
# to preemptively grab everything we could possibly want.
$self->{_element_class} = blessed $elem;
my $top = $elem->top();
$self->{_filename} = $top->can('filename') ? $top->filename() : undef;
$self->{_source} = _line_containing_violation( $elem );
$self->{_location} =
$elem->location() || [ 0, 0, 0, 0, $self->filename() ];
return $self;
}
#-----------------------------------------------------------------------------
sub set_format { return $format = verbosity_to_format( $_[0] ); } ## no critic(ArgUnpacking)
sub get_format { return $format; }
#-----------------------------------------------------------------------------
sub sort_by_location { ## no critic(ArgUnpacking)
ref $_[0] || shift; # Can call as object or class method
return scalar @_ if ! wantarray; # In case we are called in scalar context
## TODO: What if $a and $b are not Violation objects?
return
map {$_->[0]}
sort { ($a->[1] <=> $b->[1]) || ($a->[2] <=> $b->[2]) }
map {[$_, $_->location->[0] || 0, $_->location->[1] || 0]}
@_;
}
#-----------------------------------------------------------------------------
sub sort_by_severity { ## no critic(ArgUnpacking)
ref $_[0] || shift; # Can call as object or class method
return scalar @_ if ! wantarray; # In case we are called in scalar context
## TODO: What if $a and $b are not Violation objects?
return
map {$_->[0]}
sort { $a->[1] <=> $b->[1] }
map {[$_, $_->severity() || 0]}
@_;
}
#-----------------------------------------------------------------------------
sub location {
my $self = shift;
return $self->{_location};
}
#-----------------------------------------------------------------------------
sub line_number {
my ($self) = @_;
return $self->location()->[$LOCATION_LINE_NUMBER];
}
#-----------------------------------------------------------------------------
sub logical_line_number {
my ($self) = @_;
return $self->location()->[$LOCATION_LOGICAL_LINE_NUMBER];
}
#-----------------------------------------------------------------------------
sub column_number {
my ($self) = @_;
return $self->location()->[$LOCATION_COLUMN_NUMBER];
}
#-----------------------------------------------------------------------------
sub visual_column_number {
my ($self) = @_;
return $self->location()->[$LOCATION_VISUAL_COLUMN_NUMBER];
}
#-----------------------------------------------------------------------------
sub diagnostics {
my ($self) = @_;
my $policy = $self->policy();
if ( not $diagnostics{$policy} ) {
eval { ## no critic (RequireCheckingReturnValueOfEval)
my $module_name = ref $policy || $policy;
$diagnostics{$policy} =
trim_pod_section(
get_pod_section_for_module( $module_name, 'DESCRIPTION' )
);
};
$diagnostics{$policy} ||= " No diagnostics available\n";
}
return $diagnostics{$policy};
}
#-----------------------------------------------------------------------------
sub description {
my $self = shift;
return $self->{_description};
}
#-----------------------------------------------------------------------------
sub explanation {
my $self = shift;
my $expl = $self->{_explanation};
if ( !$expl ) {
$expl = '(no explanation)';
}
if ( ref $expl eq 'ARRAY' ) {
my $page = @{$expl} > 1 ? 'pages' : 'page';
$page .= $SPACE . join $COMMA, @{$expl};
$expl = "See $page of PBP";
}
return $expl;
}
#-----------------------------------------------------------------------------
sub severity {
my $self = shift;
return $self->{_severity};
}
#-----------------------------------------------------------------------------
sub policy {
my $self = shift;
return $self->{_policy};
}
#-----------------------------------------------------------------------------
sub filename {
my $self = shift;
return $self->{_filename};
}
#-----------------------------------------------------------------------------
sub logical_filename {
my ($self) = @_;
return $self->location()->[$LOCATION_LOGICAL_FILENAME];
}
#-----------------------------------------------------------------------------
sub source {
my $self = shift;
return $self->{_source};
}
#-----------------------------------------------------------------------------
sub element_class {
my ($self) = @_;
return $self->{_element_class};
}
#-----------------------------------------------------------------------------
sub to_string {
my $self = shift;
my $long_policy = $self->policy();
(my $short_policy = $long_policy) =~ s/ \A Perl::Critic::Policy:: //xms;
# Wrap the more expensive ones in sub{} to postpone evaluation
my %fspec = (
'f' => sub { $self->logical_filename() },
'F' => sub { basename( $self->logical_filename() ) },
'g' => sub { $self->filename() },
'G' => sub { basename( $self->filename() ) },
'l' => sub { $self->logical_line_number() },
'L' => sub { $self->line_number() },
'c' => sub { $self->visual_column_number() },
'C' => sub { $self->element_class() },
'm' => $self->description(),
'e' => $self->explanation(),
's' => $self->severity(),
'd' => sub { $self->diagnostics() },
'r' => sub { $self->source() },
'P' => $long_policy,
'p' => $short_policy,
);
return stringf($format, %fspec);
}
#-----------------------------------------------------------------------------
# Apparently, some perls do not implicitly stringify overloading
# objects before doing a comparison. This causes a couple of our
# sorting tests to fail. To work around this, we overload C<cmp> to
# do it explicitly.
#
# 20060503 - More information: This problem has been traced to
# Test::Simple versions <= 0.60, not perl itself. Upgrading to
# Test::Simple v0.62 will fix the problem. But rather than forcing
# everyone to upgrade, I have decided to leave this workaround in
# place.
sub _compare { return "$_[0]" cmp "$_[1]" }
#-----------------------------------------------------------------------------
sub _line_containing_violation {
my ( $elem ) = @_;
my $stmnt = $elem->statement() || $elem;
my $code_string = $stmnt->content() || $EMPTY;
# Split into individual lines
my @lines = split qr{ \n\s* }xms, $code_string;
# Take the line containing the element that is in violation
my $inx = ( $elem->line_number() || 0 ) -
( $stmnt->line_number() || 0 );
$inx > @lines and return $EMPTY;
return $lines[$inx];
}
#-----------------------------------------------------------------------------
sub _chomp_periods {
my @args = @_;
for (@args) {
next if not defined or ref;
s{ [.]+ \z }{}xms
}
return @args;
}
#-----------------------------------------------------------------------------
1;
#-----------------------------------------------------------------------------
__END__
=head1 NAME
Perl::Critic::Violation - A violation of a Policy found in some source code.
=head1 SYNOPSIS
use PPI;
use Perl::Critic::Violation;
my $elem = $doc->child(0); # $doc is a PPI::Document object
my $desc = 'Offending code'; # Describe the violation
my $expl = [1,45,67]; # Page numbers from PBP
my $sev = 5; # Severity level of this violation
my $vio = Perl::Critic::Violation->new($desc, $expl, $node, $sev);
=head1 DESCRIPTION
Perl::Critic::Violation is the generic representation of an individual
Policy violation. Its primary purpose is to provide an abstraction
layer so that clients of L<Perl::Critic|Perl::Critic> don't have to
know anything about L<PPI|PPI>. The C<violations> method of all
L<Perl::Critic::Policy|Perl::Critic::Policy> subclasses must return a
list of these Perl::Critic::Violation objects.
=head1 INTERFACE SUPPORT
This is considered to be a public class. Any changes to its interface
will go through a deprecation cycle.
=head1 CONSTRUCTOR
=over
=item C<new( $description, $explanation, $element, $severity )>
Returns a reference to a new C<Perl::Critic::Violation> object. The
arguments are a description of the violation (as string), an
explanation for the policy (as string) or a series of page numbers in
PBP (as an ARRAY ref), a reference to the L<PPI|PPI> element that
caused the violation, and the severity of the violation (as an
integer).
=back
=head1 METHODS
=over
=item C<description()>
Returns a brief description of the specific violation. In other
words, this value may change on a per violation basis.
=item C<explanation()>
Returns an explanation of the policy as a string or as reference to an
array of page numbers in PBP. This value will generally not change
based upon the specific code violating the policy.
=item C<location()>
Don't use this method. Use the C<line_number()>,
C<logical_line_number()>, C<column_number()>,
C<visual_column_number()>, and C<logical_filename()> methods instead.
Returns a five-element array reference containing the line and real &
virtual column and logical numbers and logical file name where this
Violation occurred, as in L<PPI::Element|PPI::Element>.
=item C<line_number()>
Returns the physical line number that the violation was found on.
=item C<logical_line_number()>
Returns the logical line number that the violation was found on. This
can differ from the physical line number when there were C<#line>
directives in the code.
=item C<column_number()>
Returns the physical column that the violation was found at. This
means that hard tab characters count as a single character.
=item C<visual_column_number()>
Returns the column that the violation was found at, as it would appear
if hard tab characters were expanded, based upon the value of
L<PPI::Document/"tab_width [ $width ]">.
=item C<filename()>
Returns the path to the file where this Violation occurred. In some
cases, the path may be undefined because the source code was not read
directly from a file.
=item C<logical_filename()>
Returns the logical path to the file where the Violation occurred.
This can differ from C<filename()> when there was a C<#line> directive
in the code.
=item C<severity()>
Returns the severity of this Violation as an integer ranging from 1 to
5, where 5 is the "most" severe.
=item C<sort_by_severity( @violation_objects )>
If you need to sort Violations by severity, use this handy routine:
@sorted = Perl::Critic::Violation::sort_by_severity(@violations);
=item C<sort_by_location( @violation_objects )>
If you need to sort Violations by location, use this handy routine:
@sorted = Perl::Critic::Violation::sort_by_location(@violations);
=item C<diagnostics()>
Returns a formatted string containing a full discussion of the
motivation for and details of the Policy module that created this
Violation. This information is automatically extracted from the
C<DESCRIPTION> section of the Policy module's POD.
=item C<policy()>
Returns the name of the L<Perl::Critic::Policy|Perl::Critic::Policy>
that created this Violation.
=item C<source()>
Returns the string of source code that caused this exception. If the
code spans multiple lines (e.g. multi-line statements, subroutines or
other blocks), then only the line containing the violation will be
returned.
=item C<element_class()>
Returns the L<PPI::Element|PPI::Element> subclass of the code that caused this
exception.
=item C<set_format( $format )>
Class method. Sets the format for all Violation objects when they are
evaluated in string context. The default is C<'%d at line %l, column
%c. %e'>. See L<"OVERLOADS"> for formatting options.
=item C<get_format()>
Class method. Returns the current format for all Violation objects
when they are evaluated in string context.
=item C<to_string()>
Returns a string representation of this violation. The content of the
string depends on the current value of the C<$format> package
variable. See L<"OVERLOADS"> for the details.
=back
=head1 OVERLOADS
Perl::Critic::Violation overloads the C<""> operator to produce neat
little messages when evaluated in string context.
Formats are a combination of literal and escape characters similar to
the way C<sprintf> works. If you want to know the specific formatting
capabilities, look at L<String::Format|String::Format>. Valid escape
characters are:
Escape Meaning
------- ----------------------------------------------------------------
%c Column number where the violation occurred
%d Full diagnostic discussion of the violation (DESCRIPTION in POD)
%e Explanation of violation or page numbers in PBP
%F Just the name of the logical file where the violation occurred.
%f Path to the logical file where the violation occurred.
%G Just the name of the physical file where the violation occurred.
%g Path to the physical file where the violation occurred.
%l Logical line number where the violation occurred
%L Physical line number where the violation occurred
%m Brief description of the violation
%P Full name of the Policy module that created the violation
%p Name of the Policy without the Perl::Critic::Policy:: prefix
%r The string of source code that caused the violation
%C The class of the PPI::Element that caused the violation
%s The severity level of the violation
Explanation of the C<%F>, C<%f>, C<%G>, C<%G>, C<%l>, and C<%L> formats:
Using C<#line> directives, you can affect what perl thinks the current line
number and file name are; see L<perlsyn/Plain Old Comments (Not!)> for
the details. Under normal circumstances, the values of C<%F>, C<%f>, and
C<%l> will match the values of C<%G>, C<%g>, and C<%L>, respectively. In the
presence of a C<#line> directive, the values of C<%F>, C<%f>, and C<%l> will
change to take that directive into account. The values of C<%G>, C<%g>, and
C<%L> are unaffected by those directives.
Here are some examples:
Perl::Critic::Violation::set_format("%m at line %l, column %c.\n");
# looks like "Mixed case variable name at line 6, column 23."
Perl::Critic::Violation::set_format("%m near '%r'\n");
# looks like "Mixed case variable name near 'my $theGreatAnswer = 42;'"
Perl::Critic::Violation::set_format("%l:%c:%p\n");
# looks like "6:23:NamingConventions::Capitalization"
Perl::Critic::Violation::set_format("%m at line %l. %e. \n%d\n");
# looks like "Mixed case variable name at line 6. See page 44 of PBP.
Conway's recommended naming convention is to use lower-case words
separated by underscores. Well-recognized acronyms can be in ALL
CAPS, but must be separated by underscores from other parts of the
name."
=head1 AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
=head1 COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
=cut
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
|