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