Commit | Line | Data |
84a1bb62 |
1 | package Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders; |
ac490c58 |
2 | use Moose; |
84a1bb62 |
3 | extends 'Perl::Critic::Policy::DynamicMoose'; |
ac490c58 |
4 | |
9e6b568b |
5 | Readonly::Scalar my $EXPL => q{Prefix builder method names with an underscore}; |
ac490c58 |
6 | |
914f300c |
7 | augment applies_to_metaclass => sub { 'Moose::Meta::Role' }; |
8 | |
ac490c58 |
9 | sub violates_metaclass { |
10 | my $self = shift; |
11 | my $meta = shift; |
12 | |
336800ce |
13 | my $classname = $meta->name; |
14 | |
ac490c58 |
15 | my @violations; |
16 | |
17 | my $attributes = $meta->get_attribute_map; |
18 | for my $name (keys %$attributes) { |
19 | my $attribute = $attributes->{$name}; |
914f300c |
20 | my $builder; |
ac490c58 |
21 | |
914f300c |
22 | if (blessed($attribute)) { |
23 | next if !$attribute->has_builder; |
24 | $builder = $attribute->builder; |
25 | } |
26 | else { |
27 | # Roles suck :( |
28 | next if !defined($attribute->{builder}); |
29 | $builder = $attribute->{builder}; |
30 | } |
336800ce |
31 | |
32 | if ($builder !~ /^_/) { |
33 | my $desc = "Builder method '$builder' of attribute '$attribute' of class '$classname' is public"; |
34 | push @violations, $self->violation($desc, $EXPL); |
ac490c58 |
35 | } |
36 | } |
37 | |
38 | return @violations; |
39 | } |
40 | |
ac490c58 |
41 | no Moose; |
42 | |
43 | 1; |
44 | |