lib/Perl/Critic/PolicyFactory.pm

filter: Statementstmt 92.5% 148 / 160 Branchbran 73.8% 31 / 42 Conditioncond 67.7% 21 / 31 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 30 / 30 Podpod 100% 4 / 4 totaltotal 83.5% 238 / 285 CC 35 SCAR 16.5
CC
35
Cov
86%
_init
11.4
import
11.0
create_all_policies
6.1
CRAP 5.2
« lib/Perl/Critic/Policy/TestingAndDebugging/RequireTestLabels.pm ↑ Summary lib/Perl/Critic/Policy/NamingConventions/ProhibitAmbiguousNames.pm »
1 package Perl::Critic::PolicyFactory;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use File::Spec::Unix qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use List::SomeUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use Perl::Critic::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
13 :characters
14 $POLICY_NAMESPACE
15 :data_conversion
16 policy_long_name
17 policy_short_name
18 :internal_lookup
19 40 };
20 40 ▶use Perl::Critic::PolicyConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use Perl::Critic::Exception::AggregateConfiguration;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use Perl::Critic::Exception::Configuration;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Perl::Critic::Exception::Fatal::Generic qw{ throw_generic };
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
Subroutine BEGIN: called CC=1 SCAR=0.0
25 use Perl::Critic::Exception::Fatal::PolicyDefinition
26 40 ▶ qw{ throw_policy_definition };
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Perl::Critic::Exception::Configuration::NonExistentPolicy qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use Perl::Critic::Utils::Constants qw{ :profile_strictness };
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 40 ▶use Exception::Class; # this must come after "use P::C::Exception::*"
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 40 our $VERSION = '1.156';
33  
34 #-----------------------------------------------------------------------------
35  
36 # Globals. Ick!
37 40 my @site_policy_names;
38  
39 #-----------------------------------------------------------------------------
40  
41 # Blech!!! This is ug-lee. Belongs in the constructor. And it shouldn't be
42 # called "test" mode.
43 134 ▶sub import {
Subroutine import: called CC=8 SCAR=23.9
44  
45 134 my ( undef, %args ) = @_;
46 134 my $test_mode = $args{-test};
47 134 my $extra_test_policies = $args{'-extra-test-policies'};
48  
49 134 ▶ if ( not @site_policy_names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@site_policy_names) 40 94 134
50 40 my $eval_worked = eval {
51 40 require Module::Pluggable;
52 40 Module::Pluggable->import(search_path => $POLICY_NAMESPACE,
53 require => 1, inner => 0);
54 40 @site_policy_names = plugins(); #Exported by Module::Pluggable
55 40 1;
56 };
57  
58 40 ▶ if (not $eval_worked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_worked) 0 40 40
59 0 ▶ if ( $EVAL_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Critic::PolicyFactory::EVAL_ERROR) 0 0 0
60 0 throw_generic
61 qq<Can't load Policies from namespace "$POLICY_NAMESPACE": $EVAL_ERROR>;
62 }
63  
64 throw_generic
65 0 qq<Can't load Policies from namespace "$POLICY_NAMESPACE" for an unknown reason.>;
66 }
67  
68 40 ▶ if ( not @site_policy_names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@site_policy_names) 0 40 40
69 0 throw_generic
70 qq<No Policies found in namespace "$POLICY_NAMESPACE".>;
71 }
72 }
73  
74 # In test mode, only load native policies, not third-party ones. So this
75 # filters out any policy that was loaded from within a directory called
76 # "blib". During the usual "./Build test" process this works fine,
77 # but it doesn't work if you are using prove to test against the code
78 # directly in the lib/ directory.
79  
80 134 ▶ if ( $test_mode && any {m/\b blib \b/xms} @INC ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($test_mode and &any(sub { /\b blib \b/msx; } , @INC)) 51 83 134
Condition66%2/3
$test_mode and &any(sub { /\b blib \b/msx; } , @INC)
!ll&&!rl&&r
83051
MC/DC50%1/2
$test_mode and &any(sub { /\b blib \b/msx; } , @INC)
$test_mode &any(sub { /\b blib \b/msx; } , @INC)
Truth table66%2/3
A and B
A= $test_modeB= &any(sub { /\b blib \b/msx; } , @INC)
ABresult
0X0
100
111
81 51 @site_policy_names = _modules_from_blib( @site_policy_names );
82  
83 51 ▶ if ($extra_test_policies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_test_policies) 0 51 51
84 my @extra_policy_full_names =
85 0 map { "${POLICY_NAMESPACE}::$_" } @{$extra_test_policies};
86  
87 0 push @site_policy_names, @extra_policy_full_names;
88 }
89 }
90  
91 134 return 1;
92 }
93  
94 #-----------------------------------------------------------------------------
95 # Some static helper subs
96  
97 51 ▶sub _modules_from_blib {
Subroutine _modules_from_blib: called CC=1 SCAR=0.0
98 51 my (@modules) = @_;
99 51 return grep { _was_loaded_from_blib( _module2path($_) ) } @modules;
100 }
101  
102 7395 ▶sub _module2path {
Subroutine _module2path: called CC=2 SCAR=7.2
103 7395 ▶ my $module = shift || return;
Condition50%1/2
shift() || (return)
l!l
73950
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
104 7395 return File::Spec::Unix->catdir(split m/::/xms, $module) . '.pm';
105 }
106  
107 7395 ▶sub _was_loaded_from_blib {
Subroutine _was_loaded_from_blib: called CC=3 SCAR=12.5
108 7395 ▶ my $path = shift || return;
Condition50%1/2
shift() || (return)
l!l
73950
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
109 7395 my $full_path = $INC{$path};
110 7395 ▶ return $full_path && $full_path =~ m/ (?: \A | \b b ) lib \b /xms;
Condition33%1/3
$full_path && $full_path =~ / (?: \A | \b b ) lib \b /msx
!ll&&!rl&&r
007395
MC/DC0%0/2
$full_path && $full_path =~ / (?: \A | \b b ) lib \b /msx
$full_path $full_path =~ / (?: \A | \b b ) lib \b /msx
Truth table33%1/3
A && B
A= $full_pathB= $full_path =~ / (?: \A | \b b ) lib \b /msx
ABresult
0X0
100
111
111 }
112  
113 #-----------------------------------------------------------------------------
114  
115 435 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
116  
117 435 my ( $class, %args ) = @_;
118 435 my $self = bless {}, $class;
119 435 $self->_init( %args );
120 435 return $self;
121 }
122  
123 #-----------------------------------------------------------------------------
124  
125 435 ▶sub _init {
Subroutine _init: called CC=11 SCAR=24.3
126  
127 435 my ($self, %args) = @_;
128  
129 435 my $profile = $args{-profile};
130 435 ▶ $self->{_profile} = $profile
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_profile"} = $profile or throw_internal("The -profile argument is required") 435 0 435
131 or throw_internal q{The -profile argument is required};
132  
133 435 my $incoming_errors = $args{-errors};
134 435 my $profile_strictness = $args{'-profile-strictness'};
135 435 ▶ $profile_strictness ||= $PROFILE_STRICTNESS_DEFAULT;
Condition66%2/3
$profile_strictness ||= $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_DEFAULT
l!l&&r!l&&!r
425100
MC/DC0%0/2
$profile_strictness ||= $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_DEFAULT
$profile_strictness $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_DEFAULT
Truth table66%2/3
A ||= B
A= $profile_strictnessB= $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_DEFAULT
ABresult
1X1
011
000
136 435 $self->{_profile_strictness} = $profile_strictness;
137  
138 435 ▶ if ( $profile_strictness ne $PROFILE_STRICTNESS_QUIET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_strictness ne $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_QUIET) 435 0 435
139 435 my $errors;
140  
141 # If we're supposed to be strict or problems have already been found...
142 435 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_strictness eq $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_FATAL or $incoming_errors and @{$incoming_errors->exceptions;}) 147 288 435
Condition100%6/6
$profile_strictness eq $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_FATAL or $incoming_errors and @{$incoming_errors->exceptions;}
l!l&&r!l&&!r
1461288
$incoming_errors and @{$incoming_errors->exceptions;}
!ll&&!rl&&r
102781
MC/DC100%3/3
$profile_strictness eq $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_FATAL or $incoming_errors and @{$incoming_errors->exceptions;}
$profile_strictness eq $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_FATAL $incoming_errors @{$incoming_errors->exceptions;}
Truth table100%4/4
A or B and C
A= $profile_strictness eq $Perl::Critic::PolicyFactory::PROFILE_STRICTNESS_FATALB= $incoming_errorsC= @{$incoming_errors->exceptions;}
ABCresult
1XX1
0111
00X0
0100
143 $profile_strictness eq $PROFILE_STRICTNESS_FATAL
144 279 or ( $incoming_errors and @{ $incoming_errors->exceptions() } )
145 ) {
146 147 ▶ $errors =
Branch: 1 branch
BranchTrueFalseTotal
$incoming_errors ? : 2 145 147
147 $incoming_errors
148 ? $incoming_errors
149 : Perl::Critic::Exception::AggregateConfiguration->new();
150 }
151  
152 435 $self->_validate_policies_in_profile( $errors );
153  
154 435 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $incoming_errors and $errors and $errors->has_exceptions) 0 435 435
Condition83%5/6
not $incoming_errors and $errors and $errors->has_exceptions
!ll&&!rl&&r
2901450
not $incoming_errors and $errors
!ll&&!rl&&r
28010145
MC/DC0%0/3
not $incoming_errors and $errors and $errors->has_exceptions
not $incoming_errors $errors $errors->has_exceptions
Truth table75%3/4
not(A) and B and C
A= not $incoming_errorsB= $errorsC= $errors->has_exceptions
ABCresult
0XX0
10X0
1100
1111
155 not $incoming_errors
156 and $errors
157 and $errors->has_exceptions()
158 ) {
159 0 $errors->rethrow();
160 }
161 }
162  
163 435 return $self;
164 }
165  
166 #-----------------------------------------------------------------------------
167  
168 12776 ▶sub create_policy {
Subroutine create_policy: called CC=4 SCAR=13.9
Pod documented
169  
170 12776 my ($self, %args ) = @_;
171  
172 my $policy_name = $args{-name}
173 12776 ▶ or throw_internal q{The -name argument is required};
Branch: 1 branch
BranchTrueFalseTotal
my $policy_name = $args{"-name"} or throw_internal("The -name argument is required") 12775 1 12776
174  
175 # Normalize policy name to a fully-qualified package name
176 12775 $policy_name = policy_long_name( $policy_name );
177 12775 my $policy_short_name = policy_short_name( $policy_name );
178  
179  
180 # Get the policy parameters from the user profile if they were
181 # not given to us directly. If none exist, use an empty hash.
182 12775 my $profile = $self->_profile();
183 12775 my $policy_config;
184 12775 ▶ if ( $args{-params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-params'}) { } 268 12507 12775
185 $policy_config =
186 Perl::Critic::PolicyConfig->new(
187 $policy_short_name, $args{-params}
188 268 );
189 }
190 else {
191 12507 $policy_config = $profile->policy_params($policy_name);
192 12507 ▶ $policy_config ||=
Condition33%1/3
$policy_config ||= "Perl::Critic::PolicyConfig"->new($policy_short_name)
l!l&&r!l&&!r
1250700
MC/DC0%0/2
$policy_config ||= "Perl::Critic::PolicyConfig"->new($policy_short_name)
$policy_config "Perl::Critic::PolicyConfig"->new($policy_short_name)
Truth table33%1/3
A ||= B
A= $policy_configB= "Perl::Critic::PolicyConfig"->new($policy_short_name)
ABresult
1X1
011
000
193 Perl::Critic::PolicyConfig->new( $policy_short_name );
194 }
195  
196 # Pull out base parameters.
197 12775 return $self->_instantiate_policy( $policy_name, $policy_config );
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 83 ▶sub create_all_policies {
Subroutine create_all_policies: called CC=6 SCAR=18.1
Pod documented
203  
204 83 my ( $self, $incoming_errors ) = @_;
205  
206 83 ▶ my $errors =
Branch: 1 branch
BranchTrueFalseTotal
$incoming_errors ? : 82 1 83
207 $incoming_errors
208 ? $incoming_errors
209 : Perl::Critic::Exception::AggregateConfiguration->new();
210 83 my @policies;
211  
212 83 foreach my $name ( site_policy_names() ) {
213 12035 my $policy = eval { $self->create_policy( -name => $name ) };
214  
215 12035 $errors->add_exception_or_rethrow( $EVAL_ERROR );
216  
217 12035 ▶ if ( $policy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy) 12034 1 12035
218 12034 push @policies, $policy;
219 }
220 }
221  
222 83 ▶ if ( not $incoming_errors and $errors->has_exceptions() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $incoming_errors and $errors->has_exceptions) 0 83 83
Condition66%2/3
not $incoming_errors and $errors->has_exceptions
!ll&&!rl&&r
8210
MC/DC0%0/2
not $incoming_errors and $errors->has_exceptions
not $incoming_errors $errors->has_exceptions
Truth table66%2/3
not(A) and B
A= not $incoming_errorsB= $errors->has_exceptions
ABresult
0X0
100
111
223 0 $errors->rethrow();
224 }
225  
226 83 return @policies;
227 }
228  
229 #-----------------------------------------------------------------------------
230  
231 561 ▶sub site_policy_names {
Subroutine site_policy_names: called CC=1 SCAR=0.0
Pod documented
232 561 my @sorted_policy_names = sort @site_policy_names;
233 561 return @sorted_policy_names;
234 }
235  
236 #-----------------------------------------------------------------------------
237  
238 13210 ▶sub _profile {
Subroutine _profile: called CC=1 SCAR=0.0
239 13210 my ($self) = @_;
240  
241 13210 return $self->{_profile};
242 }
243  
244 #-----------------------------------------------------------------------------
245  
246 # This two-phase initialization is caused by the historical lack of a
247 # requirement for Policies to invoke their super-constructor.
248 12775 ▶sub _instantiate_policy {
Subroutine _instantiate_policy: called CC=1 SCAR=0.0
249 12775 my ($self, $policy_name, $policy_config) = @_;
250  
251 12775 $policy_config->set_profile_strictness( $self->{_profile_strictness} );
252  
253 12775 my $policy = eval { $policy_name->new( %{$policy_config} ) };
254 12775 _handle_policy_instantiation_exception(
255 $policy_name,
256 $policy, # Note: being used as a boolean here.
257 $EVAL_ERROR,
258 );
259  
260 12627 $policy->__set_config( $policy_config );
261  
262 12627 my $eval_worked = eval { $policy->__set_base_parameters(); 1; };
263 12627 _handle_policy_instantiation_exception(
264 $policy_name, $eval_worked, $EVAL_ERROR,
265 );
266  
267 12626 return $policy;
268 }
269  
270 25402 ▶sub _handle_policy_instantiation_exception {
Subroutine _handle_policy_instantiation_exception: called CC=4 SCAR=14.0
271 25402 my ($policy_name, $eval_worked, $eval_error) = @_;
272  
273 25402 ▶ if (not $eval_worked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_worked) 149 25253 25402
274 149 ▶ if ($eval_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eval_error) 149 0 149
275 149 my $exception = Exception::Class->caught();
276  
277 149 ▶ if (ref $exception) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exception) 147 2 149
278 147 $exception->rethrow();
279 }
280  
281 throw_policy_definition
282 2 qq<Unable to create policy "$policy_name": $eval_error>;
283 }
284  
285 throw_policy_definition
286 0 qq<Unable to create policy "$policy_name" for an unknown reason.>;
287 }
288  
289 25253 return;
290 }
291  
292 #-----------------------------------------------------------------------------
293  
294 435 ▶sub _validate_policies_in_profile {
Subroutine _validate_policies_in_profile: called CC=4 SCAR=14.0
295 435 my ($self, $errors) = @_;
296  
297 435 my $profile = $self->_profile();
298 435 my %known_policies = hashify( $self->site_policy_names() );
299  
300 435 for my $policy_name ( $profile->listed_policies() ) {
301 1833 ▶ if ( not exists $known_policies{$policy_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $known_policies{$policy_name}) 2 1831 1833
302 2 my $message = qq{Policy "$policy_name" is not installed.};
303  
304 2 ▶ if ( $errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) { } 0 2 2
305 0 $errors->add_exception(
306 Perl::Critic::Exception::Configuration::NonExistentPolicy->new(
307 policy => $policy_name,
308 )
309 );
310 }
311 else {
312 2 warn qq{$message\n};
313 }
314 }
315 }
316  
317 435 return;
318 }
319  
320 #-----------------------------------------------------------------------------
321  
322 40 1;
323  
324 __END__
« lib/Perl/Critic/Policy/TestingAndDebugging/RequireTestLabels.pm ↑ Summary lib/Perl/Critic/Policy/NamingConventions/ProhibitAmbiguousNames.pm »