File Coverage

blib/lib/Perl/Critic/Policy/Bangs/ProhibitDebuggingModules.pm
Criterion Covered Total %
statement 30 31 96.7
branch 5 6 83.3
condition 3 6 50.0
subroutine 11 12 91.6
pod 4 5 80.0
total 53 60 88.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Bangs::ProhibitDebuggingModules;
2 5     5   3306 use strict;
  5         13  
  5         126  
3 5     5   24 use warnings;
  5         29  
  5         191  
4              
5             our $VERSION = '1.12';
6              
7 5     5   26 use List::MoreUtils qw(any);
  5         10  
  5         58  
8 5     5   2574 use Readonly;
  5         12  
  5         198  
9 5     5   30 use Perl::Critic::Utils qw( :severities );
  5         11  
  5         248  
10 5     5   645 use base qw(Perl::Critic::Policy);
  5         18  
  5         1386  
11              
12             =head1 NAME
13              
14             Perl::Critic::Policy::Bangs::ProhibitDebuggingModules - Prohibit loading of debugging modules like Data::Dumper
15              
16             =head1 DESCRIPTION
17              
18             This policy prohibits loading common debugging modules like L<Data::Dumper>.
19              
20             While such modules are incredibly useful during development and debugging,
21             they should probably not be loaded in production use. If this policy is
22             violated, it probably means you forgot to remove a C<use Data::Dumper;> line
23             that you had added when you were debugging.
24              
25             =head1 CONFIGURATION
26              
27             The current list of detected debugging modules is:
28              
29             =over 4
30              
31             =item * L<Data::Dumper>
32              
33             =item * L<Data::Printer>
34              
35             =back
36              
37             To add more modules that shouldn't be loaded unless you're actively debugging
38             something, add them in F<.perlcriticrc> using the C<deubgging_modules> option.
39              
40             =cut
41              
42             Readonly::Scalar my $DESC => q/Debugging module loaded/;
43              
44             sub supported_parameters {
45             return (
46             {
47 9     9 0 31661 name => 'debugging_modules',
48             description => 'Module names which are considered to be banned debugging modules',
49             behavior => 'string list',
50             list_always_present_values => [qw(
51             B::Stats
52              
53             Carp::Always.*
54             Carp::Diagnostics
55             Carp::REPL
56             Carp::Source::Always
57             Carp::Trace
58              
59             Data::Dump
60             Data::Dump::Filtered
61             Data::Dump::Streamer
62             Data::Dump::Trace
63              
64             Data::Dumper.*
65              
66             Data::Printer
67             Data::PrettyPrintObjects
68             Data::Show
69             Data::Skeleton
70             Data::TreeDumper
71              
72             DDP
73             DDS
74             Devel::Ditto
75             Devel::Dwarn
76             Devel::Modlist
77             Devel::Monitor
78             Devel::StackTrace
79             Devel::Trace
80             Devel::Unplug
81             ) ], # DDP and DDS are shorthand module names
82             }
83             );
84             }
85 9     9 1 132 sub default_severity { return $SEVERITY_HIGH }
86 0     0 1 0 sub default_themes { return qw/ bangs maintenance / }
87 5     5 1 39610 sub applies_to { return 'PPI::Statement::Include' }
88              
89             sub violates {
90 21     21 1 1578 my ($self, $include, undef) = @_;
91 21 50 66     69 return unless defined $include->type and ($include->type eq 'use' || $include->type eq 'require');
      33        
92 21 100       998 my $included = $include->module or return;
93 16         395 my $EXPL = "You've loaded $included, which probably shouldn't be loaded in production";
94              
95 16         30 my @banned = ( keys %{ $self->{_debugging_modules} } );
  16         118  
96             return $self->violation($DESC, $EXPL, $include)
97 16 100   202   89 if (any { $included =~ m/$_/xms } @banned);
  202         1896  
98 7         75 return;
99             }
100              
101             =head1 AFFILIATION
102              
103             This policy is part of L<Perl::Critic::Bangs>.
104              
105             =head1 AUTHOR
106              
107             Mike Doherty C<doherty@cpan.org>
108              
109             =head1 COPYRIGHT & LICENSE
110              
111             Copyright (c) 2012 Mike Doherty
112              
113             This library is free software; you can redistribute it and/or modify
114             it under the terms of the Artistic License 2.0.
115              
116             =cut
117              
118             1;