lib/Perl/ToPerl6/Transformer.pm

filter: Statementstmt 93.3% 181 / 194 Branchbran 71.4% 20 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.0% 58 / 63 Podpod 96.0% 24 / 25 totaltotal 90.4% 285 / 315 CC 17 SCAR 7.6
CC
17
Cov
90%
new
7.0
_format_lack_of_parameter_metadata
5.2
__set_base_parameters
5.0
CRAP 2.1
« lib/Perl/ToPerl6/Transformer/Pragmas/RewritePragmas.pm ↑ Summary lib/Perl/ToPerl6/TransformerParameter.pm »
1 package Perl::ToPerl6::Transformer;
2  
3 23 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use String::Format qw< stringf >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 23 ▶use overload ( q<""> => 'to_string', cmp => '_compare' );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 ▶use Perl::ToPerl6::Utils qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
16 :characters
17 :booleans
18 :severities
19 :data_conversion
20 interpolate
21 is_integer
22 transformer_long_name
23 transformer_short_name
24 necessity_to_number
25 23 >;
26 23 ▶use Perl::ToPerl6::Utils::DataConversion qw< dor >;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 23 ▶use Perl::ToPerl6::Utils::POD qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
28 get_module_abstract_for_module
29 get_raw_module_abstract_for_module
30 23 >;
31 23 ▶use Perl::ToPerl6::Exception::AggregateConfiguration;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 23 ▶use Perl::ToPerl6::Exception::Configuration;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 23 ▶use Perl::ToPerl6::Exception::Configuration::Option::Transformer::ExtraParameter;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 23 ▶use Perl::ToPerl6::Exception::Configuration::Option::Transformer::ParameterValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 use Perl::ToPerl6::Exception::Fatal::TransformerDefinition
36 23 ▶ qw< throw_transformer_definition >;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 23 ▶use Perl::ToPerl6::TransformerConfig qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 23 ▶use Perl::ToPerl6::TransformerParameter qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 23 ▶use Perl::ToPerl6::Transformation qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 23 ▶use Exception::Class; # this must come after "use P::C::Exception::*"
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 #-----------------------------------------------------------------------------
44  
45 23 Readonly::Scalar my $NO_LIMIT => 'no_limit';
46  
47 #-----------------------------------------------------------------------------
48  
49 23 my $format = "%p\n"; #Default stringy format
50  
51 #-----------------------------------------------------------------------------
52  
53 1433 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
54 1433 my ($class, %config) = @_;
55  
56 1433 my $self = bless {}, $class;
57  
58 1433 my $config_object;
59 1433 ▶ if ($config{_config_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{'_config_object'}) { } 0 1433 1433
60 0 $config_object = $config{_config_object};
61 }
62 else {
63 1433 $config_object =
64 Perl::ToPerl6::TransformerConfig->new(
65 $self->get_short_name(),
66 \%config,
67 );
68 }
69  
70 1433 $self->__set_config( $config_object );
71  
72 1433 my @parameters;
73 1433 my $parameter_metadata_available = 0;
74  
75 1433 ▶ if ( $class->can('supported_parameters') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("supported_parameters")) 1374 59 1433
76 1374 $parameter_metadata_available = 1;
77 @parameters =
78 map
79 1374 { Perl::ToPerl6::TransformerParameter->new($_) }
80 $class->supported_parameters();
81 }
82 1433 $self->{_parameter_metadata_available} = $parameter_metadata_available;
83 1433 $self->{_parameters} = \@parameters;
84  
85 1433 my $errors = Perl::ToPerl6::Exception::AggregateConfiguration->new();
86 1433 foreach my $parameter ( @parameters ) {
87 eval {
88 37 $parameter->parse_and_validate_config_value( $self, $config_object );
89 }
90 37 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $parameter->parse_and_validate_config_value($self, $config_object) } } or do { $errors->add_exception_or_rethrow($Perl::ToPerl6::Transformer::EVAL_ERROR) } 0 37 37
91 37 $errors->add_exception_or_rethrow($EVAL_ERROR);
92 };
93  
94 37 $config_object->remove( $parameter->get_name() );
95 }
96  
97 1433 ▶ if ($parameter_metadata_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter_metadata_available) 1374 59 1433
98 1374 $config_object->handle_extra_parameters( $self, $errors );
99 }
100  
101 1433 ▶ if ( $errors->has_exceptions() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors->has_exceptions) 38 1395 1433
102 38 $errors->rethrow();
103 }
104  
105 1395 return $self;
106 }
107  
108 #-----------------------------------------------------------------------------
109  
110 906 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
111 906 return $TRUE;
112 }
113  
114 #-----------------------------------------------------------------------------
115  
116 187 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=1 SCAR=0.0
Pod documented
117 187 return $TRUE;
118 }
119  
120 #-----------------------------------------------------------------------------
121  
122 86 ▶sub __get_parameter_name {
Subroutine __get_parameter_name: called CC=1 SCAR=0.0
123 86 my ( $self, $parameter ) = @_;
124  
125 86 return '_' . $parameter->get_name();
126 }
127  
128 #-----------------------------------------------------------------------------
129  
130 86 ▶sub __set_parameter_value {
Subroutine __set_parameter_value: called CC=1 SCAR=0.0
131 86 my ( $self, $parameter, $value ) = @_;
132  
133 86 $self->{ $self->__get_parameter_name($parameter) } = $value;
134  
135 86 return;
136 }
137  
138 #-----------------------------------------------------------------------------
139  
140 1296 ▶sub __set_base_parameters {
Subroutine __set_base_parameters: called CC=5 SCAR=16.1
141 1296 my ($self) = @_;
142  
143 1296 my $config = $self->__get_config();
144 1296 my $errors = Perl::ToPerl6::Exception::AggregateConfiguration->new();
145  
146 1296 my $user_necessity = $config->get_necessity();
147 1296 ▶ if ( defined $user_necessity ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_necessity) 45 1251 1296
148 45 my $normalized_necessity = necessity_to_number( $user_necessity );
149 44 $self->set_necessity( $normalized_necessity );
150 }
151  
152 1295 my $user_set_themes = $config->get_set_themes();
153 1295 ▶ if ( defined $user_set_themes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_set_themes) 38 1257 1295
154 38 my @set_themes = words_from_string( $user_set_themes );
155 38 $self->set_themes( @set_themes );
156 }
157  
158 1295 my $user_add_themes = $config->get_add_themes();
159 1295 ▶ if ( defined $user_add_themes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_add_themes) 41 1254 1295
160 41 my @add_themes = words_from_string( $user_add_themes );
161 41 $self->add_themes( @add_themes );
162 }
163  
164 1295 ▶ if ( $errors->has_exceptions() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors->has_exceptions) 0 1295 1295
165 0 $errors->rethrow();
166 }
167  
168 1295 return;
169 }
170  
171 #-----------------------------------------------------------------------------
172  
173 # Unparsed configuration, P::C::TransformerConfig. Compare with get_parameters().
174 2202 ▶sub __get_config {
Subroutine __get_config: called CC=1 SCAR=0.0
175 2202 my ($self) = @_;
176  
177 2202 return $self->{_config};
178 }
179  
180 2729 ▶sub __set_config {
Subroutine __set_config: called CC=1 SCAR=0.0
181 2729 my ($self, $config) = @_;
182  
183 2729 $self->{_config} = $config;
184  
185 2729 return;
186 }
187  
188 #-----------------------------------------------------------------------------
189  
190 0 ▶sub get_long_name {
Subroutine get_long_name: not called CC=1 SCAR=6.9
Pod documented
191 0 my ($self) = @_;
192  
193 0 return transformer_long_name(ref $self);
194 }
195  
196 #-----------------------------------------------------------------------------
197  
198 6925 ▶sub get_short_name {
Subroutine get_short_name: called CC=1 SCAR=0.0
Pod documented
199 6925 my ($self) = @_;
200  
201 6925 return transformer_short_name(ref $self);
202 }
203  
204 #-----------------------------------------------------------------------------
205  
206 2 ▶sub is_enabled {
Subroutine is_enabled: called CC=1 SCAR=0.0
Pod documented
207 2 my ($self) = @_;
208  
209 2 return $self->{_enabled};
210 }
211  
212 #-----------------------------------------------------------------------------
213  
214 906 ▶sub __set_enabled {
Subroutine __set_enabled: called CC=1 SCAR=0.0
215 906 my ($self, $new_value) = @_;
216  
217 906 $self->{_enabled} = $new_value;
218  
219 906 return;
220 }
221  
222 #-----------------------------------------------------------------------------
223  
224 1 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
225 1 return qw(PPI::Element);
226 }
227  
228 #-----------------------------------------------------------------------------
229  
230 45 ▶sub set_necessity {
Subroutine set_necessity: called CC=1 SCAR=0.0
Pod documented
231 45 my ($self, $necessity) = @_;
232 45 $self->{_necessity} = $necessity;
233 45 return $self;
234 }
235  
236 #-----------------------------------------------------------------------------
237  
238 1203 ▶sub get_necessity {
Subroutine get_necessity: called CC=2 SCAR=7.1
Pod documented
239 1203 my ($self) = @_;
240 1203 ▶ return $self->{_necessity} || $self->default_necessity();
Condition66%2/3
$self->{'_necessity'} || $self->default_necessity
l!l&&r!l&&!r
15510480
MC/DC0%0/2
$self->{'_necessity'} || $self->default_necessity
$self->{'_necessity'} $self->default_necessity
Truth table66%2/3
A || B
A= $self->{'_necessity'}B= $self->default_necessity
ABresult
1X1
011
000
241 }
242  
243 #-----------------------------------------------------------------------------
244  
245 3 ▶sub default_necessity {
Subroutine default_necessity: called CC=1 SCAR=0.0
Pod documented
246 3 return $NECESSITY_LOWEST;
247 }
248  
249 #-----------------------------------------------------------------------------
250  
251 39 ▶sub set_themes {
Subroutine set_themes: called CC=1 SCAR=0.0
Pod documented
252 39 my ($self, @themes) = @_;
253 39 $self->{_themes} = [ sort @themes ];
254 39 return $self;
255 }
256  
257 #-----------------------------------------------------------------------------
258  
259 1110 ▶sub get_themes {
Subroutine get_themes: called CC=2 SCAR=6.9
Pod documented
260 1110 my ($self) = @_;
261 1110 ▶ my @themes = defined $self->{_themes} ? @{ $self->{_themes} } : $self->default_themes();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_themes'} ? : 192 918 1110
262 1110 my @sorted_themes = sort @themes;
263 1110 return @sorted_themes;
264 }
265  
266 #-----------------------------------------------------------------------------
267  
268 42 ▶sub add_themes {
Subroutine add_themes: called CC=1 SCAR=0.0
Pod documented
269 42 my ($self, @additional_themes) = @_;
270 #By hashifying the themes, we squish duplicates
271 42 my %merged = hashify( $self->get_themes(), @additional_themes);
272 42 $self->{_themes} = [ keys %merged];
273 42 return $self;
274 }
275  
276 #-----------------------------------------------------------------------------
277  
278 4 ▶sub default_themes {
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
279 4 return ();
280 }
281  
282 #-----------------------------------------------------------------------------
283  
284 186 ▶sub get_abstract {
Subroutine get_abstract: called CC=1 SCAR=0.0
Pod documented
285 186 my ($self) = @_;
286  
287 186 return get_module_abstract_for_module( ref $self );
288 }
289  
290 #-----------------------------------------------------------------------------
291  
292 0 ▶sub get_raw_abstract {
Subroutine get_raw_abstract: not called CC=1 SCAR=6.9
Pod documented
293 0 my ($self) = @_;
294  
295 0 return get_raw_module_abstract_for_module( ref $self );
296 }
297  
298 #-----------------------------------------------------------------------------
299  
300 408 ▶sub parameter_metadata_available {
Subroutine parameter_metadata_available: called CC=1 SCAR=0.0
Pod documented
301 408 my ($self) = @_;
302  
303 408 return $self->{_parameter_metadata_available};
304 }
305  
306 #-----------------------------------------------------------------------------
307  
308 221 ▶sub get_parameters {
Subroutine get_parameters: called CC=1 SCAR=0.0
Pod documented
309 221 my ($self) = @_;
310  
311 221 return $self->{_parameters};
312 }
313  
314 #-----------------------------------------------------------------------------
315  
316 1 ▶sub transform {
Subroutine transform: called CC=1 SCAR=0.0
Pod undocumented
317 1 my ($self) = @_;
318  
319 1 return throw_transformer_definition(
320 $self->get_short_name() . q/ does not implement transform()./ );
321 }
322  
323 #-----------------------------------------------------------------------------
324  
325 24 ▶sub transformation {
Subroutine transformation: called CC=1 SCAR=0.0
Pod documented
326 24 my ( $self, $desc, $expl, $elem ) = @_;
327 # HACK!! Use goto instead of an explicit call because P::C::V::new() uses caller()
328 24 my $sev = $self->get_necessity();
329 24 @_ = ('Perl::ToPerl6::Transformation', $desc, $expl, $elem, $sev );
330 24 goto &Perl::ToPerl6::Transformation::new;
331 }
332  
333 #-----------------------------------------------------------------------------
334  
335 7 ▶sub new_parameter_value_exception {
Subroutine new_parameter_value_exception: called CC=1 SCAR=0.0
Pod documented
336 7 my ( $self, $option_name, $option_value, $source, $message_suffix ) = @_;
337  
338 7 return Perl::ToPerl6::Exception::Configuration::Option::Transformer::ParameterValue->new(
339 transformer => $self->get_short_name(),
340 option_name => $option_name,
341 option_value => $option_value,
342 source => $source,
343 message_suffix => $message_suffix
344 );
345 }
346  
347 #-----------------------------------------------------------------------------
348  
349 7 ▶sub throw_parameter_value_exception {
Subroutine throw_parameter_value_exception: called CC=1 SCAR=0.0
Pod documented
350 7 my ( $self, $option_name, $option_value, $source, $message_suffix ) = @_;
351  
352 7 $self->new_parameter_value_exception(
353 $option_name, $option_value, $source, $message_suffix
354 )
355 ->throw();
356 }
357 ## use mogrify
358  
359  
360 #-----------------------------------------------------------------------------
361  
362 # Static methods.
363  
364 3 ▶sub set_format { return $format = $_[0] }
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
365 5446 ▶sub get_format { return $format }
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
366  
367 #-----------------------------------------------------------------------------
368  
369 5444 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.2
Pod documented
370 5444 my ($self, @args) = @_;
371  
372 # Wrap the more expensive ones in sub{} to postpone evaluation
373 my %fspec = (
374 0 ▶ 'P' => sub { $self->get_long_name() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 5444 ▶ 'p' => sub { $self->get_short_name() },
Subroutine __ANON__: called CC=1 SCAR=0.0
376 186 ▶ 'a' => sub { dor($self->get_abstract(), $EMPTY) },
Subroutine __ANON__: called CC=1 SCAR=0.0
377 186 ▶ 'O' => sub { $self->_format_parameters(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
378 186 ▶ 'U' => sub { $self->_format_lack_of_parameter_metadata(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
379 0 ▶ 'S' => sub { $self->default_necessity() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 226 ▶ 's' => sub { $self->get_necessity() },
Subroutine __ANON__: called CC=1 SCAR=0.0
381 0 ▶ 'T' => sub { join $SPACE, $self->default_themes() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 226 ▶ 't' => sub { join $SPACE, $self->get_themes() },
Subroutine __ANON__: called CC=1 SCAR=0.0
383 5444 );
384 5444 return stringf(get_format(), %fspec);
385 }
386  
387 186 ▶sub _format_parameters {
Subroutine _format_parameters: called CC=3 SCAR=11.7
388 186 my ($self, $parameter_format) = @_;
389  
390 186 ▶ return $EMPTY if not $self->parameter_metadata_available();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parameter_metadata_available 0 186 186
391  
392 186 my $separator;
393 186 ▶ if ($parameter_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter_format) { } 186 0 186
394 186 $separator = $EMPTY;
395 } else {
396 0 $separator = $SPACE;
397 0 $parameter_format = '%n';
398 }
399  
400 return
401 join
402 $separator,
403 186 map { $_->to_formatted_string($parameter_format) } @{ $self->get_parameters() };
404 }
405  
406 186 ▶sub _format_lack_of_parameter_metadata {
Subroutine _format_lack_of_parameter_metadata: called CC=3 SCAR=16.5
407 186 my ($self, $message) = @_;
408  
409 186 ▶ return $EMPTY if $self->parameter_metadata_available();
Branch: 1 branch
BranchTrueFalseTotal
if $self->parameter_metadata_available 186 0 186
410 0 ▶ return interpolate($message) if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
411  
412 return
413 0 'Cannot programmatically discover what parameters this transformer takes.';
414 }
415  
416 #-----------------------------------------------------------------------------
417 # Apparently, some perls do not implicitly stringify overloading
418 # objects before doing a comparison. This causes a couple of our
419 # sorting tests to fail. To work around this, we overload C<cmp> to
420 # do it explicitly.
421 #
422 # 20060503 - More information: This problem has been traced to
423 # Test::Simple versions <= 0.60, not perl itself. Upgrading to
424 # Test::Simple v0.62 will fix the problem. But rather than forcing
425 # everyone to upgrade, I have decided to leave this workaround in
426 # place.
427  
428 158 ▶sub _compare { return "$_[0]" cmp "$_[1]" }
Subroutine _compare: called CC=1 SCAR=0.0
429  
430 23 1;
431  
432 __END__
« lib/Perl/ToPerl6/Transformer/Pragmas/RewritePragmas.pm ↑ Summary lib/Perl/ToPerl6/TransformerParameter.pm »