| 1 | # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic-Moose/lib/Perl/Critic/Policy/Moose/ProhibitNewMethod.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::ProhibitNewMethod; | ||||||||||
| 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 => | |||||||||
| 24 | q<"new" method/subroutine declared in a Moose class.>; | ||||||||||
| 25 | 1 | Readonly::Scalar my $EXPLANATION => | |||||||||
| 26 | q<Use BUILDARGS and BUILD instead of writing your own constructor.>; | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | sub supported_parameters { return (); } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 30 | 1 | ▶ | sub default_severity { return $SEVERITY_HIGH; } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | 0 | ▶ | sub default_themes { return qw< moose bugs >; } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 32 | 2 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | sub prepare_to_scan_document { | ||||||||||
| 36 | 2 | ▶ | my ($self, $document) = @_; | ||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | |||||||||||
| 38 | # Tech debt: duplicate code. | ||||||||||
| 39 | return $document->find_any( | ||||||||||
| 40 | sub { | ||||||||||
| 41 | 4 | ▶ | my (undef, $element) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||
| 42 | |||||||||||
| 43 | 4 | ▶ | return $FALSE if not $element->isa('PPI::Statement::Include'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 2 | ▶ | return $FALSE if not $element->type() eq 'use'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | |||||||||||
| 46 | 2 | my $module = $element->module(); | |||||||||
| 47 | 2 | ▶ | return $FALSE if not $module; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | return $module eq 'Moose'; | |||||||||
| 49 | } | ||||||||||
| 50 | 2 | ); | |||||||||
| 51 | } # end prepare_to_scan_document() | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | sub violates { | ||||||||||
| 55 | 2 | ▶ | my ($self, undef, $document) = @_; | ||||||||
|
Subroutine
violates: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 56 | |||||||||||
| 57 | my $constructor = $document->find_first( | ||||||||||
| 58 | sub { | ||||||||||
| 59 | 57 | ▶ | my (undef, $element) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 60 | |||||||||||
| 61 | 57 | ▶ | return $FALSE if not $element->isa('PPI::Statement::Sub'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 1 | return $element->name() eq 'new'; | |||||||||
| 64 | } | ||||||||||
| 65 | 2 | ); | |||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | return if not $constructor; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 1 | return $self->violation($DESCRIPTION, $EXPLANATION, $constructor); | |||||||||
| 69 | } # end violates() | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 1 | 1; | |||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||
| 75 | |||||||||||
| 76 | =pod | ||||||||||
| 77 | |||||||||||
| 78 | =head1 NAME | ||||||||||
| 79 | |||||||||||
| 80 | Perl::Critic::Policy::Moose::ProhibitNewMethod - Don't override Moose's standard constructors. | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | =head1 AFFILIATION | ||||||||||
| 84 | |||||||||||
| 85 | This policy is part of L<Perl::Critic::Moose>. | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | =head1 VERSION | ||||||||||
| 89 | |||||||||||
| 90 | This document describes Perl::Critic::Policy::Moose::ProhibitNewMethod | ||||||||||
| 91 | version 0.999_002. | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||
| 95 | |||||||||||
| 96 | Overriding C<new()> on a L<Moose> class causes a number of problems. Use | ||||||||||
| 97 | C<BUILDARGS()> and C<BUILD()> instead. | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | =head1 CONFIGURATION | ||||||||||
| 101 | |||||||||||
| 102 | This policy has no configuration options beyond the standard ones. | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | =head1 SEE ALSO | ||||||||||
| 106 | |||||||||||
| 107 | L<http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construction.pod> | ||||||||||
| 108 | L<http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Basics/Recipe11.pod> | ||||||||||
| 109 | L<http://search.cpan.org/dist/Moose/lib/Moose/Manual/BestPractices.pod> | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 113 | |||||||||||
| 114 | Right now this assumes that you've only got one C<package> statement in your | ||||||||||
| 115 | code. It will get things wrong if you create multiple classes in a single | ||||||||||
| 116 | file. | ||||||||||
| 117 | |||||||||||
| 118 | Please report any bugs or feature requests to | ||||||||||
| 119 | C<bug-perl-critic-moose@rt.cpan.org>, or through the web interface at | ||||||||||
| 120 | L<http://rt.cpan.org>. | ||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | =head1 AUTHOR | ||||||||||
| 124 | |||||||||||
| 125 | Elliot Shank C<< <perl@galumph.com> >> | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | =head1 COPYRIGHT | ||||||||||
| 129 | |||||||||||
| 130 | Copyright (c)2009, Elliot Shank C<< <perl@galumph.com> >>. Some rights | ||||||||||
| 131 | reserved. | ||||||||||
| 132 | |||||||||||
| 133 | This module is free software; you can redistribute it and/or modify it under | ||||||||||
| 134 | the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =head1 DISCLAIMER OF WARRANTY | ||||||||||
| 138 | |||||||||||
| 139 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE | ||||||||||
| 140 | SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE | ||||||||||
| 141 | STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE | ||||||||||
| 142 | SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, | ||||||||||
| 143 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||||||||||
| 144 | FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND | ||||||||||
| 145 | PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, | ||||||||||
| 146 | YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. | ||||||||||
| 147 | |||||||||||
| 148 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY | ||||||||||
| 149 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE | ||||||||||
| 150 | SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, | ||||||||||
| 151 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING | ||||||||||
| 152 | OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO | ||||||||||
| 153 | LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR | ||||||||||
| 154 | THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER | ||||||||||
| 155 | SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | ||||||||||
| 156 | POSSIBILITY OF SUCH DAMAGES. | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | =cut | ||||||||||
| 160 | |||||||||||
| 161 | # Local Variables: | ||||||||||
| 162 | # mode: cperl | ||||||||||
| 163 | # cperl-indent-level: 4 | ||||||||||
| 164 | # fill-column: 78 | ||||||||||
| 165 | # indent-tabs-mode: nil | ||||||||||
| 166 | # c-indentation-style: bsd | ||||||||||
| 167 | # End: | ||||||||||
| 168 | # setup vim: set filetype=perl tabstop=4 softtabstop=4 expandtab : | ||||||||||
| 169 | # setup vim: set shiftwidth=4 shiftround textwidth=78 nowrap autoindent : | ||||||||||
| 170 | # setup vim: set foldmethod=indent foldlevel=0 : | ||||||||||