File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitFileSize.pm
Criterion Covered Total %
statement 46 48 95.8
branch 8 10 80.0
condition 6 8 75.0
subroutine 11 12 91.6
pod 4 5 80.0
total 75 83 90.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Mardem::ProhibitFileSize;
2              
3 10     10   8882 use utf8;
  10         79  
  10         73  
4              
5 10     10   582 use 5.010;
  10         39  
6              
7 10     10   58 use strict;
  10         39  
  10         278  
8 10     10   61 use warnings;
  10         19  
  10         770  
9              
10             our $VERSION = '0.06';
11              
12 10     10   71 use Readonly;
  10         45  
  10         1011  
13              
14 10     10   66 use Perl::Critic::Utils qw{:severities :data_conversion :classification};
  10         29  
  10         678  
15              
16 10     10   4580 use base 'Perl::Critic::Policy';
  10         23  
  10         5591  
17              
18             Readonly::Scalar my $EXPL => q{Consider refactoring};
19              
20             sub default_severity
21             {
22 2     2 1 40 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 109345 return 'PPI::Document';
33             }
34              
35             sub supported_parameters
36             {
37             return (
38 6     6 0 2482544 { 'name' => 'size_count_limit',
39             'description' => 'The maximum bytes (or chars) allowed.',
40             'default_string' => '102400', # 100 KB
41             'behavior' => 'integer',
42             'integer_minimum' => 1,
43             },
44             );
45             }
46              
47             sub violates
48             {
49 6     6 1 95 my ( $self, $elem, undef ) = @_;
50              
51 6         16 my $filename = undef;
52              
53             {
54 6         14 local $@;
  6         11  
55 6   100     14 eval { $filename = $elem->filename() || undef; };
  6         26  
56 6 50       148 if ( $@ ) {
57             # Note: warn ?
58             }
59             }
60              
61 6         18 my $count = 0;
62 6         14 my $mode = '';
63              
64 6 100 66     122 if ( defined $filename && q{} ne $filename && -e -f -r $filename ) {
      66        
65 2         9 $mode = 'byte';
66              
67             # if file available use byte size
68 2         6 $count = -s _;
69              
70             }
71             else {
72 4         10 $filename = '__UNKNOWN__';
73 4         272 $mode = 'char';
74              
75             # no file = use char length
76 4         40 my $s = $elem->serialize();
77              
78             # error ?
79 4 50       321 if ( !defined $s ) {
80 0         0 return;
81             }
82              
83 4         2132 $count = length $s;
84             }
85              
86             # empty
87 6 100       39 if ( 0 >= $count ) {
88 1         3 return;
89             }
90              
91 5 100       28 if ( $count <= $self->{ '_size_count_limit' } ) {
92 3         17 return;
93             }
94              
95 2         9 my $desc = qq<File "$filename" with high $mode count ($count)>;
96 2         17 return $self->violation( $desc, $EXPL, $elem );
97             }
98              
99             1;
100              
101             __END__
102              
103             #-----------------------------------------------------------------------------
104              
105             =pod
106              
107             =encoding utf8
108              
109             =head1 NAME
110              
111             Perl::Critic::Policy::Mardem::ProhibitFileSize - large files as byte or char count
112              
113             =head1 DESCRIPTION
114              
115             This Policy checks the Perl-File Size in Bytes or the Content-Length (string)
116             if no file given. (more precise the PPI::Document's)
117              
118             =head1 CONFIGURATION
119              
120             The maximum acceptable size can be set with the C<size_count_limit>
121             configuration item. Any file (or given string) with higher size "count"
122             will generate a policy violation. The default is 102400.
123              
124             An example section for a F<.perlcriticrc>:
125              
126             [Mardem::ProhibitFileSize]
127             size_count_limit = 65536
128              
129             =head1 AFFILIATION
130              
131             This policy is part of L<Perl::Critic::Mardem>.
132              
133             =head1 AUTHOR
134              
135             Markus Demml, mardem@cpan.com
136              
137             =head1 LICENSE AND COPYRIGHT
138              
139             Copyright (c) 2024, Markus Demml
140              
141             This library is free software; you can redistribute it and/or modify it
142             under the same terms as the Perl 5 programming language system itself.
143             The full text of this license can be found in the LICENSE file included
144             with this module.
145              
146             =cut