make_immutable
[gitmo/Perl-Critic-Dynamic-Moose.git] / lib / Perl / Critic / Policy / DynamicMoose.pm
1 package Perl::Critic::Policy::DynamicMoose;
2 use Moose;
3 use MooseX::NonMoose;
4 extends 'Perl::Critic::DynamicPolicy';
5
6 has document => (
7     is      => 'rw',
8     isa     => 'PPI::Document',
9     handles => [qw/ppi_document/],
10 );
11
12 sub applies_to { 'PPI::Document' }
13 sub applies_to_metaclass { 'Class::MOP::Class', inner() }
14 sub default_themes { qw(moose dynamic), inner() }
15
16 around violation => sub {
17     my $orig    = shift;
18     my $self    = shift;
19     my $desc    = shift;
20     my $expl    = shift;
21     my $element = shift;
22
23     if (!$element) {
24         my $doc = $self->ppi_document;
25
26         # Without this hack, Storable complains of being unable to reconstruct
27         # overloading for an unknown package (perhaps PPI::Document?). For some
28         # reason it works for PPI::Element. Anyway, this should hopefully be
29         # replaced with a more useful location, something like
30         # ( class:MyClass / attr:foo / builder:build_foo )
31         $element = $doc->find('PPI::Element')->[0];
32     }
33
34     return $self->$orig($desc, $expl, $element, @_);
35 };
36
37 sub violates_dynamic {
38     my $self = shift;
39     my $doc  = shift;
40
41     $self->document($doc);
42
43     my $old_packages = $self->_find_packages;
44     $self->_compile_document;
45     my @new_packages = $self->_new_packages($old_packages);
46
47     my @violations;
48     for my $package (@new_packages) {
49         my $meta = Class::MOP::class_of($package)
50             or next;
51
52         grep { $meta->isa($_) } $self->applies_to_metaclass
53             or next;
54
55         push @violations, $self->violates_metaclass($meta, $doc);
56     }
57
58     return @violations;
59 }
60
61 sub violates_metaclass { die "Your policy (" . blessed($_[0]) . ") needs to implement violates_metaclass" }
62
63 sub _compile_document {
64     my $self = shift;
65     my $doc = $self->document;
66
67     my $source_code = $doc->content;
68
69     eval $source_code;
70
71     die "Unable to execute " . $doc->filename . ": $@" if $@;
72 }
73
74 sub _find_packages {
75     my $self = shift;
76     return [ Class::MOP::get_all_metaclass_names ];
77 }
78
79 sub _new_packages {
80     my $self = shift;
81     my $old  = shift;
82     my @new;
83     my %seen;
84
85     $seen{$_} = 1 for @$old;
86
87     for (@{ $self->_find_packages }) {
88         push @new, $_ if !$seen{$_}++;
89     }
90
91     return @new;
92 }
93
94 no Moose;
95 __PACKAGE__->meta->make_immutable;
96
97 1;
98
99 __END__
100
101 =head1 NAME
102
103 Perl::Critic::Policy::DynamicMoose
104
105 =head1 DESCRIPTION
106
107 This class is a base class for dynamic Moose policies. This class facilitates
108 critiquing metaclasses (instead of the usual PPI documents). For example, the
109 L<Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders> policy critiques
110 metaclasses by checking whether any of their attributes' builders do not start
111 with an underscore. Due to the very dynamic nature of Moose and
112 metaprogramming, such policies will be much more effective than static analysis
113 at critiquing classes.
114
115 =head1 PUBLIC METHODS
116
117 =over 4
118
119 =item C<applies_to_metaclass>
120
121 Returns a list of metaclass names that this policy can critique. By default,
122 the list is L<Class::MOP::Class>. You may use the augment modifier to add
123 other kinds of metaclasses, such as L<Moose::Meta::Role> without having to
124 repeat the L<Class::MOP::Class>:
125
126     augment applies_to_metaclass => sub { 'Moose::Meta::Role' };
127
128 Note that only the top-level metaclass is given to you. If you want to critique
129 only attributes, then you must do the Visiting yourself.
130
131 =item C<applies_to_themes>
132
133 Returns a list of themes for Perl::Critic so that users can run a particular
134 subset of themes on their code. By default, the list contains C<moose> and
135 C<dynamic>. You should use the augment modifier to add more themes instead
136 of overriding the method:
137
138     augment themes => sub { 'role' };
139
140 =item C<violation>
141
142 This extends the regular L<Perl::Critic::Policy/violation> method by providing
143 a (rather useless) default value for the C<element> parameter. For nearly all
144 cases, there's no easy way to find where a metaclass violation occurred. You
145 may still pass such an element if you have one. However, since you probably do
146 not, you should be exact in your violation's description.
147
148 =item C<violates_metaclass>
149
150 This method is required to be overridden by subclasses. It takes a metaclass
151 object and the L<Perl::Critic::Document> representing the entire compilation
152 unit. It is expected to return a list of L<Perl::Critic::Violation> objects.
153
154 =over
155
156 =head1 POLICIES
157
158 The included policies are:
159
160 =over 4
161
162 =item L<Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders>
163
164 Prohibit public builder methods for attributes. [Severity: 3]
165
166 =back
167
168 =head1 WARNING
169
170 B<VERY IMPORTANT:> Most L<Perl::Critic> Policies (including all the ones that
171 ship with Perl::Critic> use pure static analysis -- they never compile nor
172 execute any of the code that they analyze.  However, this policy is very
173 different.  It actually attempts to compile your code and then compares the
174 subroutines mentioned in your code to those found in the symbol table.
175 Therefore you should B<not> use this Policy on any code that you do not trust,
176 or may have undesirable side-effects at compile-time (such as connecting to the
177 network or mutating files).
178
179 For this Policy to work, all the modules included in your code must be
180 installed locally, and must compile without error.
181
182 =head1 AUTHOR
183
184 Shawn M Moore, C<sartak@bestpractical.com>
185
186 =cut
187