File Coverage

blib/lib/Perl/Critic/Policy/Moose/ProhibitNewMethod.pm
Criterion Covered Total %
statement 42 43 97.6
branch 8 8 100.0
condition n/a
subroutine 14 15 93.3
pod 5 6 83.3
total 69 72 95.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Moose::ProhibitNewMethod;
2              
3 1     1   488 use strict;
  1         1  
  1         21  
4 1     1   4 use warnings;
  1         1  
  1         17  
5 1     1   3 use namespace::autoclean;
  1         0  
  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 >;
  1         1  
  1         41  
12 1     1   99 use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >;
  1         1  
  1         32  
13              
14 1     1   3 use base 'Perl::Critic::Policy';
  1         2  
  1         349  
15              
16             Readonly::Scalar my $DESCRIPTION =>
17             q<"new" method/subroutine declared in a Moose class.>;
18             Readonly::Scalar my $EXPLANATION =>
19             q<Use BUILDARGS and BUILD instead of writing your own constructor.>;
20              
21             sub supported_parameters {
22             return (
23             {
24 6     6 0 15108 name => 'equivalent_modules',
25             description =>
26             q<The additional modules to treat as equivalent to "Moose", "Moose::Role", or "MooseX::Role::Parameterized".>,
27             default_string => 'Moose Moose::Role MooseX::Role::Parameterized',
28             behavior => 'string list',
29             list_always_present_values =>
30             [qw< Moose Moose::Role MooseX::Role::Parameterized >],
31             },
32             );
33             }
34              
35 3     3 1 26 sub default_severity { return $SEVERITY_HIGH; }
36 0     0 1 0 sub default_themes { return qw< moose bugs >; }
37 6     6 1 156 sub applies_to { return 'PPI::Document' }
38              
39             sub prepare_to_scan_document {
40 6     6 1 26389 my ( $self, $document ) = @_;
41              
42 6         11 return $self->_is_interesting_document($document);
43             }
44              
45             sub _is_interesting_document {
46 13     13   14 my ( $self, $document ) = @_;
47              
48 13         10 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
  13         35  
49 33 100       2314 return $TRUE if $document->uses_module($module);
50             }
51              
52 1         8 return $FALSE;
53             }
54              
55             sub violates {
56 6     6 1 33 my ( $self, undef, $document ) = @_;
57              
58 6         6 my @violations;
59 6         15 foreach my $namespace ( $document->namespaces() ) {
60             SUBDOCUMENT:
61 7         6426 foreach my $subdocument (
62             $document->subdocuments_for_namespace($namespace) ) {
63             next SUBDOCUMENT
64 7 100       61 if not $self->_is_interesting_document($subdocument);
65              
66 6 100       1160 if ( my $destructor
67             = $subdocument->find_first( \&_is_constructor ) ) {
68 3         75 push
69             @violations,
70             $self->violation(
71             $DESCRIPTION, $EXPLANATION,
72             $destructor
73             );
74             }
75             }
76             }
77              
78 6         411 return @violations;
79             }
80              
81             sub _is_constructor {
82 131     131   776 my ( undef, $element ) = @_;
83              
84 131 100       336 return $FALSE if not $element->isa('PPI::Statement::Sub');
85              
86 4         13 return $element->name() eq 'new';
87             }
88              
89             1;
90              
91             # ABSTRACT: Don't override Moose's standard constructors.
92              
93             __END__
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             Perl::Critic::Policy::Moose::ProhibitNewMethod - Don't override Moose's standard constructors.
102              
103             =head1 VERSION
104              
105             version 1.05
106              
107             =head1 DESCRIPTION
108              
109             Overriding C<new()> on a L<Moose> class causes a number of problems, including
110             speed issues and problems with order of invocation of constructors when
111             multiple inheritance is involved. Use C<BUILDARGS()> and C<BUILD()> instead.
112              
113             =head1 AFFILIATION
114              
115             This policy is part of L<Perl::Critic::Moose>.
116              
117             =head1 CONFIGURATION
118              
119             There is a single option, C<equivalent_modules>. This allows you to specify
120             modules that should be treated the same as L<Moose> and L<Moose::Role>, if,
121             say, you were doing something with L<Moose::Exporter>. For example, if you
122             were to have this in your F<.perlcriticrc> file:
123              
124             [Moose::ProhibitNewMethod]
125             equivalent_modules = MyCompany::Moose MooseX::NewThing
126              
127             then the following code would result in a violation:
128              
129             package Baz;
130              
131             use MyCompany::Moose;
132              
133             sub new {
134             ...
135             }
136              
137             =head1 SEE ALSO
138              
139             =over 4
140              
141             =item * L<Moose::Manual::Construction>
142              
143             =item * L<Moose::Manual::BestPractices>
144              
145             =back
146              
147             =head1 SUPPORT
148              
149             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Critic-Moose>
150             (or L<bug-perl-critic-moose@rt.cpan.org|mailto:bug-perl-critic-moose@rt.cpan.org>).
151              
152             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
153              
154             =head1 AUTHORS
155              
156             =over 4
157              
158             =item *
159              
160             Elliot Shank <perl@galumph.com>
161              
162             =item *
163              
164             Dave Rolsky <autarch@urth.org>
165              
166             =back
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is copyright (c) 2008 - 2016 by Elliot Shank.
171              
172             This is free software; you can redistribute it and/or modify it under
173             the same terms as the Perl 5 programming language system itself.
174              
175             =cut