Policy/Moose/RequireMakeImmutable.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 60.0% 24 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 83.3% 5 / 6 totaltotal 83.7% 103 / 123 SCAR 32.0
CC
22
Cov
83%
__ANON__
24.1
_is_interesting_document
3.2
violates
2.0
CRAP 24.6
↑ Summary Policy/Moose/ProhibitMultipleWiths.pm »
1 package Perl::Critic::Policy::Moose::RequireMakeImmutable;
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 => 'No call was made to make_immutable().';
16 1 Readonly::Scalar my $EXPLANATION =>
17 q<Moose can't optimize itself if classes remain mutable.>;
18  
19 sub supported_parameters {
20 return (
21 {
22 4 ▶ 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".>,
25 default_string => 'Moose',
26 behavior => 'string list',
27 list_always_present_values => [qw< Moose >],
28 },
29 );
30 }
31  
32 2 ▶sub default_severity { return $SEVERITY_MEDIUM; }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
33 0 ▶sub default_themes { return qw( moose performance ); }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
34 4 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
35  
36 sub prepare_to_scan_document {
37 4 ▶ my ( $self, $document ) = @_;
Subroutine prepare_to_scan_document: called CC=1 SCAR=0.0
Pod documented
38  
39 4 return $self->_is_interesting_document($document);
40 }
41  
42 sub _is_interesting_document {
43 4 ▶ my ( $self, $document ) = @_;
Subroutine _is_interesting_document: called CC=3 SCAR=11.7
44  
45 4 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
46 4 ▶ return $TRUE if $document->uses_module($module);
Branch: 1 branch
BranchTrueFalseTotal
if $document->uses_module($module) 4 0 4
47 }
48  
49 0 return $FALSE;
50 }
51  
52 sub violates {
53 4 ▶ my ( $self, undef, $document ) = @_;
Subroutine violates: called CC=2 SCAR=7.1
Pod documented
54  
55 my $makes_immutable = $document->find_any(
56 sub {
57 155 ▶ my ( undef, $element ) = @_;
Subroutine __ANON__: called CC=19 SCAR=31.8
58  
59 155 ▶ return $FALSE if not is_ppi_generic_statement($element);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ppi_generic_statement($element) 149 6 155
60  
61 6 my $current_token = $element->schild(0);
62 6 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 6 6
63 6 ▶ return $FALSE if not $current_token->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Word") 0 6 6
64 6 ▶ return $FALSE if $current_token->content() ne '__PACKAGE__';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "__PACKAGE__" 4 2 6
65  
66 2 $current_token = $current_token->snext_sibling();
67 2 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 2 2
68 2 ▶ return $FALSE if not $current_token->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Operator") 0 2 2
69 2 ▶ return $FALSE if $current_token->content() ne '->';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "->" 0 2 2
70  
71 2 $current_token = $current_token->snext_sibling();
72 2 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 2 2
73 2 ▶ return $FALSE if not $current_token->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Word") 0 2 2
74 2 ▶ return $FALSE if $current_token->content() ne 'meta';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "meta" 0 2 2
75  
76 2 $current_token = $current_token->snext_sibling();
77 2 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 2 2
78 2 ▶ if ( $current_token->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_token->isa("PPI::Structure::List")) 1 1 2
79 1 $current_token = $current_token->snext_sibling();
80 1 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 1 1
81 }
82  
83 2 ▶ return $FALSE if not $current_token->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Operator") 0 2 2
84 2 ▶ return $FALSE if $current_token->content() ne '->';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "->" 0 2 2
85  
86 2 $current_token = $current_token->snext_sibling();
87 2 ▶ return $FALSE if not $current_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token 0 2 2
88 2 ▶ return $FALSE if not $current_token->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_token->isa("PPI::Token::Word") 0 2 2
89 2 ▶ return $FALSE if $current_token->content() ne 'make_immutable';
Branch: 1 branch
BranchTrueFalseTotal
if $current_token->content ne "make_immutable" 0 2 2
90  
91 2 return $TRUE;
92 }
93 4 );
94  
95 4 ▶ return if $makes_immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $makes_immutable 2 2 4
96 2 return $self->violation( $DESCRIPTION, $EXPLANATION, $document );
97 }
98  
99 1 1;
100  
101 # ABSTRACT: Ensure that you've made your Moose code fast
102  
103 __END__
104  
105 =pod
106  
107 =head1 NAME
108  
109 Perl::Critic::Policy::Moose::RequireMakeImmutable - Ensure that you've made your Moose code fast
110  
111 =head1 VERSION
112  
113 version 1.04
114  
115 =head1 DESCRIPTION
116  
117 L<Moose> is very flexible. That flexibility comes at a performance cost. You
118 can ameliorate some of that cost by telling Moose when you are done putting
119 your classes together.
120  
121 Thus, if you C<use Moose>, this policy requires that you do
122 C<< __PACKAGE__->meta()->make_immutable() >>.
123  
124 =head1 AFFILIATION
125  
126 This policy is part of L<Perl::Critic::Moose>.
127  
128 =head1 CONFIGURATION
129  
130 There is a single option, C<equivalent_modules>. This allows you to specify
131 modules that should be treated the same as L<Moose> and L<Moose::Role>, if,
132 say, you were doing something with L<Moose::Exporter>. For example, if you
133 were to have this in your F<.perlcriticrc> file:
134  
135 [Moose::RequireMakeImmutable]
136 equivalent_modules = MyCompany::Moose MooseX::NewThing
137  
138 then the following code would result in a violation:
139  
140 package Baz;
141  
142 use MyCompany::Moose;
143  
144 sub new {
145 ...
146 }
147  
148 # no make_immutable call
149  
150 =head1 AUTHORS
151  
152 =over 4
153  
154 =item *
155  
156 Elliot Shank <perl@galumph.com>
157  
158 =item *
159  
160 Dave Rolsky <autarch@urth.org>
161  
162 =back
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 This software is copyright (c) 2008 - 2015 by Elliot Shank.
167  
168 This is free software; you can redistribute it and/or modify it under
169 the same terms as the Perl 5 programming language system itself.
170  
171 =cut
↑ Summary Policy/Moose/ProhibitMultipleWiths.pm »