lib/Perl/Critic/UserProfile.pm

filter: Statementstmt 94.0% 126 / 134 Branchbran 61.5% 16 / 26 Conditioncond 69.2% 27 / 39 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 96.7% 30 / 31 Podpod 100% 8 / 8 totaltotal 83.4% 217 / 260 CC 29 SCAR 15.6
CC
29
Cov
85%
_find_profile_path
21.3
_find_home_dir
10.8
raw_policy_params
6.1
CRAP 4.8
« lib/Perl/Critic/Policy/Modules/ProhibitAutomaticExportation.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.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.156';
19  
20 #-----------------------------------------------------------------------------
21  
22 444 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23  
24 444 my ( $class, %args ) = @_;
25 444 my $self = bless {}, $class;
26 444 $self->_init( %args );
27 440 return $self;
28 }
29  
30 #-----------------------------------------------------------------------------
31  
32 444 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
33  
34 444 my ( $self, %args ) = @_;
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 2118 ▶sub options_processor {
Subroutine options_processor: called CC=1 SCAR=0.0
Pod documented
45  
46 2118 my ($self) = @_;
47 2118 return $self->{_options_processor};
48 }
49  
50 #-----------------------------------------------------------------------------
51  
52 12507 ▶sub policy_params {
Subroutine policy_params: called CC=1 SCAR=0.0
Pod documented
53  
54 12507 my ( $self, $policy ) = @_;
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 12519 ▶sub raw_policy_params {
Subroutine raw_policy_params: called CC=6 SCAR=18.2
Pod documented
67  
68 12519 my ( $self, $policy ) = @_;
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 11031 ▶sub policy_is_disabled {
Subroutine policy_is_disabled: called CC=3 SCAR=11.0
Pod documented
84  
85 11031 my ( $self, $policy ) = @_;
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 11031 ▶sub policy_is_enabled {
Subroutine policy_is_enabled: called CC=3 SCAR=11.0
Pod documented
97  
98 11031 my ( $self, $policy ) = @_;
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 435 ▶sub listed_policies {
Subroutine listed_policies: called CC=2 SCAR=6.9
Pod documented
110  
111 435 my ( $self, $policy ) = @_;
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 3505 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
126 3505 my ( $self ) = @_;
127  
128 3505 return $self->{_source};
129 }
130  
131 3 ▶sub _set_source {
Subroutine _set_source: called CC=1 SCAR=0.0
132 3 my ( $self, $source ) = @_;
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 444 ▶sub _load_profile {
Subroutine _load_profile: called CC=3 SCAR=11.0
150  
151 444 my ( $self, $profile ) = @_;
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 440 ▶sub _set_options_processor {
Subroutine _set_options_processor: called CC=2 SCAR=6.9
167  
168 440 my ($self) = @_;
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 386 ▶sub _load_profile_from_file {
Subroutine _load_profile_from_file: called CC=5 SCAR=16.1
179 386 my ( $self, $file ) = @_;
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 1 ▶sub _load_profile_from_array {
Subroutine _load_profile_from_array: called CC=2 SCAR=7.1
204 1 my ( $self, $array_ref ) = @_;
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 5 ▶sub _load_profile_from_string {
Subroutine _load_profile_from_string: called CC=2 SCAR=6.9
220 5 my ( $self, $string ) = @_;
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 51 ▶sub _load_profile_from_hash {
Subroutine _load_profile_from_hash: called CC=1 SCAR=0.0
235 51 my ( $self, $hash_ref ) = @_;
236 51 return $hash_ref;
237 }
238  
239 #-----------------------------------------------------------------------------
240  
241 1 ▶sub _find_profile_path {
Subroutine _find_profile_path: called CC=5 SCAR=30.6
242  
243 #Check explicit environment setting
244 1 ▶ return $ENV{PERLCRITIC} if exists $ENV{PERLCRITIC};
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 0 ▶sub _find_home_dir {
Subroutine _find_home_dir: not called CC=4 SCAR=23.7
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 BEGIN: called CC=1 SCAR=3.5
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 6 ▶sub _fix_defaults_key {
Subroutine _fix_defaults_key: called CC=2 SCAR=6.9
277 6 my ( $profile ) = @_;
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__
« lib/Perl/Critic/Policy/Modules/ProhibitAutomaticExportation.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.pm »