lib/Perl/ToPerl6.pm

filter: Statementstmt 85.4% 100 / 117 Branchbran 53.1% 17 / 32 Conditioncond 64.2% 9 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 96.0% 24 / 25 Podpod 100% 6 / 6 totaltotal 77.8% 158 / 203 CC 25 SCAR 23.0
CC
25
Cov
77%
transform
28.1
_transform
8.3
_gather_transformations
6.7
CRAP 10.0
« lib/Perl/ToPerl6/Transformer/BasicTypes/Strings/RewriteHereDocs.pm ↑ Summary lib/Perl/ToPerl6/Transformer/CompoundStatements/RewriteLoops.pm »
1 package Perl::ToPerl6;
2  
3 12 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use List::MoreUtils qw< firstidx >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Perl::ToPerl6::Exception::Configuration::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Perl::ToPerl6::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Perl::ToPerl6::Transformation;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Perl::ToPerl6::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Perl::ToPerl6::Statistics;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use Perl::ToPerl6::Utils qw< :characters hashify shebang_line >;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #-----------------------------------------------------------------------------
24  
25 12 our $VERSION = '0.040';
26  
27 #-----------------------------------------------------------------------------
28  
29 12 Readonly::Array our @EXPORT_OK => qw(transform);
30  
31 #=============================================================================
32 # PUBLIC methods
33  
34 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
35 8 my ( $class, %args ) = @_;
36 8 my $self = bless {}, $class;
37 8 ▶ $self->{_config} = $args{-config} || Perl::ToPerl6::Config->new( %args );
Condition66%2/3
$args{'-config'} || 'Perl::ToPerl6::Config'->new(%args)
l!l&&r!l&&!r
071
MC/DC0%0/2
$args{'-config'} || 'Perl::ToPerl6::Config'->new(%args)
$args{'-config'} 'Perl::ToPerl6::Config'->new(%args)
Truth table33%1/3
A || B
A= $args{'-config'}B= 'Perl::ToPerl6::Config'->new(%args)
ABresult
1X1
011
000
38 7 $self->{_stats} = Perl::ToPerl6::Statistics->new();
39 7 return $self;
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 25 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
45 25 my $self = shift;
46 25 return $self->{_config};
47 }
48  
49 #-----------------------------------------------------------------------------
50  
51 0 ▶sub apply_transform {
Subroutine apply_transform: not called CC=1 SCAR=6.9
Pod documented
52 0 my ( $self, @args ) = @_;
53 #Delegate to Perl::ToPerl6::Config
54 0 return $self->config()->apply_transform( @args );
55 }
56  
57 #-----------------------------------------------------------------------------
58  
59 5 ▶sub transformers {
Subroutine transformers: called CC=1 SCAR=0.0
Pod documented
60 5 my $self = shift;
61  
62 #Delegate to Perl::ToPerl6::Config
63 5 return $self->config()->transformers();
64 }
65  
66 #-----------------------------------------------------------------------------
67  
68 6 ▶sub statistics {
Subroutine statistics: called CC=1 SCAR=0.0
Pod documented
69 6 my $self = shift;
70 6 return $self->{_stats};
71 }
72  
73 #-----------------------------------------------------------------------------
74  
75 7 ▶sub transform {
Subroutine transform: called CC=13 SCAR=33.3
Pod documented
76  
77 #-------------------------------------------------------------------
78 # This subroutine can be called as an object method or as a static
79 # function. In the latter case, the first argument can be a
80 # hashref of configuration parameters that shall be used to create
81 # an object behind the scenes. Note that this object does not
82 # persist. In other words, it is not a singleton.
83 #
84 # In addition, if it is called with a trailing 'doc => \$ref'
85 # named argument, the reference is populated with the serialized document.
86 # This is only really needed for test suites.
87 #
88 # Here are some of the ways this subroutine might get called:
89 #
90 # #Object style...
91 # $mogrify->transform( $code );
92 # $mogrify->transform( $code, doc => \$my_doc );
93 #
94 # #Functional style...
95 # transform( $code );
96 # transform( {}, $code );
97 # transform( {-foo => bar}, $code );
98 # transform( {-foo => bar}, $code, doc => \$my_doc );
99 #------------------------------------------------------------------
100  
101 7 ▶ my ( $self, $source_code ) = @_ >= 2 ? @_ : ( {}, $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 ? : 5 2 7
102 7 ▶ $self = ref $self eq 'HASH' ? __PACKAGE__->new(%{ $self }) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq 'HASH' ? : 4 3 7
103 7 ▶ return if not defined $source_code; # If no code, then nothing to do.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source_code 2 5 7
104  
105 5 my $config = $self->config();
106 5 ▶ my $doc =
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source_code) && $source_code->isa('Perl::ToPerl6::Document') ? : 1 4 5
Condition66%2/3
builtin::blessed($source_code) and $source_code->isa("Perl::ToPerl6::Document")
!ll&&!rl&&r
401
MC/DC50%1/2
builtin::blessed($source_code) and $source_code->isa("Perl::ToPerl6::Document")
builtin::blessed($source_code) $source_code->isa("Perl::ToPerl6::Document")
Truth table66%2/3
A and B
A= builtin::blessed($source_code)B= $source_code->isa("Perl::ToPerl6::Document")
ABresult
0X0
100
111
107 blessed($source_code) && $source_code->isa('Perl::ToPerl6::Document')
108 ? $source_code
109 : Perl::ToPerl6::Document->new(
110 '-source' => $source_code,
111 '-program-extensions' => [$config->program_extensions_as_regexes()],
112 );
113  
114 5 ▶ if ( 0 == $self->transformers() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $self->transformers) 0 5 5
115 0 Perl::ToPerl6::Exception::Configuration::Generic->throw(
116 message => 'There are no enabled transformers.',
117 )
118 }
119  
120 5 my @transformations = $self->_gather_transformations($doc);
121  
122 # Never thought I'd be smuggling myself in one of these.
123 #
124 5 ▶ if ( $_[-2] and $_[-2] eq 'doc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-2] and $_[-2] eq "doc") 0 5 5
Condition66%2/3
$_[-2] and $_[-2] eq "doc"
!ll&&!rl&&r
140
MC/DC0%0/2
$_[-2] and $_[-2] eq "doc"
$_[-2] $_[-2] eq "doc"
Truth table66%2/3
A and B
A= $_[-2]B= $_[-2] eq "doc"
ABresult
0X0
100
111
125 0 ${$_[-1]} = $doc->serialize;
126 }
127 5 ▶ unless( ref $source_code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $source_code) 0 5 5
128 0 ▶ if ( $config->in_place() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->in_place) { } 0 0 0
129 0 ▶ open my $fh, '>', $source_code
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $source_code or die "Could not overwrite '${source_code}': $!" 0 0 0
130 or die "Could not overwrite '$source_code': $!";
131 0 print $fh $doc->serialize;
132 0 close $fh;
133 }
134 else {
135 0 ▶ open my $fh, '>', $source_code . '.pl6'
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $source_code . '.pl6' or die "Could not write to '$source_code.pl6': $!" 0 0 0
136 or die "Could not write to '$source_code.pl6': $!";
137 0 print $fh $doc->serialize;
138 0 close $fh;
139 }
140 }
141 5 return @transformations;
142 }
143  
144 #=============================================================================
145 # PRIVATE methods
146  
147 5 ▶sub _gather_transformations {
Subroutine _gather_transformations: called CC=5 SCAR=19.0
148 5 my ($self, $doc) = @_;
149  
150 # Disable exempt code lines, if desired
151 5 ▶ if ( not $self->config->force() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->force) 5 0 5
152 5 $doc->process_annotations();
153 }
154  
155 # Evaluate each transformer
156 5 my @transformers = $self->config->transformers();
157 5 my @ordered_transformers = _futz_with_transformer_order(@transformers);
158 5 my @transformations = map { _transform($_, $doc) } @ordered_transformers;
159  
160 # Accumulate statistics
161 5 $self->statistics->accumulate( $doc, \@transformations );
162  
163 # If requested, rank transformations by their necessity and return the top N.
164 5 ▶ if ( @transformations && (my $top = $self->config->top()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@transformations and my $top = $self->config->top) 0 5 5
Condition33%1/3
@transformations and my $top = $self->config->top
!ll&&!rl&&r
050
MC/DC0%0/2
@transformations and my $top = $self->config->top
@transformations my $top = $self->config->top
Truth table33%1/3
A and B
A= @transformationsB= my $top = $self->config->top
ABresult
0X0
100
111
165 0 ▶ my $limit = @transformations < $top ? $#transformations : $top-1;
Branch: 1 branch
BranchTrueFalseTotal
@transformations < $top ? : 0 0 0
166 0 @transformations = Perl::ToPerl6::Transformation::sort_by_necessity(@transformations);
167 0 @transformations = ( reverse @transformations )[ 0 .. $limit ]; #Slicing...
168 }
169  
170 # Always return transformations sorted by location
171 5 return Perl::ToPerl6::Transformation->sort_by_location(@transformations);
172 }
173  
174 #=============================================================================
175 # PRIVATE functions
176  
177 187 ▶sub _transform {
Subroutine _transform: called CC=8 SCAR=21.1
178 187 my ($transformer, $doc) = @_;
179  
180 187 ▶ return if not $transformer->prepare_to_scan_document($doc);
Branch: 1 branch
BranchTrueFalseTotal
unless $transformer->prepare_to_scan_document($doc) 0 187 187
181  
182 187 my @transformations = ();
183  
184 TYPE:
185 187 for my $type ( $transformer->applies_to() ) {
186 197 my @elements;
187 197 ▶ if ($type eq 'PPI::Document') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'PPI::Document') { } 13 184 197
188 13 @elements = ($doc);
189 }
190 else {
191 184 ▶ @elements = @{ $doc->find($type) || [] };
Condition100%2/2
$doc->find($type) || []
l!l
18166
MC/DC100%1/1
$doc->find($type) || []
$doc->find($type)
Truth table100%2/2
A
A= $doc->find($type)
Aresult
11
00
192 }
193  
194 ELEMENT:
195 197 for my $element (@elements) {
196  
197 # Evaluate the transformer on this $element. A transformer may
198 # return zero or more transformations. We only want the
199 # transformations that occur on lines that have not been
200 # disabled.
201  
202 VIOLATION:
203 41 for my $transformation ( $transformer->transform( $element, $doc ) ) {
204  
205 24 my $line = $transformation->location()->[0];
206 24 ▶ if ( $doc->line_is_disabled_for_transformer($line, $transformer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->line_is_disabled_for_transformer($line, $transformer)) 0 24 24
207 0 $doc->add_suppressed_transformation($transformation);
208 0 next VIOLATION;
209 }
210  
211 24 push @transformations, $transformation;
212 }
213 }
214 }
215  
216 187 return @transformations;
217 }
218  
219 #-----------------------------------------------------------------------------
220  
221 5 ▶sub _futz_with_transformer_order {
Subroutine _futz_with_transformer_order: called CC=1 SCAR=0.0
222 # The ProhibitUselessNoCritic transformer is another special transformer. It
223 # deals with the transformations that *other* Transformers produce. Therefore
224 # it needs to be run *after* all the other Transformers. TODO: find
225 # a way for Transformers to express an ordering preference somehow.
226  
227 5 my @transformer_objects = @_;
228 5 my $magical_transformer_name = 'Perl::ToPerl6::Transformer::Miscellanea::ProhibitUselessNoCritic';
229 5 ▶ my $idx = firstidx {ref $_ eq $magical_transformer_name} @transformer_objects;
Subroutine __ANON__: called CC=1 SCAR=0.0
230 5 push @transformer_objects, splice @transformer_objects, $idx, 1;
231 5 return @transformer_objects;
232 }
233  
234 #-----------------------------------------------------------------------------
235  
236 12 1;
237  
238  
239  
240 __END__
« lib/Perl/ToPerl6/Transformer/BasicTypes/Strings/RewriteHereDocs.pm ↑ Summary lib/Perl/ToPerl6/Transformer/CompoundStatements/RewriteLoops.pm »