File Coverage

blib/lib/Perl/Critic/Policy/Moose/ProhibitDESTROYMethod.pm
Criterion Covered Total %
statement 39 40 97.5
branch 8 8 100.0
condition n/a
subroutine 13 14 92.8
pod 5 6 83.3
total 65 68 95.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Moose::ProhibitDESTROYMethod;
2              
3 1     1   544 use strict;
  1         2  
  1         29  
4 1     1   3 use warnings;
  1         1  
  1         18  
5 1     1   3 use namespace::autoclean;
  1         1  
  1         3  
6              
7             our $VERSION = '1.05';
8              
9 1     1   52 use Readonly ();
  1         1  
  1         13  
10              
11 1     1   4 use Perl::Critic::Utils qw< :booleans :severities $EMPTY >;
  1         1  
  1         45  
12              
13 1     1   113 use base 'Perl::Critic::Policy';
  1         1  
  1         344  
14              
15             Readonly::Scalar my $DESCRIPTION =>
16             q<"DESTROY" method/subroutine declared in a Moose class.>;
17             Readonly::Scalar my $EXPLANATION => q<Use DEMOLISH for your destructors.>;
18              
19             sub supported_parameters {
20             return (
21             {
22 6     6 0 27926 name => 'equivalent_modules',
23             description =>
24             q<The additional modules to treat as equivalent to "Moose", "Moose::Role", or "MooseX::Role::Parameterized".>,
25             behavior => 'string list',
26             list_always_present_values =>
27             [qw< Moose Moose::Role MooseX::Role::Parameterized >],
28             },
29             );
30             }
31              
32 3     3 1 26 sub default_severity { return $SEVERITY_MEDIUM; }
33 0     0 1 0 sub default_themes { return qw< moose bugs >; }
34 6     6 1 273 sub applies_to { return 'PPI::Document' }
35              
36             sub prepare_to_scan_document {
37 6     6 1 27184 my ( $self, $document ) = @_;
38              
39 6         14 return $self->_is_interesting_document($document);
40             }
41              
42             sub _is_interesting_document {
43 13     13   13 my ( $self, $document ) = @_;
44              
45 13         11 foreach my $module ( keys %{ $self->{_equivalent_modules} } ) {
  13         31  
46 17 100       585 return $TRUE if $document->uses_module($module);
47             }
48              
49 1         9 return $FALSE;
50             }
51              
52             sub violates {
53 6     6 1 39 my ( $self, undef, $document ) = @_;
54              
55 6         8 my @violations;
56 6         11 foreach my $namespace ( $document->namespaces() ) {
57             SUBDOCUMENT:
58 7         6536 foreach my $subdocument (
59             $document->subdocuments_for_namespace($namespace) ) {
60             next SUBDOCUMENT
61 7 100       54 if not $self->_is_interesting_document($subdocument);
62              
63 6 100       2622 if ( my $destructor
64             = $subdocument->find_first( \&_is_destructor ) ) {
65 3         71 push
66             @violations,
67             $self->violation(
68             $DESCRIPTION, $EXPLANATION,
69             $destructor
70             );
71             }
72             }
73             }
74              
75 6         500 return @violations;
76             }
77              
78             sub _is_destructor {
79 133     133   779 my ( undef, $element ) = @_;
80              
81 133 100       372 return $FALSE if not $element->isa('PPI::Statement::Sub');
82              
83 5         14 return $element->name() eq 'DESTROY';
84             }
85              
86             1;
87              
88             # ABSTRACT: Use DEMOLISH instead of DESTROY
89              
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             Perl::Critic::Policy::Moose::ProhibitDESTROYMethod - Use DEMOLISH instead of DESTROY
99              
100             =head1 VERSION
101              
102             version 1.05
103              
104             =head1 DESCRIPTION
105              
106             Getting the order of destructor execution correct with inheritance involved is
107             difficult. Let L<Moose> take care of it for you by putting your cleanup code
108             into a C<DEMOLISH()> method instead of a C<DESTROY()> method.
109              
110             # ok
111             package Foo;
112              
113             use Moose::Role;
114              
115             sub DEMOLISH {
116             ...
117             }
118              
119             # not ok
120             package Foo;
121              
122             use Moose::Role;
123              
124             sub DESTROY {
125             ...
126             }
127              
128             =for stopwords destructor
129              
130             =head1 AFFILIATION
131              
132             This policy is part of L<Perl::Critic::Moose>.
133              
134             =head1 CONFIGURATION
135              
136             There is a single option, C<equivalent_modules>. This allows you to specify
137             modules that should be treated the same as L<Moose> and L<Moose::Role>, if,
138             say, you were doing something with L<Moose::Exporter>. For example, if you
139             were to have this in your F<.perlcriticrc> file:
140              
141             [Moose::ProhibitDESTROYMethod]
142             equivalent_modules = MyCompany::Moose MooseX::NewThing
143              
144             then the following code would result in a violation:
145              
146             package Baz;
147              
148             use MyCompany::Moose;
149              
150             sub DESTROY {
151             ...
152             }
153              
154             =head1 SEE ALSO
155              
156             L<Moose::Manual::Construction>
157              
158             =head1 SUPPORT
159              
160             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Critic-Moose>
161             (or L<bug-perl-critic-moose@rt.cpan.org|mailto:bug-perl-critic-moose@rt.cpan.org>).
162              
163             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
164              
165             =head1 AUTHORS
166              
167             =over 4
168              
169             =item *
170              
171             Elliot Shank <perl@galumph.com>
172              
173             =item *
174              
175             Dave Rolsky <autarch@urth.org>
176              
177             =back
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2008 - 2016 by Elliot Shank.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut