Commit | Line | Data |
84a1bb62 |
1 | package Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders; |
ac490c58 |
2 | use Moose; |
84a1bb62 |
3 | extends 'Perl::Critic::Policy::DynamicMoose'; |
ac490c58 |
4 | |
e3f2864c |
5 | use Perl::Critic::Utils ':severities'; |
8013ebe8 |
6 | use Perl::Critic::Utils::Moose 'meta_type'; |
7b3c959c |
7 | |
9e6b568b |
8 | Readonly::Scalar my $EXPL => q{Prefix builder method names with an underscore}; |
e3f2864c |
9 | sub default_severity { $SEVERITY_MEDIUM } |
ac490c58 |
10 | |
914f300c |
11 | augment applies_to_metaclass => sub { 'Moose::Meta::Role' }; |
12 | |
ac490c58 |
13 | sub violates_metaclass { |
14 | my $self = shift; |
15 | my $meta = shift; |
16 | |
336800ce |
17 | my $classname = $meta->name; |
18 | |
ac490c58 |
19 | my @violations; |
20 | |
501e8f35 |
21 | for my $name ($meta->get_attribute_list) { |
22 | my $attribute = $meta->get_attribute($name); |
914f300c |
23 | my $builder; |
ac490c58 |
24 | |
914f300c |
25 | if (blessed($attribute)) { |
26 | next if !$attribute->has_builder; |
27 | $builder = $attribute->builder; |
28 | } |
29 | else { |
30 | # Roles suck :( |
31 | next if !defined($attribute->{builder}); |
32 | $builder = $attribute->{builder}; |
33 | } |
336800ce |
34 | |
35 | if ($builder !~ /^_/) { |
7b3c959c |
36 | my $type = meta_type($meta); |
37 | my $desc = "Builder method '$builder' of attribute '$attribute' of $type '$classname' is public"; |
336800ce |
38 | push @violations, $self->violation($desc, $EXPL); |
ac490c58 |
39 | } |
40 | } |
41 | |
42 | return @violations; |
43 | } |
44 | |
ac490c58 |
45 | no Moose; |
46 | |
47 | 1; |
48 | |
519dbfa0 |
49 | __END__ |
50 | |
51 | =head1 NAME |
52 | |
53 | Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders |
54 | |
55 | =head1 DESCRIPTION |
56 | |
57 | An attribute's L<Moose/builder> method is used to provide a default value |
58 | for that attribute. Such methods are rarely intended for external use, and |
59 | should not be considered part of that class's public API. Thus we recommend |
60 | that your attribute builder methods' names are prefixed with an underscore |
61 | to mark them private. |
62 | |
c562b9cb |
63 | =head1 WARNING |
64 | |
65 | B<VERY IMPORTANT:> Most L<Perl::Critic> Policies (including all the ones that |
66 | ship with Perl::Critic> use pure static analysis -- they never compile nor |
67 | execute any of the code that they analyze. However, this policy is very |
68 | different. It actually attempts to compile your code and then compares the |
69 | subroutines mentioned in your code to those found in the symbol table. |
70 | Therefore you should B<not> use this Policy on any code that you do not trust, |
71 | or may have undesirable side-effects at compile-time (such as connecting to the |
72 | network or mutating files). |
73 | |
74 | For this Policy to work, all the modules included in your code must be |
75 | installed locally, and must compile without error. |
76 | |
e556eafb |
77 | =head1 AUTHOR |
78 | |
79 | Shawn M Moore, C<sartak@bestpractical.com> |
80 | |
519dbfa0 |
81 | =cut |
82 | |