lib/Perl/ToPerl6/UserProfile.pm

filter: Statementstmt 90.6% 126 / 139 Branchbran 56.2% 18 / 32 Conditioncond 83.3% 25 / 30 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 96.7% 30 / 31 Podpod 100% 8 / 8 totaltotal 84.3% 216 / 256 CC 30 SCAR 18.2
CC
30
Cov
84%
_find_home_dir
30.0
_find_profile_path
17.8
raw_transformer_params
6.1
CRAP 6.2
« lib/Perl/ToPerl6/Transformer/Variables/ReplaceNegativeIndex.pm ↑ Summary lib/Perl/ToPerl6/Exception/AggregateConfiguration.pm »
1 package Perl::ToPerl6::UserProfile;
2  
3 16 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 ▶use Config::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use Perl::ToPerl6::OptionsProcessor qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Perl::ToPerl6::Utils qw{ :characters transformer_long_name transformer_short_name };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Perl::ToPerl6::Exception::Fatal::Internal qw{ throw_internal };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Perl::ToPerl6::Exception::Configuration::Generic qw{ throw_generic };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use Perl::ToPerl6::TransformerConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #-----------------------------------------------------------------------------
20  
21 128 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22  
23 128 my ( $class, %args ) = @_;
24 128 my $self = bless {}, $class;
25 128 $self->_init( %args );
26 124 return $self;
27 }
28  
29 #-----------------------------------------------------------------------------
30  
31 128 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
32  
33 128 my ( $self, %args ) = @_;
34 # The profile can be defined, undefined, or an empty string.
35 128 ▶ my $profile = defined $args{-profile} ? $args{-profile} : _find_profile_path();
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-profile'} ? : 103 25 128
36 128 $self->_load_profile( $profile );
37 124 $self->_set_options_processor();
38 124 return $self;
39 }
40  
41 #-----------------------------------------------------------------------------
42  
43 616 ▶sub options_processor {
Subroutine options_processor: called CC=1 SCAR=0.0
Pod documented
44  
45 616 my ($self) = @_;
46 616 return $self->{_options_processor};
47 }
48  
49 #-----------------------------------------------------------------------------
50  
51 1294 ▶sub transformer_params {
Subroutine transformer_params: called CC=1 SCAR=0.0
Pod documented
52  
53 1294 my ( $self, $transformer ) = @_;
54  
55 1294 my $short_name = transformer_short_name($transformer);
56  
57 1294 return Perl::ToPerl6::TransformerConfig->new(
58 $short_name,
59 $self->raw_transformer_params($transformer),
60 );
61 }
62  
63 #-----------------------------------------------------------------------------
64  
65 1305 ▶sub raw_transformer_params {
Subroutine raw_transformer_params: called CC=6 SCAR=18.2
Pod documented
66  
67 1305 my ( $self, $transformer ) = @_;
68 1305 my $profile = $self->{_profile};
69 1305 ▶ my $long_name = ref $transformer || transformer_long_name( $transformer );
Condition33%1/3
ref $transformer || transformer_long_name($transformer)
l!l&&r!l&&!r
013050
MC/DC0%0/2
ref $transformer || transformer_long_name($transformer)
ref $transformer transformer_long_name($transformer)
Truth table33%1/3
A || B
A= ref $transformerB= transformer_long_name($transformer)
ABresult
1X1
011
000
70 1305 my $short_name = transformer_short_name( $long_name );
71  
72 return
73 $profile->{$short_name}
74 || $profile->{$long_name}
75 || $profile->{"-$short_name"}
76 1305 ▶ || $profile->{"-$long_name"}
Condition90%10/11
$profile->{$short_name} || $profile->{$long_name} || $$profile{"-$short_name"} || $$profile{"-$long_name"} || {}
l!l
581247
$profile->{$short_name} or $profile->{$long_name} or $$profile{"-$short_name"} or $$profile{"-$long_name"}
l!l&&r!l&&!r
5801247
$profile->{$short_name} or $profile->{$long_name} or $$profile{"-$short_name"}
l!l&&r!l&&!r
5441247
$profile->{$short_name} or $profile->{$long_name}
l!l&&r!l&&!r
5221251
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
77 || {};
78 }
79  
80 #-----------------------------------------------------------------------------
81  
82 961 ▶sub transformer_is_disabled {
Subroutine transformer_is_disabled: called CC=3 SCAR=11.0
Pod documented
83  
84 961 my ( $self, $transformer ) = @_;
85 961 my $profile = $self->{_profile};
86 961 ▶ my $long_name = ref $transformer || transformer_long_name( $transformer );
Condition66%2/3
ref $transformer || transformer_long_name($transformer)
l!l&&r!l&&!r
950110
MC/DC0%0/2
ref $transformer || transformer_long_name($transformer)
ref $transformer transformer_long_name($transformer)
Truth table66%2/3
A || B
A= ref $transformerB= transformer_long_name($transformer)
ABresult
1X1
011
000
87 961 my $short_name = transformer_short_name( $long_name );
88  
89 return exists $profile->{"-$short_name"}
90 961 ▶ || exists $profile->{"-$long_name"};
Condition100%3/3
exists $$profile{"-$short_name"} || exists $$profile{"-$long_name"}
l!l&&r!l&&!r
82951
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
91 }
92  
93 #-----------------------------------------------------------------------------
94  
95 960 ▶sub transformer_is_enabled {
Subroutine transformer_is_enabled: called CC=3 SCAR=11.0
Pod documented
96  
97 960 my ( $self, $transformer ) = @_;
98 960 my $profile = $self->{_profile};
99 960 ▶ my $long_name = ref $transformer || transformer_long_name( $transformer );
Condition66%2/3
ref $transformer || transformer_long_name($transformer)
l!l&&r!l&&!r
950100
MC/DC0%0/2
ref $transformer || transformer_long_name($transformer)
ref $transformer transformer_long_name($transformer)
Truth table66%2/3
A || B
A= ref $transformerB= transformer_long_name($transformer)
ABresult
1X1
011
000
100 960 my $short_name = transformer_short_name( $long_name );
101  
102 return exists $profile->{$short_name}
103 960 ▶ || exists $profile->{$long_name};
Condition100%3/3
exists $profile->{$short_name} || exists $profile->{$long_name}
l!l&&r!l&&!r
522906
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
104 }
105  
106 #-----------------------------------------------------------------------------
107  
108 119 ▶sub listed_transformers {
Subroutine listed_transformers: called CC=2 SCAR=6.9
Pod documented
109  
110 119 my ( $self, $transformer ) = @_;
111 119 my @normalized_transformer_names = ();
112  
113 119 for my $transformer_name ( sort keys %{$self->{_profile}} ) {
114 55 $transformer_name =~ s/\A - //xmso; #Chomp leading "-"
115 55 my $transformer_long_name = transformer_long_name( $transformer_name );
116 55 push @normalized_transformer_names, $transformer_long_name;
117 }
118  
119 119 return @normalized_transformer_names;
120 }
121  
122 #-----------------------------------------------------------------------------
123  
124 960 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
125 960 my ( $self ) = @_;
126  
127 960 return $self->{_source};
128 }
129  
130 3 ▶sub _set_source {
Subroutine _set_source: called CC=1 SCAR=0.0
131 3 my ( $self, $source ) = @_;
132  
133 3 $self->{_source} = $source;
134  
135 3 return;
136 }
137  
138 #-----------------------------------------------------------------------------
139 # Begin PRIVATE methods
140  
141 16 Readonly::Hash my %LOADER_FOR => (
142 ARRAY => \&_load_profile_from_array,
143 DEFAULT => \&_load_profile_from_file,
144 HASH => \&_load_profile_from_hash,
145 SCALAR => \&_load_profile_from_string,
146 );
147  
148 128 ▶sub _load_profile {
Subroutine _load_profile: called CC=3 SCAR=11.0
149  
150 128 my ( $self, $profile ) = @_;
151  
152 128 ▶ my $ref_type = ref $profile || 'DEFAULT';
Condition100%2/2
ref $profile || 'DEFAULT'
l!l
13115
MC/DC100%1/1
ref $profile || 'DEFAULT'
ref $profile
Truth table100%2/2
A
A= ref $profile
Aresult
11
00
153 128 my $loader = $LOADER_FOR{$ref_type};
154  
155 128 ▶ if (not $loader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loader) 1 127 128
156 1 throw_internal qq{Can't load UserProfile from type "$ref_type"};
157 }
158  
159 127 $self->{_profile} = $loader->($self, $profile);
160 124 return $self;
161 }
162  
163 #-----------------------------------------------------------------------------
164  
165 124 ▶sub _set_options_processor {
Subroutine _set_options_processor: called CC=2 SCAR=6.9
166  
167 124 my ($self) = @_;
168 124 my $profile = $self->{_profile};
169 124 ▶ my $defaults = delete $profile->{__defaults__} || {};
Condition100%2/2
delete $profile->{'__defaults__'} || {}
l!l
4120
MC/DC100%1/1
delete $profile->{'__defaults__'} || {}
delete $profile->{'__defaults__'}
Truth table100%2/2
A
A= delete $profile->{'__defaults__'}
Aresult
11
00
170 $self->{_options_processor} =
171 124 Perl::ToPerl6::OptionsProcessor->new( %{ $defaults } );
172 124 return $self;
173 }
174  
175 #-----------------------------------------------------------------------------
176  
177 115 ▶sub _load_profile_from_file {
Subroutine _load_profile_from_file: called CC=5 SCAR=16.1
178 115 my ( $self, $file ) = @_;
179  
180 # Handle special cases.
181 115 ▶ return {} if not defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 25 90 115
182 90 ▶ return {} if $file eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $Perl::ToPerl6::UserProfile::EMPTY 3 87 90
183 87 ▶ return {} if $file eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "NONE" 84 3 87
184  
185 3 $self->_set_source( $file );
186  
187 3 my $profile = Config::Tiny->read( $file );
188 3 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 1 2 3
189 1 my $errstr = Config::Tiny::errstr();
190 1 throw_generic
191 message => qq{Could not parse profile "$file": $errstr},
192 source => $file;
193 }
194  
195 2 _fix_defaults_key( $profile );
196  
197 2 return $profile;
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 1 ▶sub _load_profile_from_array {
Subroutine _load_profile_from_array: called CC=2 SCAR=7.1
203 1 my ( $self, $array_ref ) = @_;
204 1 my $joined = join qq{\n}, @{ $array_ref };
205 1 my $profile = Config::Tiny->read_string( $joined );
206  
207 1 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 0 1 1
208 0 throw_generic 'Profile error: ' . Config::Tiny::errstr();
209 }
210  
211 1 _fix_defaults_key( $profile );
212  
213 1 return $profile;
214 }
215  
216 #-----------------------------------------------------------------------------
217  
218 5 ▶sub _load_profile_from_string {
Subroutine _load_profile_from_string: called CC=2 SCAR=6.9
219 5 my ( $self, $string ) = @_;
220 5 my $profile = Config::Tiny->read_string( ${ $string } );
221  
222 5 ▶ if (not defined $profile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profile) 2 3 5
223 2 throw_generic 'Profile error: ' . Config::Tiny::errstr();
224 }
225  
226 3 _fix_defaults_key( $profile );
227  
228 3 return $profile;
229 }
230  
231 #-----------------------------------------------------------------------------
232  
233 6 ▶sub _load_profile_from_hash {
Subroutine _load_profile_from_hash: called CC=1 SCAR=0.0
234 6 my ( $self, $hash_ref ) = @_;
235 6 return $hash_ref;
236 }
237  
238 #-----------------------------------------------------------------------------
239  
240 1 ▶sub _find_profile_path {
Subroutine _find_profile_path: called CC=5 SCAR=28.8
241  
242 #Define default filename
243 1 my $rc_file = '.perlmogrifyrc';
244  
245 #Check explicit environment setting
246 1 ▶ return $ENV{PERLMOGRIFY} if exists $ENV{PERLMOGRIFY};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"PERLMOGRIFY"} 1 0 1
247  
248 #Check current directory
249 0 ▶ return $rc_file if -f $rc_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $rc_file 0 0 0
250  
251 #Check home directory
252 0 ▶ if ( my $home_dir = _find_home_dir() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $home_dir = _find_home_dir()) 0 0 0
253 0 my $path = File::Spec->catfile( $home_dir, $rc_file );
254 0 ▶ return $path if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
255 }
256  
257 #No profile defined
258 0 return;
259 }
260  
261 #-----------------------------------------------------------------------------
262  
263 0 ▶sub _find_home_dir {
Subroutine _find_home_dir: not called CC=5 SCAR=34.0
264  
265 # Try using File::HomeDir
266 0 ▶ if ( eval { require File::HomeDir } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require File::HomeDir } }) 0 0 0
267 0 return File::HomeDir->my_home();
268 }
269  
270 # Check usual environment vars
271 0 for my $key (qw(HOME USERPROFILE HOMESHARE)) {
272 0 ▶ next if not defined $ENV{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$key} 0 0 0
273 0 ▶ return $ENV{$key} if -d $ENV{$key};
Branch: 1 branch
BranchTrueFalseTotal
if -d $ENV{$key} 0 0 0
274 }
275  
276 # No home directory defined
277 0 return;
278 }
279  
280 #-----------------------------------------------------------------------------
281  
282 # !$%@$%^ Config::Tiny uses a completely non-descriptive name for global
283 # values.
284 6 ▶sub _fix_defaults_key {
Subroutine _fix_defaults_key: called CC=2 SCAR=6.9
285 6 my ( $profile ) = @_;
286  
287 6 my $defaults = delete $profile->{_};
288 6 ▶ if ($defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults) 3 3 6
289 3 $profile->{__defaults__} = $defaults;
290 }
291  
292 6 return;
293 }
294  
295 16 1;
296  
297 __END__
« lib/Perl/ToPerl6/Transformer/Variables/ReplaceNegativeIndex.pm ↑ Summary lib/Perl/ToPerl6/Exception/AggregateConfiguration.pm »