| 1 | package Perl::Critic::Policy::Moose::ProhibitLazyBuild; | ||||||||||||||||||||
| 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 | 1 | Readonly::Scalar my $DESCRIPTION => 'lazy_build is discouraged.'; | |||||||||||||||||||
| 16 | 1 | Readonly::Scalar my $EXPLANATION => | |||||||||||||||||||
| 17 | q(lazy_build pollutes the namespace and encourages mutability. See https://metacpan.org/pod/distribution/Moose/lib/Moose/Manual/BestPractices.pod#Avoid-lazy_build .); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 4 | ▶ | sub supported_parameters { | ||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 20 | return ( | ||||||||||||||||||||
| 21 | { | ||||||||||||||||||||
| 22 | 4 | name => 'equivalent_modules', | |||||||||||||||||||
| 23 | description => | ||||||||||||||||||||
| 24 | q<The additional modules to treat as equivalent to "Moose", "Moose::Role", "MooseX::Role::Parameterized", or "MooseX::Singleton".>, | ||||||||||||||||||||
| 25 | default_string => | ||||||||||||||||||||
| 26 | 'Moose Moose::Role MooseX::Role::Parameterized MooseX::Singleton', | ||||||||||||||||||||
| 27 | behavior => 'string list', | ||||||||||||||||||||
| 28 | list_always_present_values => [ | ||||||||||||||||||||
| 29 | qw< Moose Moose::Role MooseX::Role::Parameterized MooseX::Singleton > | ||||||||||||||||||||
| 30 | ], | ||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | ▶ | sub default_severity { return $SEVERITY_LOW; } | ||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | 0 | ▶ | sub default_themes { return qw< moose bugs >; } | ||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 37 | 4 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 4 | ▶ | sub prepare_to_scan_document { | ||||||||||||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 40 | 4 | my ( $self, $document ) = @_; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 4 | return $self->_is_interesting_document($document); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 4 | ▶ | sub _is_interesting_document { | ||||||||||||||||||
|
Subroutine
_is_interesting_document: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 46 | 4 | my ( $self, $document ) = @_; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 4 | foreach my $module ( keys %{ $self->{_equivalent_modules} } ) { | |||||||||||||||||||
| 49 | 13 | ▶ | return $TRUE if $document->uses_module($module); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | return $FALSE; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 4 | ▶ | sub violates { | ||||||||||||||||||
|
Subroutine
violates: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 56 | 4 | my ( $self, undef, $document ) = @_; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | my $uses_lazy_build = $document->find_any( | ||||||||||||||||||||
| 59 | 95 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||
| 60 | 95 | my ( undef, $element ) = @_; | |||||||||||||||||||
| 61 | 95 | ▶ | return $FALSE if not is_ppi_generic_statement($element); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 4 | my $current_token = $element->schild(0); | |||||||||||||||||||
| 63 | 4 | ▶ | return $FALSE if not $current_token; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 4 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 4 | ▶ | return $FALSE if $current_token->content() ne 'has'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 4 | my $parent = $current_token->parent; | |||||||||||||||||||
| 67 | my $lazy_build = grep { | ||||||||||||||||||||
| 68 | 4 | ▶ | $_->isa('PPI::Token::Word') | ||||||||||||||||||
|
Condition100%3/3
$_->isa('PPI::Token::Word') && $_->content eq 'lazy_build'
MC/DC100%2/2
$_->isa('PPI::Token::Word') && $_->content eq 'lazy_build'
$_->isa('PPI::Token::Word')
$_->content eq 'lazy_build'
Truth table100%3/3
A && B
A= $_->isa('PPI::Token::Word')B= $_->content eq 'lazy_build'
| |||||||||||||||||||||
| 69 | && $_->content() eq 'lazy_build' | ||||||||||||||||||||
| 70 | } $parent->tokens; | ||||||||||||||||||||
| 71 | 4 | ▶ | return $lazy_build ? $TRUE : $FALSE; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 4 | ); | |||||||||||||||||||
| 74 | 4 | ▶ | return unless $uses_lazy_build; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 2 | return $self->violation( $DESCRIPTION, $EXPLANATION, $document ); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # ABSTRACT: Avoid lazy_build | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||