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