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