File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitLargeFile.pm
Criterion Covered Total %
statement 39 40 97.5
branch 5 6 83.3
condition 4 6 66.6
subroutine 11 12 91.6
pod 4 5 80.0
total 63 69 91.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Mardem::ProhibitLargeFile;
2              
3 10     10   9037 use utf8;
  10         55  
  10         85  
4              
5 10     10   542 use 5.010;
  10         40  
6              
7 10     10   55 use strict;
  10         21  
  10         274  
8 10     10   85 use warnings;
  10         19  
  10         909  
9              
10             our $VERSION = '0.06';
11              
12 10     10   62 use Readonly;
  10         19  
  10         747  
13              
14 10     10   107 use Perl::Critic::Utils qw{:severities :data_conversion :classification};
  10         19  
  10         784  
15              
16 10     10   4702 use base 'Perl::Critic::Policy';
  10         25  
  10         5257  
17              
18             Readonly::Scalar my $EXPL => q{Consider refactoring};
19              
20             sub default_severity
21             {
22 2     2 1 41 return $SEVERITY_LOW;
23             }
24              
25             sub default_themes
26             {
27 0     0 1 0 return qw(maintenance);
28             }
29              
30             sub applies_to
31             {
32 6     6 1 83965 return 'PPI::Document';
33             }
34              
35             sub supported_parameters
36             {
37             return (
38 6     6 0 2156388 { 'name' => 'line_count_limit',
39             'description' => 'The maximum line count allowed.',
40             'default_string' => '999',
41             'behavior' => 'integer',
42             'integer_minimum' => 1,
43             },
44             );
45             }
46              
47             sub violates
48             {
49 6     6 1 97 my ( $self, $elem, undef ) = @_;
50              
51 6         17 my $filename = '__UNKNOWN__';
52              
53             {
54 6         43 local $@;
  6         12  
55 6   66     20 eval { $filename = $elem->filename() || $filename; };
  6         28  
56 6 50       165 if ( $@ ) {
57             # Note: warn ?
58             }
59             }
60              
61 6         49 my $s = $elem->serialize();
62 6 100 66     2096 if ( !defined $s || q{} eq $s ) {
63 1         6 return;
64             }
65              
66 5         73 my @matches = $s =~ /\n/og;
67 5         15 my $lines = scalar @matches;
68              
69 5 100       52 if ( $lines <= $self->{ '_line_count_limit' } ) {
70 3         19 return;
71             }
72              
73 2         7 my $desc = qq<File "$filename" with high line count ($lines)>;
74 2         20 return $self->violation( $desc, $EXPL, $elem );
75             }
76              
77             1;
78              
79             __END__
80              
81             #-----------------------------------------------------------------------------
82              
83             =pod
84              
85             =encoding utf8
86              
87             =head1 NAME
88              
89             Perl::Critic::Policy::Mardem::ProhibitLargeFile - large files as line count
90              
91             =head1 DESCRIPTION
92              
93             This Policy counts the lines within a Perl-File
94             (more precise the PPI::Document's)
95              
96             =head1 CONFIGURATION
97              
98             The maximum acceptable lines can be set with the C<line_count_limit>
99             configuration item. Any file (or given string) with higher line count
100             will generate a policy violation. The default is 999.
101              
102             An example section for a F<.perlcriticrc>:
103              
104             [Mardem::ProhibitLargeFile]
105             line_count_limit = 1
106              
107             =head1 AFFILIATION
108              
109             This policy is part of L<Perl::Critic::Mardem>.
110              
111             =head1 AUTHOR
112              
113             Markus Demml, mardem@cpan.com
114              
115             =head1 LICENSE AND COPYRIGHT
116              
117             Copyright (c) 2024, Markus Demml
118              
119             This library is free software; you can redistribute it and/or modify it
120             under the same terms as the Perl 5 programming language system itself.
121             The full text of this license can be found in the LICENSE file included
122             with this module.
123              
124             =cut