File Coverage

blib/lib/Perl/Critic/Policy/Moose/ProhibitLazyBuild.pm
Criterion Covered Total %
statement 44 46 95.6
branch 13 16 81.2
condition n/a
subroutine 14 15 93.3
pod 5 6 83.3
total 76 83 91.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Moose::ProhibitLazyBuild;
2              
3 1     1   154992 use strict;
  1         1  
  1         22  
4 1     1   3 use warnings;
  1         1  
  1         18  
5 1     1   405 use namespace::autoclean;
  1         10184  
  1         2  
6              
7             our $VERSION = '1.05';
8              
9 1     1   53 use Readonly ();
  1         1  
  1         14  
10              
11 1     1   4 use Perl::Critic::Utils qw< :booleans :severities >;
  1         1  
  1         55  
12 1     1   115 use Perl::Critic::Utils::PPI qw< is_ppi_generic_statement >;
  1         1  
  1         36  
13              
14 1     1   4 use base 'Perl::Critic::Policy';
  1         1  
  1         486  
15             Readonly::Scalar my $DESCRIPTION => 'lazy_build is discouraged.';
16             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             sub supported_parameters {
20             return (
21             {
22 4     4 0 11000 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     2 1 63 sub default_severity { return $SEVERITY_LOW; }
36 0     0 1 0 sub default_themes { return qw< moose bugs >; }
37 4     4 1 104 sub applies_to { return 'PPI::Document' }
38              
39             sub prepare_to_scan_document {
40 4     4 1 25074 my ( $self, $document ) = @_;
41              
42 4         11 return $self->_is_interesting_document($document);
43             }
44              
45             sub _is_interesting_document {
46 4     4   4 my ( $self, $document ) = @_;
47              
48 4         4 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
  4         15  
49 11 100       159 return $TRUE if $document->uses_module($module);
50             }
51              
52 0         0 return $FALSE;
53             }
54              
55             sub violates {
56 4     4 1 27 my ( $self, undef, $document ) = @_;
57              
58             my $uses_lazy_build = $document->find_any(
59             sub {
60 95     95   1026 my ( undef, $element ) = @_;
61 95 100       144 return $FALSE if not is_ppi_generic_statement($element);
62 4         30 my $current_token = $element->schild(0);
63 4 50       39 return $FALSE if not $current_token;
64 4 50       11 return $FALSE if not $current_token->isa('PPI::Token::Word');
65 4 50       9 return $FALSE if $current_token->content() ne 'has';
66 4         22 my $parent = $current_token->parent;
67             my $lazy_build = grep {
68 4 100       18 $_->isa('PPI::Token::Word')
  120         517  
69             && $_->content() eq 'lazy_build'
70             } $parent->tokens;
71 4 100       16 return $lazy_build ? $TRUE : $FALSE;
72             }
73 4         20 );
74 4 100       66 return unless $uses_lazy_build;
75 2         12 return $self->violation( $DESCRIPTION, $EXPLANATION, $document );
76             }
77              
78             1;
79              
80             # ABSTRACT: Avoid lazy_build
81              
82             __END__
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             Perl::Critic::Policy::Moose::ProhibitLazyBuild - Avoid lazy_build
91              
92             =head1 VERSION
93              
94             version 1.05
95              
96             =head1 DESCRIPTION
97              
98             C< lazy_build => 1 > seemed like a good idea at the time, but it
99             creates problems (see
100             L<here|https://metacpan.org/pod/distribution/Moose/lib/Moose/Manual/BestPractices.pod#Avoid-lazy_build>).
101             This policy will complain if it finds lazy_build => 1 in your code.
102              
103             =head1 AFFILIATION
104              
105             This policy is part of L<Perl::Critic::Moose>.
106              
107             =head1 SUPPORT
108              
109             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Critic-Moose>
110             (or L<bug-perl-critic-moose@rt.cpan.org|mailto:bug-perl-critic-moose@rt.cpan.org>).
111              
112             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
113              
114             =head1 AUTHORS
115              
116             =over 4
117              
118             =item *
119              
120             Elliot Shank <perl@galumph.com>
121              
122             =item *
123              
124             Dave Rolsky <autarch@urth.org>
125              
126             =back
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2008 - 2016 by Elliot Shank.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut