| 1 | # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic-Moose/lib/Perl/Critic/Policy/Moose/ProhibitMultipleWiths.pm $ | ||||||||||
| 2 | # $Date: 2009-05-15 19:35:37 -0500 (Fri, 15 May 2009) $ | ||||||||||
| 3 | # $Author: clonezone $ | ||||||||||
| 4 | # $Revision: 3336 $ | ||||||||||
| 5 | |||||||||||
| 6 | package Perl::Critic::Policy::Moose::ProhibitMultipleWiths; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use 5.008; # Moose's minimum version. | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | our $VERSION = '0.999_002'; | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use Readonly (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | use Perl::Critic::Utils qw< :booleans :severities >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 1 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 1 | Readonly::Scalar my $DESCRIPTION => 'Multiple calls to with() were made.'; | |||||||||
| 24 | 1 | Readonly::Scalar my $EXPLANATION => | |||||||||
| 25 | q<Roles cannot protect against name conflicts if they are not composed.>; | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 4 | ▶ | sub supported_parameters { return (); } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | 1 | ▶ | sub default_severity { return $SEVERITY_HIGH; } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 30 | 0 | ▶ | sub default_themes { return qw( bugs moose roles ); } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 31 | 4 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | sub prepare_to_scan_document { | ||||||||||
| 35 | 4 | ▶ | my ($self, $document) = @_; | ||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | |||||||||||
| 37 | # Tech debt: duplicate code. | ||||||||||
| 38 | return $document->find_any( | ||||||||||
| 39 | sub { | ||||||||||
| 40 | 8 | ▶ | my (undef, $element) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||
| 41 | |||||||||||
| 42 | 8 | ▶ | return $FALSE if not $element->isa('PPI::Statement::Include'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 4 | ▶ | return $FALSE if not $element->type() eq 'use'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 4 | my $module = $element->module(); | |||||||||
| 46 | 4 | ▶ | return $FALSE if not $module; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 4 | return $module eq 'Moose'; | |||||||||
| 48 | } | ||||||||||
| 49 | 4 | ); | |||||||||
| 50 | } # end prepare_to_scan_document() | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | sub violates { | ||||||||||
| 54 | 4 | ▶ | my ($self, undef, $document) = @_; | ||||||||
|
Subroutine
violates: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 55 | |||||||||||
| 56 | my $with_statements = $document->find( | ||||||||||
| 57 | sub { | ||||||||||
| 58 | 108 | ▶ | my (undef, $element) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.5
| |||||||||||
| 59 | |||||||||||
| 60 | 108 | ▶ | return $FALSE if not is_ppi_generic_statement($element); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 5 | my $current_token = $element->schild(0); | |||||||||
| 63 | 5 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 5 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 5 | ▶ | return $FALSE if $current_token->content() ne 'with'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 5 | return $TRUE; | |||||||||
| 68 | } | ||||||||||
| 69 | 4 | ); | |||||||||
| 70 | |||||||||||
| 71 | 4 | ▶ | return if not $with_statements; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 4 | ▶ | return if @{ $with_statements } < 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 1 | my $second_with = $with_statements->[1]; | |||||||||
| 75 | 1 | return $self->violation($DESCRIPTION, $EXPLANATION, $second_with); | |||||||||
| 76 | } # end violates() | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | 1 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||
| 82 | |||||||||||
| 83 | =pod | ||||||||||
| 84 | |||||||||||
| 85 | =head1 NAME | ||||||||||
| 86 | |||||||||||
| 87 | Perl::Critic::Policy::Moose::ProhibitMultipleWiths - Require role composition | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | =head1 AFFILIATION | ||||||||||
| 91 | |||||||||||
| 92 | This policy is part of L<Perl::Critic::Moose>. | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | =head1 VERSION | ||||||||||
| 96 | |||||||||||
| 97 | This document describes Perl::Critic::Policy::Moose::ProhibitMultipleWiths | ||||||||||
| 98 | version 0.999_002. | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||
| 102 | |||||||||||
| 103 | L<Moose::Role>s are, among other things, the answer to name conflicts plaguing | ||||||||||
| 104 | multiple inheritance and mix-ins. However, to enjoy this protection, you must | ||||||||||
| 105 | compose your roles together. Roles do not generate conflicts if they are | ||||||||||
| 106 | consumed individually. | ||||||||||
| 107 | |||||||||||
| 108 | Pass all of your roles to a single L<Moose/with> statement. | ||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | =head1 CONFIGURATION | ||||||||||
| 112 | |||||||||||
| 113 | This policy has no configuration options beyond the standard ones. | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | =head1 SEE ALSO | ||||||||||
| 117 | |||||||||||
| 118 | L<http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Roles/Recipe2.pod> | ||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 122 | |||||||||||
| 123 | Right now this assumes that you've only got one C<package> statement in your | ||||||||||
| 124 | code. It will get things wrong if you create multiple classes in a single | ||||||||||
| 125 | file. | ||||||||||
| 126 | |||||||||||
| 127 | Please report any bugs or feature requests to | ||||||||||
| 128 | C<bug-perl-critic-moose@rt.cpan.org>, or through the web interface at | ||||||||||
| 129 | L<http://rt.cpan.org>. | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | =head1 AUTHOR | ||||||||||
| 133 | |||||||||||
| 134 | Shawn Moore C<< <sartak@bestpractical.com> >> | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =head1 COPYRIGHT | ||||||||||
| 138 | |||||||||||
| 139 | Copyright (c)2009, Shawn Moore C<< <sartak@bestpractical.com> >>. Some rights | ||||||||||
| 140 | reserved. | ||||||||||
| 141 | |||||||||||
| 142 | This module is free software; you can redistribute it and/or modify it under | ||||||||||
| 143 | the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | =head1 DISCLAIMER OF WARRANTY | ||||||||||
| 147 | |||||||||||
| 148 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE | ||||||||||
| 149 | SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE | ||||||||||
| 150 | STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE | ||||||||||
| 151 | SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, | ||||||||||
| 152 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||||||||||
| 153 | FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND | ||||||||||
| 154 | PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, | ||||||||||
| 155 | YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. | ||||||||||
| 156 | |||||||||||
| 157 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY | ||||||||||
| 158 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE | ||||||||||
| 159 | SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, | ||||||||||
| 160 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING | ||||||||||
| 161 | OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO | ||||||||||
| 162 | LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR | ||||||||||
| 163 | THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER | ||||||||||
| 164 | SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | ||||||||||
| 165 | POSSIBILITY OF SUCH DAMAGES. | ||||||||||
| 166 | |||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | # Local Variables: | ||||||||||
| 171 | # mode: cperl | ||||||||||
| 172 | # cperl-indent-level: 4 | ||||||||||
| 173 | # fill-column: 78 | ||||||||||
| 174 | # indent-tabs-mode: nil | ||||||||||
| 175 | # c-indentation-style: bsd | ||||||||||
| 176 | # End: | ||||||||||
| 177 | # setup vim: set filetype=perl tabstop=4 softtabstop=4 expandtab : | ||||||||||
| 178 | # setup vim: set shiftwidth=4 shiftround textwidth=78 nowrap autoindent : | ||||||||||
| 179 | # setup vim: set foldmethod=indent foldlevel=0 : | ||||||||||