Policy/Moose/ProhibitNewMethod.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 83.3% 5 / 6 totaltotal 95.8% 69 / 72 SCAR 20.8
CC
8
Cov
98%
violates
5.0
_is_interesting_document
3.0
default_themes
2.0
CRAP 8.0
« Policy/Moose/ProhibitDESTROYMethod.pm ↑ Summary Moose.pm »
1 package Perl::Critic::Policy::Moose::ProhibitNewMethod;
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 >;
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 =>
16 q<"new" method/subroutine declared in a Moose class.>;
17 1 Readonly::Scalar my $EXPLANATION =>
18 q<Use BUILDARGS and BUILD instead of writing your own constructor.>;
19  
20 sub supported_parameters {
21 return (
22 {
23 6 ▶ name => 'equivalent_modules',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 description =>
25 q<The additional modules to treat as equivalent to "Moose", "Moose::Role", or "MooseX::Role::Parameterized".>,
26 default_string => 'Moose Moose::Role MooseX::Role::Parameterized',
27 behavior => 'string list',
28 list_always_present_values =>
29 [qw< Moose Moose::Role MooseX::Role::Parameterized >],
30 },
31 );
32 }
33  
34 3 ▶sub default_severity { return $SEVERITY_HIGH; }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
35 0 ▶sub default_themes { return qw< moose bugs >; }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
36 6 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
37  
38 sub prepare_to_scan_document {
39 6 ▶ my ( $self, $document ) = @_;
Subroutine prepare_to_scan_document: called CC=1 SCAR=0.0
Pod documented
40  
41 6 return $self->_is_interesting_document($document);
42 }
43  
44 sub _is_interesting_document {
45 13 ▶ my ( $self, $document ) = @_;
Subroutine _is_interesting_document: called CC=3 SCAR=11.0
46  
47 13 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
48 23 ▶ return $TRUE if $document->uses_module($module);
Branch: 1 branch
BranchTrueFalseTotal
if $document->uses_module($module) 12 11 23
49 }
50  
51 1 return $FALSE;
52 }
53  
54 sub violates {
55 6 ▶ my ( $self, undef, $document ) = @_;
Subroutine violates: called CC=5 SCAR=16.1
Pod documented
56  
57 6 my @violations;
58 6 foreach my $namespace ( $document->namespaces() ) {
59 SUBDOCUMENT:
60 7 foreach my $subdocument (
61 $document->subdocuments_for_namespace($namespace) ) {
62 next SUBDOCUMENT
63 7 ▶ if not $self->_is_interesting_document($subdocument);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_interesting_document($subdocument) 1 6 7
64  
65 6 ▶ if ( my $destructor
Branch: 1 branch
BranchTrueFalseTotal
if (my $destructor = $subdocument->find_first(\&Perl::Critic::Policy::Moose::ProhibitNewMethod::_is_constructor)) 3 3 6
66 = $subdocument->find_first( \&_is_constructor ) ) {
67 3 push
68 @violations,
69 $self->violation(
70 $DESCRIPTION, $EXPLANATION,
71 $destructor
72 );
73 }
74 }
75 }
76  
77 6 return @violations;
78 }
79  
80 sub _is_constructor {
81 131 ▶ my ( undef, $element ) = @_;
Subroutine _is_constructor: called CC=2 SCAR=6.9
82  
83 131 ▶ return $FALSE if not $element->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement::Sub") 127 4 131
84  
85 4 return $element->name() eq 'new';
86 }
87  
88 1 1;
89  
90 # ABSTRACT: Don't override Moose's standard constructors.
91  
92 __END__
93  
94 =pod
95  
96 =head1 NAME
97  
98 Perl::Critic::Policy::Moose::ProhibitNewMethod - Don't override Moose's standard constructors.
99  
100 =head1 VERSION
101  
102 version 1.04
103  
104 =head1 DESCRIPTION
105  
106 Overriding C<new()> on a L<Moose> class causes a number of problems, including
107 speed issues and problems with order of invocation of constructors when
108 multiple inheritance is involved. Use C<BUILDARGS()> and C<BUILD()> instead.
109  
110 =head1 AFFILIATION
111  
112 This policy is part of L<Perl::Critic::Moose>.
113  
114 =head1 CONFIGURATION
115  
116 There is a single option, C<equivalent_modules>. This allows you to specify
117 modules that should be treated the same as L<Moose> and L<Moose::Role>, if,
118 say, you were doing something with L<Moose::Exporter>. For example, if you
119 were to have this in your F<.perlcriticrc> file:
120  
121 [Moose::ProhibitNewMethod]
122 equivalent_modules = MyCompany::Moose MooseX::NewThing
123  
124 then the following code would result in a violation:
125  
126 package Baz;
127  
128 use MyCompany::Moose;
129  
130 sub new {
131 ...
132 }
133  
134 =head1 SEE ALSO
135  
136 =over 4
137  
138 =item * L<Moose::Manual::Construction>
139  
140 =item * L<Moose::Manual::BestPractices>
141  
142 =back
143  
144 =head1 AUTHORS
145  
146 =over 4
147  
148 =item *
149  
150 Elliot Shank <perl@galumph.com>
151  
152 =item *
153  
154 Dave Rolsky <autarch@urth.org>
155  
156 =back
157  
158 =head1 COPYRIGHT AND LICENSE
159  
160 This software is copyright (c) 2008 - 2015 by Elliot Shank.
161  
162 This is free software; you can redistribute it and/or modify it under
163 the same terms as the Perl 5 programming language system itself.
164  
165 =cut
« Policy/Moose/ProhibitDESTROYMethod.pm ↑ Summary Moose.pm »