1 package Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders;
3 extends 'Perl::Critic::Policy::DynamicMoose';
5 use Perl::Critic::Utils ':severities';
6 use Perl::Critic::Utils::Moose 'meta_type';
8 Readonly::Scalar my $EXPL => q{Prefix builder method names with an underscore};
9 sub default_severity { $SEVERITY_MEDIUM }
11 augment applies_to_metaclass => sub { 'Moose::Meta::Role' };
13 sub violates_metaclass {
17 my $classname = $meta->name;
21 my $attributes = $meta->get_attribute_map;
22 for my $name (keys %$attributes) {
23 my $attribute = $attributes->{$name};
26 if (blessed($attribute)) {
27 next if !$attribute->has_builder;
28 $builder = $attribute->builder;
32 next if !defined($attribute->{builder});
33 $builder = $attribute->{builder};
36 if ($builder !~ /^_/) {
37 my $type = meta_type($meta);
38 my $desc = "Builder method '$builder' of attribute '$attribute' of $type '$classname' is public";
39 push @violations, $self->violation($desc, $EXPL);
54 Perl::Critic::Policy::DynamicMoose::ProhibitPublicBuilders
58 An attribute's L<Moose/builder> method is used to provide a default value
59 for that attribute. Such methods are rarely intended for external use, and
60 should not be considered part of that class's public API. Thus we recommend
61 that your attribute builder methods' names are prefixed with an underscore
66 B<VERY IMPORTANT:> Most L<Perl::Critic> Policies (including all the ones that
67 ship with Perl::Critic> use pure static analysis -- they never compile nor
68 execute any of the code that they analyze. However, this policy is very
69 different. It actually attempts to compile your code and then compares the
70 subroutines mentioned in your code to those found in the symbol table.
71 Therefore you should B<not> use this Policy on any code that you do not trust,
72 or may have undesirable side-effects at compile-time (such as connecting to the
73 network or mutating files).
75 For this Policy to work, all the modules included in your code must be
76 installed locally, and must compile without error.
80 Shawn M Moore, C<sartak@bestpractical.com>