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