| 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
| |||||||||||
| 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
| |||||||||||
| 60 | |||||||||||
| 61 | 6 | my $current_token = $element->schild(0); | |||||||||
| 62 | 6 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 6 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 6 | ▶ | return $FALSE if $current_token->content() ne '__PACKAGE__'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 2 | $current_token = $current_token->snext_sibling(); | |||||||||
| 67 | 2 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Operator'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 2 | ▶ | return $FALSE if $current_token->content() ne '->'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 2 | $current_token = $current_token->snext_sibling(); | |||||||||
| 72 | 2 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 2 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 2 | ▶ | return $FALSE if $current_token->content() ne 'meta'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 2 | $current_token = $current_token->snext_sibling(); | |||||||||
| 77 | 2 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 2 | ▶ | if ( $current_token->isa('PPI::Structure::List') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 1 | $current_token = $current_token->snext_sibling(); | |||||||||
| 80 | 1 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 2 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Operator'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 2 | ▶ | return $FALSE if $current_token->content() ne '->'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | 2 | $current_token = $current_token->snext_sibling(); | |||||||||
| 87 | 2 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 2 | ▶ | return $FALSE if $current_token->content() ne 'make_immutable'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 2 | return $TRUE; | |||||||||
| 92 | } | ||||||||||
| 93 | 4 | ); | |||||||||
| 94 | |||||||||||
| 95 | 4 | ▶ | return if $makes_immutable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ||||||||||