Policy/Moose/RequireCleanNamespace.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.0% 9 / 10 Podpod 80.0% 4 / 5 totaltotal 90.6% 68 / 75 SCAR 22.0
CC
9
Cov
93%
violates
9.0
default_themes
2.0
BEGIN
1.0
CRAP 9.0
« Policy/Moose/ProhibitLazyBuild.pm ↑ Summary Policy/Moose/ProhibitDESTROYMethod.pm »
1 package Perl::Critic::Policy::Moose::RequireCleanNamespace;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.04';
7  
8 1 ▶use Readonly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Perl::Critic::Utils qw< :booleans :severities $PERIOD >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 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 ▶ name => 'modules',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
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 ▶sub default_severity { return $SEVERITY_MEDIUM; }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
36 0 ▶sub default_themes { return qw( moose maintenance ); }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
37 11 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
38  
39 sub violates {
40 11 ▶ my ( $self, undef, $document ) = @_;
Subroutine violates: called CC=9 SCAR=22.0
Pod documented
41  
42 11 my %modules = ( use => {}, require => {}, no => {} );
43 11 my $includes = $document->find('PPI::Statement::Include');
44 11 ▶ return if not $includes;
Branch: 1 branch
BranchTrueFalseTotal
unless $includes 0 11 11
45  
46 11 for my $include ( @{$includes} ) {
47  
48 # skip if nothing imported
49 20 ▶ if ( $include->type eq 'use' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include->type eq "use") 15 5 20
50 15 my $lists = $include->find('PPI::Structure::List');
51 15 ▶ next if $lists and not grep { $_->children > 0 } @{$lists};
Branch: 1 branch
BranchTrueFalseTotal
if $lists and not grep({$_->children > 0;} @{$lists;}) 1 14 15
Condition66%2/3
$lists and not grep({$_->children > 0;} @{$lists;})
!ll&&!rl&&r
1401
MC/DC50%1/2
$lists and not grep({$_->children > 0;} @{$lists;})
$lists not grep({$_->children > 0;} @{$lists;})
Truth table66%2/3
A and not(B)
A= $listsB= not grep({$_->children > 0;} @{$lists;})
ABresult
0X0
100
111
52 }
53  
54 19 $modules{ $include->type }->{ $include->module } = 1;
55 }
56  
57 11 ▶ return if grep { $modules{use}{$_} } keys %{ $self->{_cleaners} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$modules{"use"}{$_};} keys %{$$self{"_cleaners"};} 2 9 11
58  
59 9 my $modules_to_unimport = $self->{_modules};
60 my @used_but_not_unimported
61 10 ▶ = grep { $modules_to_unimport->{$_} and not $modules{no}->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
$modules_to_unimport->{$_} and not $modules{"no"}{$_} 10 0 10
62 9 keys %{ $modules{use} };
63  
64 9 ▶ return if not @used_but_not_unimported;
Branch: 1 branch
BranchTrueFalseTotal
unless @used_but_not_unimported 5 4 9
65  
66 4 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 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
« Policy/Moose/ProhibitLazyBuild.pm ↑ Summary Policy/Moose/ProhibitDESTROYMethod.pm »