File Coverage

blib/lib/Perl/Critic/Policy/Moose/RequireCleanNamespace.pm
Criterion Covered Total %
statement 42 43 97.6
branch 10 12 83.3
condition 2 3 66.6
subroutine 10 11 90.9
pod 4 5 80.0
total 68 74 91.8


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