blib/lib/Perl/Critic/UserProfile.pm

filter: Statementstmt 93.8% 123 / 131 Branchbran 61.5% 16 / 26 Conditioncond 69.2% 27 / 39 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 96.6% 29 / 30 Podpod 100% 8 / 8 totaltotal 83.2% 213 / 256 CC 29 SCAR 17.0
CC
29
Cov
85%
_find_profile_path
21.3
_find_home_dir
20.0
raw_policy_params
6.1
CRAP 5.5
« blib/lib/Perl/Critic/Policy/Variables/ProhibitPerl4PackageNames.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireTestLabels.pm »
1 package Perl::Critic::UserProfile;
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 Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Config::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use Perl::Critic::OptionsProcessor qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Perl::Critic::Utils qw{ $EMPTY policy_long_name policy_short_name };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Perl::Critic::Exception::Configuration::Generic qw{ throw_generic };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Perl::Critic::PolicyConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 40 our $VERSION = '1.150';
19  
20 #-----------------------------------------------------------------------------
21  
22 sub new {
23  
24 444 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 444 my $self = bless {}, $class;
26 444 $self->_init( %args );
27 440 return $self;
28 }
29  
30 #-----------------------------------------------------------------------------
31  
32 sub _init {
33  
34 444 ▶ my ( $self, %args ) = @_;
Subroutine _init: called CC=2 SCAR=7.0
35 # The profile can be defined, undefined, or an empty string.
36 444 ▶ my $profile = $args{-profile} // _find_profile_path();
Condition66%2/3
$args{"-profile"} // _find_profile_path()
l!l&&r!l&&!r
412032
MC/DC50%1/2
$args{"-profile"} // _find_profile_path()
$args{"-profile"} _find_profile_path()
Truth table66%2/3
A // B
A= $args{"-profile"}B= _find_profile_path()
ABresult
1X1
011
000
37 444 $self->_load_profile( $profile );
38 440 $self->_set_options_processor();
39 440 return $self;
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 sub options_processor {
45  
46 2118 ▶ my ($self) = @_;
Subroutine options_processor: called CC=1 SCAR=0.0
Pod documented
47 2118 return $self->{_options_processor};
48 }
49  
50 #-----------------------------------------------------------------------------
51  
52 sub policy_params {
53  
54 12507 ▶ my ( $self, $policy ) = @_;
Subroutine policy_params: called CC=1 SCAR=0.0
Pod documented
55  
56 12507 my $short_name = policy_short_name($policy);
57  
58 12507 return Perl::Critic::PolicyConfig->new(
59 $short_name,
60 $self->raw_policy_params($policy),
61 );
62 }
63  
64 #-----------------------------------------------------------------------------
65  
66 sub raw_policy_params {
67  
68 12519 ▶ my ( $self, $policy ) = @_;
Subroutine raw_policy_params: called CC=6 SCAR=18.2
Pod documented
69 12519 my $profile = $self->{_profile};
70 12519 ▶ my $long_name = ref $policy || policy_long_name( $policy );
Condition33%1/3
ref $policy || policy_long_name($policy)
l!l&&r!l&&!r
0125190
MC/DC0%0/2
ref $policy || policy_long_name($policy)
ref $policy policy_long_name($policy)
Truth table33%1/3
A || B
A= ref $policyB= policy_long_name($policy)
ABresult
1X1
011
000
71 12519 my $short_name = policy_short_name( $long_name );
72  
73 return
74 $profile->{$short_name}
75 || $profile->{$long_name}
76 || $profile->{"-$short_name"}
77 12519 ▶ || $profile->{"-$long_name"}
Condition90%10/11
$profile->{$short_name} || $profile->{$long_name} || $$profile{"-$short_name"} || $$profile{"-$long_name"} || {}
l!l
183910680
$profile->{$short_name} or $profile->{$long_name} or $$profile{"-$short_name"} or $$profile{"-$long_name"}
l!l&&r!l&&!r
1839010680
$profile->{$short_name} or $profile->{$long_name} or $$profile{"-$short_name"}
l!l&&r!l&&!r
161022910680
$profile->{$short_name} or $profile->{$long_name}
l!l&&r!l&&!r
168144210909
MC/DC75%3/4
$profile->{$short_name} || $profile->{$long_name} || $$profile{"-$short_name"} || $$profile{"-$long_name"} || {}
$profile->{$short_name} $profile->{$long_name} $$profile{"-$short_name"} $$profile{"-$long_name"}
Truth table80%4/5
A or B or C or D
A= $profile->{$short_name}B= $profile->{$long_name}C= $$profile{"-$short_name"}D= $$profile{"-$long_name"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
78 || {};
79 }
80  
81 #-----------------------------------------------------------------------------
82  
83 sub policy_is_disabled {
84  
85 11031 ▶ my ( $self, $policy ) = @_;
Subroutine policy_is_disabled: called CC=3 SCAR=11.0
Pod documented
86 11031 my $profile = $self->{_profile};
87 11031 ▶ my $long_name = ref $policy || policy_long_name( $policy );
Condition66%2/3
ref $policy || policy_long_name($policy)
l!l&&r!l&&!r
11019120
MC/DC0%0/2
ref $policy || policy_long_name($policy)
ref $policy policy_long_name($policy)
Truth table66%2/3
A || B
A= ref $policyB= policy_long_name($policy)
ABresult
1X1
011
000
88 11031 my $short_name = policy_short_name( $long_name );
89  
90 return exists $profile->{"-$short_name"}
91 11031 ▶ || exists $profile->{"-$long_name"};
Condition100%3/3
exists $$profile{"-$short_name"} || exists $$profile{"-$long_name"}
l!l&&r!l&&!r
235210794
MC/DC100%2/2
exists $$profile{"-$short_name"} || exists $$profile{"-$long_name"}
exists $$profile{"-$short_name"} exists $$profile{"-$long_name"}
Truth table100%3/3
A || B
A= exists $$profile{"-$short_name"}B= exists $$profile{"-$long_name"}
ABresult
1X1
011
000
92 }
93  
94 #-----------------------------------------------------------------------------
95  
96 sub policy_is_enabled {
97  
98 11031 ▶ my ( $self, $policy ) = @_;
Subroutine policy_is_enabled: called CC=3 SCAR=11.0
Pod documented
99 11031 my $profile = $self->{_profile};
100 11031 ▶ my $long_name = ref $policy || policy_long_name( $policy );
Condition66%2/3
ref $policy || policy_long_name($policy)
l!l&&r!l&&!r
11019120
MC/DC0%0/2
ref $policy || policy_long_name($policy)
ref $policy policy_long_name($policy)
Truth table66%2/3
A || B
A= ref $policyB= policy_long_name($policy)
ABresult
1X1
011
000
101 11031 my $short_name = policy_short_name( $long_name );
102  
103 return exists $profile->{$short_name}
104 11031 ▶ || exists $profile->{$long_name};
Condition100%3/3
exists $profile->{$short_name} || exists $profile->{$long_name}
l!l&&r!l&&!r
16514429424
MC/DC100%2/2
exists $profile->{$short_name} || exists $profile->{$long_name}
exists $profile->{$short_name} exists $profile->{$long_name}
Truth table100%3/3
A || B
A= exists $profile->{$short_name}B= exists $profile->{$long_name}
ABresult
1X1
011
000
105 }
106  
107 #-----------------------------------------------------------------------------
108  
109 sub listed_policies {
110  
111 435 ▶ my ( $self, $policy ) = @_;
Subroutine listed_policies: called CC=2 SCAR=6.9
Pod documented
112 435 my @normalized_policy_names;
113  
114 435 for my $policy_name ( sort keys %{$self->{_profile}} ) {
115 1833 $policy_name =~ s/\A - //xmso; #Chomp leading "-"
116 1833 my $policy_long_name = policy_long_name( $policy_name );
117 1833 push @normalized_policy_names, $policy_long_name;
118 }
119  
120 435 return @normalized_policy_names;
121 }
122  
123 #-----------------------------------------------------------------------------
124  
125 sub source {
126 3505 ▶ my ( $self ) = @_;
Subroutine source: called CC=1 SCAR=0.0
Pod documented
127  
128 3505 return $self->{_source};
129 }
130  
131 sub _set_source {
132 3 ▶ my ( $self, $source ) = @_;
Subroutine _set_source: called CC=1 SCAR=0.0
133  
134 3 $self->{_source} = $source;
135  
136 3 return;
137 }
138  
139 #-----------------------------------------------------------------------------
140 # Begin PRIVATE methods
141  
142 40 Readonly::Hash my %LOADER_FOR => (
143 ARRAY => \&_load_profile_from_array,
144 DEFAULT => \&_load_profile_from_file,
145 HASH => \&_load_profile_from_hash,
146 SCALAR => \&_load_profile_from_string,
147 );
148  
149 sub _load_profile {
150  
151 444 ▶ my ( $self, $profile ) = @_;
Subroutine _load_profile: called CC=3 SCAR=11.0
152  
153 444 ▶ my $ref_type = ref $profile || 'DEFAULT';
Condition100%2/2
ref $profile || 'DEFAULT'
l!l
58386
MC/DC100%1/1
ref $profile || 'DEFAULT'
ref $profile
Truth table100%2/2
A
A= ref $profile
Aresult
11
00
154 444 my $loader = $LOADER_FOR{$ref_type};
155  
156 444 ▶ if (not $loader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loader) 1 443 444
157 1 throw_internal qq{Can't load UserProfile from type "$ref_type"};
158 }
159  
160 443 $self->{_profile} = $loader->($self, $profile);
161 440 return $self;
162 }
163  
164 #-----------------------------------------------------------------------------
165  
166 sub _set_options_processor {
167  
168 440 ▶ my ($self) = @_;
Subroutine _set_options_processor: called CC=2 SCAR=6.9
169 440 my $profile = $self->{_profile};
170 440 ▶ my $defaults = delete $profile->{__defaults__} || {};
Condition100%2/2
delete $profile->{'__defaults__'} || {}
l!l
4436
MC/DC100%1/1
delete $profile->{'__defaults__'} || {}
delete $profile->{'__defaults__'}
Truth table100%2/2
A
A= delete $profile->{'__defaults__'}
Aresult
11
00
171 $self->{_options_processor} =
172 440 Perl::Critic::OptionsProcessor->new( %{ $defaults } );
173 440 return $self;
174 }
175  
176 #-----------------------------------------------------------------------------
177  
178 sub _load_profile_from_file {
179 386 ▶ my ( $self, $file ) = @_;
Subroutine _load_profile_from_file: called CC=5 SCAR=16.1
180  
181 # Handle special cases.
182 386 ▶ return {} if not defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 32 354 386
183 354 ▶ return {} if $file eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $Perl::Critic::UserProfile::EMPTY 3 351 354
184 351 ▶ return {} if $file eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "NONE" 348 3 351
185  
186 3 $self->_set_source( $file );
187  
188 3 my $profile = Config::Tiny->read( $file );
189 3 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 1 2 3
190 1 my $errstr = Config::Tiny::errstr();
191 1 throw_generic
192 message => qq{Could not parse profile "$file": $errstr},
193 source => $file;
194 }
195  
196 2 _fix_defaults_key( $profile );
197  
198 2 return $profile;
199 }
200  
201 #-----------------------------------------------------------------------------
202  
203 sub _load_profile_from_array {
204 1 ▶ my ( $self, $array_ref ) = @_;
Subroutine _load_profile_from_array: called CC=2 SCAR=7.1
205 1 my $joined = join qq{\n}, @{ $array_ref };
206 1 my $profile = Config::Tiny->read_string( $joined );
207  
208 1 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 0 1 1
209 0 throw_generic 'Profile error: ' . Config::Tiny::errstr();
210 }
211  
212 1 _fix_defaults_key( $profile );
213  
214 1 return $profile;
215 }
216  
217 #-----------------------------------------------------------------------------
218  
219 sub _load_profile_from_string {
220 5 ▶ my ( $self, $string ) = @_;
Subroutine _load_profile_from_string: called CC=2 SCAR=6.9
221 5 my $profile = Config::Tiny->read_string( ${ $string } );
222  
223 5 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 2 3 5
224 2 throw_generic 'Profile error: ' . Config::Tiny::errstr();
225 }
226  
227 3 _fix_defaults_key( $profile );
228  
229 3 return $profile;
230 }
231  
232 #-----------------------------------------------------------------------------
233  
234 sub _load_profile_from_hash {
235 51 ▶ my ( $self, $hash_ref ) = @_;
Subroutine _load_profile_from_hash: called CC=1 SCAR=0.0
236 51 return $hash_ref;
237 }
238  
239 #-----------------------------------------------------------------------------
240  
241 sub _find_profile_path {
242  
243 #Check explicit environment setting
244 1 ▶ return $ENV{PERLCRITIC} if exists $ENV{PERLCRITIC};
Subroutine _find_profile_path: called CC=5 SCAR=30.6
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"PERLCRITIC"} 1 0 1
245  
246 #Define default filename
247 0 my $rc_file = '.perlcriticrc';
248  
249 #Check current directory
250 0 ▶ return $rc_file if -f $rc_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $rc_file 0 0 0
251  
252 #Check home directory
253 0 ▶ if ( my $home_dir = _find_home_dir() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $home_dir = _find_home_dir()) 0 0 0
254 0 my $path = File::Spec->catfile( $home_dir, $rc_file );
255 0 ▶ return $path if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
256 }
257  
258 #No profile defined
259 0 return;
260 }
261  
262 #-----------------------------------------------------------------------------
263  
264 sub _find_home_dir {
265 # This logic is taken from File::HomeDir::Tiny.
266 return
267 ($^O eq 'MSWin32') && ("$]" < 5.016) ## no critic ( Variables::ProhibitPunctuationVars ValuesAndExpressions::ProhibitMagicNumbers ValuesAndExpressions::ProhibitMismatchedOperators )
268 ? ($ENV{HOME} || $ENV{USERPROFILE})
269 0 ▶ : (<~>)[0];
Subroutine _find_home_dir: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' && "$]" < 5.016 ? : 0 0 0
Condition0%0/6
$^O eq "MSWin32" and "$]" < "5.016"
!ll&&!rl&&r
000
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" and "$]" < "5.016"
$^O eq "MSWin32" "$]" < "5.016"
MC/DC0%0/2
$ENV{"HOME"} or $ENV{"USERPROFILE"}
$ENV{"HOME"} $ENV{"USERPROFILE"}
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= "$]" < "5.016"
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}
ABresult
1X1
011
000
270 }
271  
272 #-----------------------------------------------------------------------------
273  
274 # !$%@$%^ Config::Tiny uses a completely non-descriptive name for global
275 # values.
276 sub _fix_defaults_key {
277 6 ▶ my ( $profile ) = @_;
Subroutine _fix_defaults_key: called CC=2 SCAR=6.9
278  
279 6 my $defaults = delete $profile->{_};
280 6 ▶ if ($defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults) 3 3 6
281 3 $profile->{__defaults__} = $defaults;
282 }
283  
284 6 return;
285 }
286  
287 40 1;
288  
289 __END__
290  
291 #-----------------------------------------------------------------------------
292  
293 =pod
294  
295 =for stopwords UserProfile
296  
297 =head1 NAME
298  
299 Perl::Critic::UserProfile - The contents of the user's profile, often F<.perlcriticrc>.
300  
301  
302 =head1 DESCRIPTION
303  
304 This is a helper class that encapsulates the contents of the user's
305 profile, which is usually stored in a F<.perlcriticrc> file. There are
306 no user-serviceable parts here.
307  
308  
309 =head1 INTERFACE SUPPORT
310  
311 This is considered to be a non-public class. Its interface is subject
312 to change without notice.
313  
314  
315 =head1 CONSTRUCTOR
316  
317 =over
318  
319 =item C<< new( -profile => $p ) >>
320  
321 B<-profile> is the path to the user's profile. If -profile is not
322 defined, then it looks for the profile at F<./.perlcriticrc> and then
323 F<$HOME/.perlcriticrc>. If neither of those files exists, then the
324 UserProfile is created with default values.
325  
326 This object does not take into account any command-line overrides;
327 L<Perl::Critic::Config|Perl::Critic::Config> does that.
328  
329  
330 =back
331  
332  
333 =head1 METHODS
334  
335 =over
336  
337 =item C< options_processor() >
338  
339 Returns the
340 L<Perl::Critic::OptionsProcessor|Perl::Critic::OptionsProcessor>
341 object for this UserProfile.
342  
343  
344 =item C< policy_is_disabled( $policy ) >
345  
346 Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
347 object or the name of one, returns true if the user has disabled that
348 policy in their profile.
349  
350  
351 =item C< policy_is_enabled( $policy ) >
352  
353 Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
354 object or the name of one, returns true if the user has explicitly
355 enabled that policy in their user profile.
356  
357  
358 =item C< policy_params( $policy ) >
359  
360 Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
361 object or the name of one, returns a
362 L<Perl::Critic::PolicyConfig|Perl::Critic::PolicyConfig> for the
363 user's configuration parameters for that policy.
364  
365  
366 =item C< raw_policy_params( $policy ) >
367  
368 Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
369 object or the name of one, returns a reference to a hash of the user's
370 configuration parameters for that policy.
371  
372  
373 =item C< listed_policies() >
374  
375 Returns a list of the names of all the Policies that are mentioned in
376 the profile. The Policy names will be fully qualified (e.g.
377 Perl::Critic::Foo).
378  
379  
380 =item C< source() >
381  
382 The place where the profile information came from, if available.
383 Usually the path to a F<.perlcriticrc>.
384  
385  
386 =back
387  
388  
389 =head1 SEE ALSO
390  
391 L<Perl::Critic::Config|Perl::Critic::Config>,
392 L<Perl::Critic::OptionsProcessor|Perl::Critic::OptionsProcessor>
393  
394  
395 =head1 AUTHOR
396  
397 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
398  
399  
400 =head1 COPYRIGHT
401  
402 Copyright (c) 2005-2023 Imaginative Software Systems.
403  
404 This program is free software; you can redistribute it and/or modify
405 it under the same terms as Perl itself. The full text of this license
406 can be found in the LICENSE file included with this module.
407  
408 =cut
409  
410 # Local Variables:
411 # mode: cperl
412 # cperl-indent-level: 4
413 # fill-column: 78
414 # indent-tabs-mode: nil
415 # c-indentation-style: bsd
416 # End:
417 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Variables/ProhibitPerl4PackageNames.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireTestLabels.pm »