| 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 | |||||||||||
| 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 | 1 | Readonly::Scalar my $DESCRIPTION => 'lazy_build is discouraged.'; | |||||||||
| 15 | 1 | Readonly::Scalar my $EXPLANATION => | |||||||||
| 16 | q(lazy_build pollutes the namespace and encourages mutability. See https://metacpan.org/pod/distribution/Moose/lib/Moose/Manual/BestPractices.pod#Avoid-lazy_build .); | ||||||||||
| 17 | |||||||||||
| 18 | sub supported_parameters { | ||||||||||
| 19 | return ( | ||||||||||
| 20 | { | ||||||||||
| 21 | 4 | ▶ | name => 'equivalent_modules', | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | description => | ||||||||||
| 23 | q<The additional modules to treat as equivalent to "Moose", "Moose::Role", "MooseX::Role::Parameterized", or "MooseX::Singleton".>, | ||||||||||
| 24 | default_string => | ||||||||||
| 25 | 'Moose Moose::Role MooseX::Role::Parameterized MooseX::Singleton', | ||||||||||
| 26 | behavior => 'string list', | ||||||||||
| 27 | list_always_present_values => [ | ||||||||||
| 28 | qw< Moose Moose::Role MooseX::Role::Parameterized MooseX::Singleton > | ||||||||||
| 29 | ], | ||||||||||
| 30 | }, | ||||||||||
| 31 | ); | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 2 | ▶ | sub default_severity { return $SEVERITY_LOW; } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | 0 | ▶ | sub default_themes { return qw< moose bugs >; } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 36 | 4 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | |||||||||||
| 38 | sub prepare_to_scan_document { | ||||||||||
| 39 | 4 | ▶ | my ( $self, $document ) = @_; | ||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | 4 | return $self->_is_interesting_document($document); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub _is_interesting_document { | ||||||||||
| 45 | 4 | ▶ | my ( $self, $document ) = @_; | ||||||||
|
Subroutine
_is_interesting_document: called CC=3 SCAR=11.1
| |||||||||||
| 46 | |||||||||||
| 47 | 4 | foreach my $module ( keys %{ $self->{_equivalent_modules} } ) { | |||||||||
| 48 | 8 | ▶ | return $TRUE if $document->uses_module($module); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | return $FALSE; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub violates { | ||||||||||
| 55 | 4 | ▶ | my ( $self, undef, $document ) = @_; | ||||||||
|
Subroutine
violates: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 56 | |||||||||||
| 57 | my $uses_lazy_build = $document->find_any( | ||||||||||
| 58 | sub { | ||||||||||
| 59 | 95 | ▶ | my ( undef, $element ) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||
| 60 | 95 | ▶ | return $FALSE if not is_ppi_generic_statement($element); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 4 | my $current_token = $element->schild(0); | |||||||||
| 62 | 4 | ▶ | return $FALSE if not $current_token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 4 | ▶ | return $FALSE if not $current_token->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 4 | ▶ | return $FALSE if $current_token->content() ne 'has'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 4 | my $parent = $current_token->parent; | |||||||||
| 66 | my $lazy_build = grep { | ||||||||||
| 67 | 4 | ▶ | $_->isa('PPI::Token::Word') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | && $_->content() eq 'lazy_build' | ||||||||||
| 69 | } $parent->tokens; | ||||||||||
| 70 | 4 | ▶ | return $lazy_build ? $TRUE : $FALSE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | 4 | ); | |||||||||
| 73 | 4 | ▶ | return unless $uses_lazy_build; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 2 | return $self->violation( $DESCRIPTION, $EXPLANATION, $document ); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 1 | 1; | |||||||||
| 78 | |||||||||||
| 79 | # ABSTRACT: Avoid lazy_build | ||||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||
| 82 | |||||||||||
| 83 | =pod | ||||||||||
| 84 | |||||||||||
| 85 | =head1 NAME | ||||||||||
| 86 | |||||||||||
| 87 | Perl::Critic::Policy::Moose::ProhibitLazyBuild - Avoid lazy_build | ||||||||||
| 88 | |||||||||||
| 89 | =head1 VERSION | ||||||||||
| 90 | |||||||||||
| 91 | version 1.04 | ||||||||||
| 92 | |||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||
| 94 | |||||||||||
| 95 | C< lazy_build => 1 > seemed like a good idea at the time, but it | ||||||||||
| 96 | creates problems (see | ||||||||||
| 97 | L<here|https://metacpan.org/pod/distribution/Moose/lib/Moose/Manual/BestPractices.pod#Avoid-lazy_build>). | ||||||||||
| 98 | This policy will complain if it finds lazy_build => 1 in your code. | ||||||||||
| 99 | |||||||||||
| 100 | =head1 AFFILIATION | ||||||||||
| 101 | |||||||||||
| 102 | This policy is part of L<Perl::Critic::Moose>. | ||||||||||
| 103 | |||||||||||
| 104 | =head1 AUTHORS | ||||||||||
| 105 | |||||||||||
| 106 | =over 4 | ||||||||||
| 107 | |||||||||||
| 108 | =item * | ||||||||||
| 109 | |||||||||||
| 110 | Elliot Shank <perl@galumph.com> | ||||||||||
| 111 | |||||||||||
| 112 | =item * | ||||||||||
| 113 | |||||||||||
| 114 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 115 | |||||||||||
| 116 | =back | ||||||||||
| 117 | |||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 119 | |||||||||||
| 120 | This software is copyright (c) 2008 - 2015 by Elliot Shank. | ||||||||||
| 121 | |||||||||||
| 122 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 123 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||