Policy/Moose/ProhibitMultipleWiths.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 81.2% 13 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 83.3% 5 / 6 totaltotal 93.1% 82 / 88 SCAR 24.9
CC
12
Cov
94%
violates
6.0
_is_with_statement
5.2
_is_interesting_document
3.0
CRAP 12.0
« Policy/Moose/RequireMakeImmutable.pm ↑ Summary Policy/Moose/ProhibitLazyBuild.pm »
1 package Perl::Critic::Policy::Moose::ProhibitMultipleWiths;
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 $EMPTY >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 Readonly::Scalar my $DESCRIPTION => 'Multiple calls to with() were made.';
16 1 Readonly::Scalar my $EXPLANATION =>
17 q<Roles cannot protect against name conflicts if they are not composed.>;
18  
19 sub supported_parameters {
20 return (
21 {
22 13 ▶ name => 'equivalent_modules',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
23 description =>
24 q<The additional modules to treat as equivalent to "Moose", "Moose::Role", or "MooseX::Role::Parameterized".>,
25 default_string => 'Moose Moose::Role MooseX::Role::Parameterized',
26 behavior => 'string list',
27 list_always_present_values =>
28 [qw< Moose Moose::Role MooseX::Role::Parameterized >],
29 },
30 );
31 }
32  
33 5 ▶sub default_severity { return $SEVERITY_HIGH; }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 0 ▶sub default_themes { return qw( bugs moose roles ); }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
35 12 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 sub prepare_to_scan_document {
38 13 ▶ my ( $self, $document ) = @_;
Subroutine prepare_to_scan_document: called CC=1 SCAR=0.0
Pod documented
39  
40 13 return $self->_is_interesting_document($document);
41 }
42  
43 sub _is_interesting_document {
44 28 ▶ my ( $self, $document ) = @_;
Subroutine _is_interesting_document: called CC=3 SCAR=11.0
45  
46 28 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
47 51 ▶ return $TRUE if $document->uses_module($module);
Branch: 1 branch
BranchTrueFalseTotal
if $document->uses_module($module) 26 25 51
48 }
49  
50 2 return $FALSE;
51 }
52  
53 sub violates {
54 12 ▶ my ( $self, undef, $document ) = @_;
Subroutine violates: called CC=6 SCAR=17.9
Pod documented
55  
56 12 my @violations;
57 12 foreach my $namespace ( $document->namespaces() ) {
58 SUBDOCUMENT:
59 15 foreach my $subdocument (
60 $document->subdocuments_for_namespace($namespace) ) {
61 next SUBDOCUMENT
62 15 ▶ if not $self->_is_interesting_document($subdocument);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_interesting_document($subdocument) 1 14 15
63  
64 14 my $with_statements = $subdocument->find( \&_is_with_statement );
65  
66 14 ▶ next SUBDOCUMENT if not $with_statements;
Branch: 1 branch
BranchTrueFalseTotal
unless $with_statements 1 13 14
67 13 ▶ next SUBDOCUMENT if @{$with_statements} < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$with_statements;} < 2 8 5 13
68  
69 5 my $second_with = $with_statements->[1];
70 5 push
71 @violations,
72 $self->violation( $DESCRIPTION, $EXPLANATION, $second_with );
73 }
74 }
75  
76 12 return @violations;
77 }
78  
79 sub _is_with_statement {
80 305 ▶ my ( undef, $element ) = @_;
Subroutine _is_with_statement: called CC=5 SCAR=16.5
81  
82 305 ▶ return $FALSE if not is_ppi_generic_statement($element);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ppi_generic_statement($element) 287 18 305
83  
84 18 my $current_token = $element->schild(0);
85 18 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 18 18
86 18 ▶ return $FALSE if not $current_token->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Word") 0 18 18
87 18 ▶ return $FALSE if $current_token->content() ne 'with';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "with" 0 18 18
88  
89 18 return $TRUE;
90 }
91  
92 1 1;
93  
94 # ABSTRACT: Require role composition
95  
96 __END__
97  
98 =pod
99  
100 =head1 NAME
101  
102 Perl::Critic::Policy::Moose::ProhibitMultipleWiths - Require role composition
103  
104 =head1 VERSION
105  
106 version 1.04
107  
108 =head1 DESCRIPTION
109  
110 L<Moose::Role>s are, among other things, the answer to name conflicts plaguing
111 multiple inheritance and mix-ins. However, to enjoy this protection, you must
112 compose your roles together. Roles do not generate conflicts if they are
113 consumed individually.
114  
115 Pass all of your roles to a single L<with|Moose/with> statement.
116  
117 # ok
118 package Foo;
119  
120 use Moose::Role;
121  
122 with qw< Bar Baz >;
123  
124 # not ok
125 package Foo;
126  
127 use Moose::Role;
128  
129 with 'Bar';
130 with 'Baz';
131  
132 =head1 AFFILIATION
133  
134 This policy is part of L<Perl::Critic::Moose>.
135  
136 =head1 CONFIGURATION
137  
138 There is a single option, C<equivalent_modules>. This allows you to specify
139 modules that should be treated the same as L<Moose> and L<Moose::Role>, if,
140 say, you were doing something with L<Moose::Exporter>. For example, if you
141 were to have this in your F<.perlcriticrc> file:
142  
143 [Moose::ProhibitMultipleWiths]
144 equivalent_modules = MyCompany::Moose MooseX::NewThing
145  
146 then the following code would result in a violation:
147  
148 package Baz;
149  
150 use MyCompany::Moose;
151  
152 with 'Bing';
153 with 'Bong';
154  
155 =head1 AUTHORS
156  
157 =over 4
158  
159 =item *
160  
161 Elliot Shank <perl@galumph.com>
162  
163 =item *
164  
165 Dave Rolsky <autarch@urth.org>
166  
167 =back
168  
169 =head1 COPYRIGHT AND LICENSE
170  
171 This software is copyright (c) 2008 - 2015 by Elliot Shank.
172  
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175  
176 =cut
« Policy/Moose/RequireMakeImmutable.pm ↑ Summary Policy/Moose/ProhibitLazyBuild.pm »