File: generatestats

package info (click to toggle)
libperl-critic-perl 1.156-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,544 kB
  • sloc: perl: 24,092; lisp: 341; makefile: 7
file content (300 lines) | stat: -rwxr-xr-x 6,569 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use 5.008001;
use strict;
use warnings;

use version; our $VERSION = qv('1.116');

use Carp qw{ croak };
use English qw{ -no_match_vars };
use Readonly;

use File::Spec qw{ };
use Perl6::Say;

use Perl::Critic::Utils qw{ all_perl_files };
use Perl::Critic;


if ( ! @ARGV ) {
    die qq{usage: generatestats path [...]\n};
}

main();

exit 0;


sub main {
    foreach my $path ( @ARGV ) {
        say "Looking at $path.";

        my @files = all_perl_files($path);
        say 'Analyzing ', scalar @files, ' files.';

        my $results = summarize( \@files, File::Spec->canonpath($path) );

        report($results);

        say; say;
    }

    return;
}


sub summarize {
    my ( $files, $path ) = @_;

    # Force reporting level to be really strict, just so that the statistics
    # include everything.
    my $critic = Perl::Critic->new( -severity => 1 );

    my %total_severities;
    my %total_policies;
    my %types;
    my %files;

    foreach my $file ( @{$files} ) {
        my $relative_path;
        my $type;

        if ($file eq $path) {
            $relative_path = $file;
        } else {
            my $absolute_path_length = ( length $path ) + 1;

            $relative_path = substr $file, $absolute_path_length;
        }

        if ($file =~ m/ [.] ([^.]+) \z /xms) {
            $type = $1;
        } else {
            $type = '<program>';
        }

        $types{$type}{files}++;
        foreach my $violation ( $critic->critique($file) ) {
            $files{ $relative_path }{ severities }{ $violation->severity() }++;
            $files{ $relative_path }{ policies   }{ $violation->policy()   }++;

            $types{ $type          }{ severities }{ $violation->severity() }++;
            $types{ $type          }{ policies   }{ $violation->policy()   }++;

            $total_severities{ $violation->severity() }++;
            $total_policies{   $violation->policy()   }++;
        }
    }

    return {
        severities  => \%total_severities,
        policies    => \%total_policies,
        types       => \%types,
        files       => \%files,
    };
}


sub report {
    my ( $results ) = @_;

    report_totals( $results );
    report_types(  $results );
    report_files(  $results );

    return;
}


sub report_totals {
    my ( $results ) = @_;

    say;
    say 'Total violations by severity:';
    report_severities( $results->{severities} );

    say;
    say 'Total violations by policy:';
    report_policies( $results->{policies} );

    return;
}


sub report_types {
    my ( $results ) = @_;
    my   $types     = $results->{types};

    say;
    say 'Total files by type:';
    foreach my $type ( sort keys %{$types} ) {
        say qq{\t}, $type, ': ', $types->{$type}{files};
    }

    foreach my $type ( sort keys %{$types} ) {
        say;
        say "Violations in $type files by severity:";
        report_severities( $types->{$type}{severities} );

        say;
        say "Violations in $type files by policy:";
        report_policies( $types->{$type}{policies} );
    }

    return;
}


sub report_files {
    my ( $results ) = @_;
    my   $files     = $results->{files};

    foreach my $file ( sort keys %{$files} ) {
        say;
        say "Violations in $file by severity:";
        report_severities( $files->{$file}{severities} );

        say;
        say "Violations in $file by policy:";
        report_policies( $files->{$file}{policies} );
    }

    return;
}


sub report_severities {
    my ($severities) = @_;

    foreach my $severity ( reverse sort { $a <=> $b } keys %{$severities} ) {
        say qq{\t}, $severity, ': ', $severities->{$severity};
    }

    return;
}


sub report_policies {
    my ($policies) = @_;

    foreach my $policy ( sort keys %{$policies} ) {
        (my $short_policy = $policy) =~ s/ \A Perl::Critic::Policy:: //xms;

        say qq{\t}, $short_policy, ': ', $policies->{$policy};
    }

    return;
}


__END__

=pod

=for stopwords codebase perlartistic

=head1 NAME

C<generatestats> - Produce some simple quality statistics of a codebase


=head1 USAGE

    generatestats path [...]


=head1 DESCRIPTION

Scan a body of code and generate some statistics on violations of the
installed L<Perl::Critic|Perl::Critic> policies.  While there is no means of
configuring the policies here, this will take into account your
F<.perlcriticrc>, if available.


=head1 REQUIRED ARGUMENTS

A list of paths to files and directories to find code in.


=head1 OPTIONS

None.


=head1 DIAGNOSTICS

None.


=head1 EXIT STATUS

0


=head1 CONFIGURATION

None.


=head1 DEPENDENCIES

L<Perl::Critic|Perl::Critic>
L<Perl6::Say|Perl6::Say>
L<Readonly|Readonly>


=head1 INCOMPATIBILITIES

None reported.


=head1 BUGS AND LIMITATIONS

This is an example program and thus does minimal error handling.


=head1 AUTHOR

Elliot Shank  C<< <perl@galumph.com> >>


=head1 COPYRIGHT

Copyright (c) 2006-2011, Elliot Shank.

This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic|perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

=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 :