/usr/share/perl5/Perl/Critic/Statistics.pm is in libperl-critic-perl 1.121-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 | ##############################################################################
# $URL$
# $Date$
# $Author$
# $Revision$
##############################################################################
package Perl::Critic::Statistics;
use 5.006001;
use strict;
use warnings;
use English qw(-no_match_vars);
use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
#-----------------------------------------------------------------------------
our $VERSION = '1.121';
#-----------------------------------------------------------------------------
sub new {
my ( $class ) = @_;
my $self = bless {}, $class;
$self->{_modules} = 0;
$self->{_subs} = 0;
$self->{_statements} = 0;
$self->{_lines} = 0;
$self->{_lines_of_blank} = 0;
$self->{_lines_of_comment} = 0;
$self->{_lines_of_data} = 0;
$self->{_lines_of_perl} = 0;
$self->{_lines_of_pod} = 0;
$self->{_violations_by_policy} = {};
$self->{_violations_by_severity} = {};
$self->{_total_violations} = 0;
return $self;
}
#-----------------------------------------------------------------------------
sub accumulate {
my ($self, $doc, $violations) = @_;
$self->{_modules}++;
my $subs = $doc->find('PPI::Statement::Sub');
if ($subs) {
foreach my $sub ( @{$subs} ) {
$self->{_subs}++;
$self->{_subs_total_mccabe} += calculate_mccabe_of_sub( $sub );
}
}
my $statements = $doc->find('PPI::Statement');
$self->{_statements} += $statements ? scalar @{$statements} : 0;
## no critic (RequireDotMatchAnything, RequireExtendedFormatting, RequireLineBoundaryMatching)
my @lines = split /$INPUT_RECORD_SEPARATOR/, $doc->serialize();
## use critic
$self->{_lines} += scalar @lines;
{
my ( $in_data, $in_pod );
foreach ( @lines ) {
if ( q{=} eq substr $_, 0, 1 ) { ## no critic (ProhibitCascadingIfElse)
$in_pod = not m/ \A \s* =cut \b /smx;
$self->{_lines_of_pod}++;
} elsif ( $in_pod ) {
$self->{_lines_of_pod}++;
} elsif ( q{__END__} eq $_ || q{__DATA__} eq $_ ) {
$in_data = 1;
$self->{_lines_of_perl}++;
} elsif ( $in_data ) {
$self->{_lines_of_data}++;
} elsif ( m/ \A \s* \# /smx ) {
$self->{_lines_of_comment}++;
} elsif ( m/ \A \s* \z /smx ) {
$self->{_lines_of_blank}++;
} else {
$self->{_lines_of_perl}++;
}
}
}
foreach my $violation ( @{ $violations } ) {
$self->{_violations_by_severity}->{ $violation->severity() }++;
$self->{_violations_by_policy}->{ $violation->policy() }++;
$self->{_total_violations}++;
}
return;
}
#-----------------------------------------------------------------------------
sub modules {
my ( $self ) = @_;
return $self->{_modules};
}
#-----------------------------------------------------------------------------
sub subs {
my ( $self ) = @_;
return $self->{_subs};
}
#-----------------------------------------------------------------------------
sub statements {
my ( $self ) = @_;
return $self->{_statements};
}
#-----------------------------------------------------------------------------
sub lines {
my ( $self ) = @_;
return $self->{_lines};
}
#-----------------------------------------------------------------------------
sub lines_of_blank {
my ( $self ) = @_;
return $self->{_lines_of_blank};
}
#-----------------------------------------------------------------------------
sub lines_of_comment {
my ( $self ) = @_;
return $self->{_lines_of_comment};
}
#-----------------------------------------------------------------------------
sub lines_of_data {
my ( $self ) = @_;
return $self->{_lines_of_data};
}
#-----------------------------------------------------------------------------
sub lines_of_perl {
my ( $self ) = @_;
return $self->{_lines_of_perl};
}
#-----------------------------------------------------------------------------
sub lines_of_pod {
my ( $self ) = @_;
return $self->{_lines_of_pod};
}
#-----------------------------------------------------------------------------
sub _subs_total_mccabe {
my ( $self ) = @_;
return $self->{_subs_total_mccabe};
}
#-----------------------------------------------------------------------------
sub violations_by_severity {
my ( $self ) = @_;
return $self->{_violations_by_severity};
}
#-----------------------------------------------------------------------------
sub violations_by_policy {
my ( $self ) = @_;
return $self->{_violations_by_policy};
}
#-----------------------------------------------------------------------------
sub total_violations {
my ( $self ) = @_;
return $self->{_total_violations};
}
#-----------------------------------------------------------------------------
sub statements_other_than_subs {
my ( $self ) = @_;
return $self->statements() - $self->subs();
}
#-----------------------------------------------------------------------------
sub average_sub_mccabe {
my ( $self ) = @_;
return if $self->subs() == 0;
return $self->_subs_total_mccabe() / $self->subs();
}
#-----------------------------------------------------------------------------
sub violations_per_file {
my ( $self ) = @_;
return if $self->modules() == 0;
return $self->total_violations() / $self->modules();
}
#-----------------------------------------------------------------------------
sub violations_per_statement {
my ( $self ) = @_;
my $statements = $self->statements_other_than_subs();
return if $statements == 0;
return $self->total_violations() / $statements;
}
#-----------------------------------------------------------------------------
sub violations_per_line_of_code {
my ( $self ) = @_;
return if $self->lines() == 0;
return $self->total_violations() / $self->lines();
}
#-----------------------------------------------------------------------------
1;
__END__
#-----------------------------------------------------------------------------
=pod
=for stopwords McCabe
=head1 NAME
Perl::Critic::Statistics - Compile stats on Perl::Critic violations.
=head1 DESCRIPTION
This class accumulates statistics on Perl::Critic violations across one or
more files. NOTE: This class is experimental and subject to change.
=head1 INTERFACE SUPPORT
This is considered to be a non-public class. Its interface is subject
to change without notice.
=head1 METHODS
=over
=item C<new()>
Create a new instance of Perl::Critic::Statistics. No arguments are supported
at this time.
=item C< accumulate( $doc, \@violations ) >
Accumulates statistics about the C<$doc> and the C<@violations> that were
found.
=item C<modules()>
The number of chunks of code (usually files) that have been analyzed.
=item C<subs()>
The total number of subroutines analyzed by this Critic.
=item C<statements()>
The total number of statements analyzed by this Critic.
=item C<lines()>
The total number of lines of code analyzed by this Critic.
=item C<lines_of_blank()>
The total number of blank lines analyzed by this Critic. This includes only
blank lines in code, not POD or data.
=item C<lines_of_comment()>
The total number of comment lines analyzed by this Critic. This includes only
lines whose first non-whitespace character is C<#>.
=item C<lines_of_data()>
The total number of lines of data section analyzed by this Critic, not
counting the C<__END__> or C<__DATA__> line. POD in a data section is counted
as POD, not data.
=item C<lines_of_perl()>
The total number of lines of Perl code analyzed by this Critic. Perl appearing
in the data section is not counted.
=item C<lines_of_pod()>
The total number of lines of POD analyzed by this Critic. Pod occurring in a
data section is counted as POD, not as data.
=item C<violations_by_severity()>
The number of violations of each severity found by this Critic as a
reference to a hash keyed by severity.
=item C<violations_by_policy()>
The number of violations of each policy found by this Critic as a
reference to a hash keyed by full policy name.
=item C<total_violations()>
The total number of violations found by this Critic.
=item C<statements_other_than_subs()>
The total number of statements minus the number of subroutines.
Useful because a subroutine is considered a statement by PPI.
=item C<average_sub_mccabe()>
The average McCabe score of all scanned subroutines.
=item C<violations_per_file()>
The total violations divided by the number of modules.
=item C<violations_per_statement()>
The total violations divided by the number statements minus
subroutines.
=item C<violations_per_line_of_code()>
The total violations divided by the lines of code.
=back
=head1 AUTHOR
Elliot Shank C<< <perl@galumph.com> >>
=head1 COPYRIGHT
Copyright (c) 2007-2011 Elliot Shank.
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 :
|