File Coverage

blib/lib/Perl/Critic/Policy/Moose/ProhibitLazyBuild.pm
Criterion Covered Total %
statement 41 43 95.3
branch 13 16 81.2
condition n/a
subroutine 13 14 92.8
pod 5 6 83.3
total 72 79 91.1


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Moose::ProhibitLazyBuild;
2              
3 1     1   470085 use strict;
  1         3  
  1         25  
4 1     1   5 use warnings;
  1         3  
  1         39  
5              
6             our $VERSION = '1.04';
7              
8 1     1   5 use Readonly ();
  1         2  
  1         19  
9              
10 1     1   5 use Perl::Critic::Utils qw< :booleans :severities >;
  1         2  
  1         94  
11 1     1   241 use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >;
  1         2  
  1         51  
12              
13 1     1   5 use base 'Perl::Critic::Policy';
  1         2  
  1         1716  
14             Readonly::Scalar my $DESCRIPTION => 'lazy_build is discouraged.';
15             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     4 0 17692 name => 'equivalent_modules',
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     2 1 25 sub default_severity { return $SEVERITY_LOW; }
35 0     0 1 0 sub default_themes { return qw< moose bugs >; }
36 4     4 1 161 sub applies_to { return 'PPI::Document' }
37              
38             sub prepare_to_scan_document {
39 4     4 1 40645 my ( $self, $document ) = @_;
40              
41 4         15 return $self->_is_interesting_document($document);
42             }
43              
44             sub _is_interesting_document {
45 4     4   9 my ( $self, $document ) = @_;
46              
47 4         7 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
  4         19  
48 9 100       216 return $TRUE if $document->uses_module($module);
49             }
50              
51 0         0 return $FALSE;
52             }
53              
54             sub violates {
55 4     4 1 40 my ( $self, undef, $document ) = @_;
56              
57             my $uses_lazy_build = $document->find_any(
58             sub {
59 95     95   1786 my ( undef, $element ) = @_;
60 95 100       229 return $FALSE if not is_ppi_generic_statement($element);
61 4         45 my $current_token = $element->schild(0);
62 4 50       54 return $FALSE if not $current_token;
63 4 50       15 return $FALSE if not $current_token->isa('PPI::Token::Word');
64 4 50       15 return $FALSE if $current_token->content() ne 'has';
65 4         30 my $parent = $current_token->parent;
66             my $lazy_build = grep {
67 4 100       26 $_->isa('PPI::Token::Word')
  120         1007  
68             && $_->content() eq 'lazy_build'
69             } $parent->tokens;
70 4 100       22 return $lazy_build ? $TRUE : $FALSE;
71             }
72 4         40 );
73 4 100       101 return unless $uses_lazy_build;
74 2         13 return $self->violation( $DESCRIPTION, $EXPLANATION, $document );
75             }
76              
77             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