File Coverage

blib/lib/Perl/Critic/Policy/Moose/RequireCleanNamespace.pm
Criterion Covered Total %
statement 39 40 97.5
branch 10 12 83.3
condition 2 3 66.6
subroutine 9 10 90.0
pod 4 5 80.0
total 64 70 91.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Moose::RequireCleanNamespace;
2              
3 1     1   1541 use strict;
  1         4  
  1         54  
4 1     1   10 use warnings;
  1         4  
  1         84  
5              
6             our $VERSION = '1.04';
7              
8 1     1   9 use Readonly ();
  1         4  
  1         35  
9              
10 1     1   8 use Perl::Critic::Utils qw< :booleans :severities $PERIOD >;
  1         3  
  1         105  
11              
12 1     1   296 use base 'Perl::Critic::Policy';
  1         2  
  1         852  
13              
14             Readonly::Scalar my $EXPLANATION =>
15             q<Don't leave things used for implementation in your interface.>;
16              
17             sub supported_parameters {
18             return (
19             {
20 11     11 0 42587 name => 'modules',
21             description => 'The modules that need to be unimported.',
22             default_string =>
23             'Moose Moose::Role Moose::Util::TypeConstraints',
24             behavior => 'string list',
25             },
26             {
27             name => 'cleaners',
28             description => 'Modules that clean imports.',
29             default_string => 'namespace::autoclean',
30             behavior => 'string list',
31             },
32             );
33             }
34              
35 4     4 1 50 sub default_severity { return $SEVERITY_MEDIUM; }
36 0     0 1 0 sub default_themes { return qw( moose maintenance ); }
37 11     11 1 101373 sub applies_to { return 'PPI::Document' }
38              
39             sub violates {
40 11     11 1 108 my ( $self, undef, $document ) = @_;
41              
42 11         53 my %modules = ( use => {}, require => {}, no => {} );
43 11         41 my $includes = $document->find('PPI::Statement::Include');
44 11 50       147 return if not $includes;
45              
46 11         18 for my $include ( @{$includes} ) {
  11         32  
47              
48             # skip if nothing imported
49 20 100       486 if ( $include->type eq 'use' ) {
50 15         372 my $lists = $include->find('PPI::Structure::List');
51 15 100 66     4697 next if $lists and not grep { $_->children > 0 } @{$lists};
  1         9  
  1         3  
52             }
53              
54 19         146 $modules{ $include->type }->{ $include->module } = 1;
55             }
56              
57 11 100       426 return if grep { $modules{use}{$_} } keys %{ $self->{_cleaners} };
  11         53  
  11         44  
58              
59 9         19 my $modules_to_unimport = $self->{_modules};
60             my @used_but_not_unimported
61 10 50       72 = grep { $modules_to_unimport->{$_} and not $modules{no}->{$_} }
62 9         19 keys %{ $modules{use} };
  9         23  
63              
64 9 100       47 return if not @used_but_not_unimported;
65              
66 4         38 return $self->violation(
67             q<Didn't unimport >
68             . ( join q<, >, sort @used_but_not_unimported )
69             . $PERIOD,
70             $EXPLANATION,
71             $document,
72             );
73             }
74              
75             1;
76              
77             # ABSTRACT: Require removing implementation details from you packages.
78              
79             __END__
80              
81             =pod
82              
83             =head1 NAME
84              
85             Perl::Critic::Policy::Moose::RequireCleanNamespace - Require removing implementation details from you packages.
86              
87             =head1 VERSION
88              
89             version 1.04
90              
91             =head1 DESCRIPTION
92              
93             Anything in your namespace is part of your interface. The L<Moose> sugar is an
94             implementation detail and not part of what you want to support as part of your
95             functionality, especially if you may change your implementation to not use
96             Moose in the future. Thus, this policy requires you to say C<no Moose;> or
97             C<no Moose::Role;>, etc. as appropriate for modules you C<use>.
98              
99             =for stopwords unimport
100              
101             =head1 AFFILIATION
102              
103             This policy is part of L<Perl::Critic::Moose>.
104              
105             =head1 CONFIGURATION
106              
107             By default, this module will complain if you C<use> L<Moose>, L<Moose::Role>,
108             or C<Moose::Util::TypeConstraints> but don't unimport them. You can set the
109             modules looked for using the C<modules> option.
110              
111             [Moose::RequireCleanNamespace]
112             modules = Moose Moose::Role Moose::Util::TypeConstraints MooseX::My::New::Sugar
113              
114             This module also knows that L<namespace::autoclean> will clean out imports. If
115             you'd like to allow other modules to be recognized as namespace cleaners, you
116             can set the C<cleaners> option.
117              
118             [Moose::RequireCleanNamespace]
119             cleaners = My::Cleaner
120              
121             If you use C<use> a module with an empty import list, then this module knows
122             that nothing needs to be cleaned, and will ignore that particular import.
123              
124             =head1 SEE ALSO
125              
126             L<Moose::Manual::BestPractices>
127              
128             =head1 AUTHORS
129              
130             =over 4
131              
132             =item *
133              
134             Elliot Shank <perl@galumph.com>
135              
136             =item *
137              
138             Dave Rolsky <autarch@urth.org>
139              
140             =back
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2008 - 2015 by Elliot Shank.
145              
146             This is free software; you can redistribute it and/or modify it under
147             the same terms as the Perl 5 programming language system itself.
148              
149             =cut