| 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 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '1.05'; | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Readonly (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Perl::Critic::Utils qw< :booleans :severities >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | Readonly::Scalar my $DESCRIPTION => 'No call was made to make_immutable().'; | |||||||||
| 17 | 1 | Readonly::Scalar my $EXPLANATION => | |||||||||
| 18 | q<Moose can't optimize itself if classes remain mutable.>; | ||||||||||
| 19 | |||||||||||
| 20 | 4 | ▶ | sub supported_parameters { | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | return ( | ||||||||||
| 22 | { | ||||||||||
| 23 | 4 | name => 'equivalent_modules', | |||||||||
| 24 | description => | ||||||||||
| 25 | q<The additional modules to treat as equivalent to "Moose".>, | ||||||||||
| 26 | default_string => 'Moose', | ||||||||||
| 27 | behavior => 'string list', | ||||||||||
| 28 | list_always_present_values => [qw< Moose >], | ||||||||||
| 29 | }, | ||||||||||
| 30 | ); | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | sub default_severity { return $SEVERITY_MEDIUM; } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 0 | ▶ | sub default_themes { return qw( moose performance ); } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 4 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | |||||||||||
| 37 | 4 | ▶ | sub prepare_to_scan_document { | ||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | 4 | my ( $self, $document ) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 4 | return $self->_is_interesting_document($document); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 4 | ▶ | sub _is_interesting_document { | ||||||||
|
Subroutine
_is_interesting_document: called CC=3 SCAR=11.1
| |||||||||||
| 44 | 4 | my ( $self, $document ) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 4 | foreach my $module ( keys %{ $self->{_equivalent_modules} } ) { | |||||||||
| 47 | 5 | ▶ | return $TRUE if $document->uses_module($module); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | return $FALSE; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 4 | ▶ | sub violates { | ||||||||
|
Subroutine
violates: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 54 | 4 | my ( $self, undef, $document ) = @_; | |||||||||
| 55 | |||||||||||
| 56 | my $makes_immutable = $document->find_any( | ||||||||||
| 57 | 155 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=19 SCAR=31.8
| |||||||||||
| 58 | 155 | my ( undef, $element ) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 155 | ▶ | return $FALSE if not is_ppi_generic_statement($element); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 6 | my $current_token = $element->schild(0); | |||||||||
| 63 | 6 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 6 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 6 | ▶ | 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 | 4 | ); | |||||||||
| 95 | |||||||||||
| 96 | 4 | ▶ | return if $makes_immutable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 2 | return $self->violation( $DESCRIPTION, $EXPLANATION, $document ); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1 | 1; | |||||||||
| 101 | |||||||||||
| 102 | # ABSTRACT: Ensure that you've made your Moose code fast | ||||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||