lib/Perl/ToPerl6/Transformation.pm

filter: Statementstmt 98.7% 154 / 156 Branchbran 78.5% 22 / 28 Conditioncond 65.5% 19 / 29 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 95.7% 45 / 47 Podpod 100% 20 / 20 totaltotal 89.5% 266 / 297 CC 28 SCAR 11.1
CC
28
Cov
92%
_line_containing_transformation
7.1
sort_by_location
6.5
new
6.0
CRAP 3.0
« lib/Perl/ToPerl6/Utils/POD.pm ↑ Summary lib/Perl/ToPerl6/TransformerConfig.pm »
1 package Perl::ToPerl6::Transformation;
2  
3 26 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 26 ▶use File::Basename qw< basename >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use IO::String qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Pod::PlainText qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use String::Format qw< stringf >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 26 ▶use overload ( q{""} => 'to_string', cmp => '_compare' );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 26 ▶use Perl::ToPerl6::Utils qw< :characters :internal_lookup >;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 26 ▶use Perl::ToPerl6::Utils::POD qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
20 get_pod_section_for_module
21 trim_pod_section
22 26 >;
23 26 ▶use Perl::ToPerl6::Exception::Fatal::Internal qw< throw_internal >;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 26 Readonly::Scalar my $LOCATION_LINE_NUMBER => 0;
26 26 Readonly::Scalar my $LOCATION_COLUMN_NUMBER => 1;
27 26 Readonly::Scalar my $LOCATION_VISUAL_COLUMN_NUMBER => 2;
28 26 Readonly::Scalar my $LOCATION_LOGICAL_LINE_NUMBER => 3;
29 26 Readonly::Scalar my $LOCATION_LOGICAL_FILENAME => 4;
30  
31  
32 # Class variables...
33 26 my $format = "%m at line %l, column %c. %e.\n"; # Default stringy format
34 26 my %diagnostics = (); # Cache of diagnostic messages
35  
36 #-----------------------------------------------------------------------------
37  
38 26 Readonly::Scalar my $CONSTRUCTOR_ARG_COUNT => 5;
39  
40 44 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
41 44 my ( $class, $desc, $expl, $elem, $sev ) = @_;
42  
43 # Check arguments to help out developers who might
44 # be creating new Perl::ToPerl6::Transformer modules.
45  
46 44 ▶ if ( @_ != $CONSTRUCTOR_ARG_COUNT ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != $CONSTRUCTOR_ARG_COUNT) 1 43 44
47 1 throw_internal 'Wrong number of args to Transformation->new()';
48 }
49  
50 43 ▶ if ( eval { $elem->isa( 'Perl::ToPerl6::Document' ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $elem->isa("Perl::ToPerl6::Document") } }) 1 42 43
51 # break the facade, return the real PPI::Document
52 1 $elem = $elem->ppi_document();
53 }
54  
55 43 ▶ if ( not eval { $elem->isa( 'PPI::Element' ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $elem->isa("PPI::Element") } }) 1 42 43
56 1 throw_internal '3rd arg to Transformation->new() must be a PPI::Element';
57 }
58  
59 # Strip punctuation. These are controlled by the user via the
60 # formats. He/She can use whatever makes sense to them.
61 42 ($desc, $expl) = _chomp_periods($desc, $expl);
62  
63 # Create object
64 42 my $self = bless {}, $class;
65 42 $self->{_description} = $desc;
66 42 $self->{_explanation} = $expl;
67 42 $self->{_necessity} = $sev;
68 42 $self->{_transformer} = caller;
69  
70 # PPI eviscerates the Elements in a Document when the Document gets
71 # DESTROY()ed, and thus they aren't useful after it is gone. So we have
72 # to preemptively grab everything we could possibly want.
73 42 $self->{_element_class} = blessed $elem;
74  
75 42 my $top = $elem->top();
76 42 ▶ $self->{_filename} = $top->can('filename') ? $top->filename() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$top->can('filename') ? : 42 0 42
77 42 $self->{_source} = _line_containing_transformation( $elem );
78 $self->{_location} =
79 42 ▶ $elem->location() || [ 0, 0, 0, 0, $self->filename() ];
Condition50%1/2
$elem->location || [0, 0, 0, 0, $self->filename]
l!l
420
MC/DC0%0/1
$elem->location || [0, 0, 0, 0, $self->filename]
$elem->location
Truth table50%1/2
A
A= $elem->location
Aresult
11
00
80  
81 42 return $self;
82 }
83  
84 #-----------------------------------------------------------------------------
85  
86 19 ▶sub set_format { return $format = verbosity_to_format( $_[0] ); }
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
87 6 ▶sub get_format { return $format; }
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
88  
89 #-----------------------------------------------------------------------------
90  
91 6 ▶sub sort_by_location {
Subroutine sort_by_location: called CC=6 SCAR=18.7
Pod documented
92  
93 6 ▶ ref $_[0] || shift; # Can call as object or class method
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or shift() 0 6 6
94 6 ▶ return scalar @_ if ! wantarray; # In case we are called in scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 6 6
95  
96 ## TODO: What if $a and $b are not Transformation objects?
97 return
98 28 map {$_->[0]}
99 46 ▶ sort { ($a->[1] <=> $b->[1]) || ($a->[2] <=> $b->[2]) }
Condition100%3/3
$Perl::ToPerl6::Transformation::a->[1] <=> $Perl::ToPerl6::Transformation::b->[1] || $Perl::ToPerl6::Transformation::a->[2] <=> $Perl::ToPerl6::Transformation::b->[2]
l!l&&r!l&&!r
171316
MC/DC100%2/2
$Perl::ToPerl6::Transformation::a->[1] <=> $Perl::ToPerl6::Transformation::b->[1] || $Perl::ToPerl6::Transformation::a->[2] <=> $Perl::ToPerl6::Transformation::b->[2]
$Perl::ToPerl6::Transformation::a->[1] <=> $Perl::ToPerl6::Transformation::b->[1] $Perl::ToPerl6::Transformation::a->[2] <=> $Perl::ToPerl6::Transformation::b->[2]
Truth table100%3/3
A || B
A= $Perl::ToPerl6::Transformation::a->[1] <=> $Perl::ToPerl6::Transformation::b->[1]B= $Perl::ToPerl6::Transformation::a->[2] <=> $Perl::ToPerl6::Transformation::b->[2]
ABresult
1X1
011
000
100 6 ▶ map {[$_, $_->location->[0] || 0, $_->location->[1] || 0]}
Condition50%2/4
$_->location->[0] || 0
l!l
280
$_->location->[1] || 0
l!l
280
MC/DC0%0/1
$_->location->[0] || 0
$_->location->[0]
MC/DC0%0/1
$_->location->[1] || 0
$_->location->[1]
Truth table50%1/2
A
A= $_->location->[0]
Aresult
11
00
Truth table50%1/2
A
A= $_->location->[1]
Aresult
11
00
101 @_;
102 }
103  
104 #-----------------------------------------------------------------------------
105  
106 1 ▶sub sort_by_necessity {
Subroutine sort_by_necessity: called CC=4 SCAR=14.0
Pod documented
107  
108 1 ▶ ref $_[0] || shift; # Can call as object or class method
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or shift() 0 1 1
109 1 ▶ return scalar @_ if ! wantarray; # In case we are called in scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
110  
111 ## TODO: What if $a and $b are not Transformation objects?
112 return
113 6 map {$_->[0]}
114 9 sort { $a->[1] <=> $b->[1] }
115 1 ▶ map {[$_, $_->necessity() || 0]}
Condition100%2/2
$_->necessity || 0
l!l
51
MC/DC100%1/1
$_->necessity || 0
$_->necessity
Truth table100%2/2
A
A= $_->necessity
Aresult
11
00
116 @_;
117 }
118  
119 #-----------------------------------------------------------------------------
120  
121 116 ▶sub location {
Subroutine location: called CC=1 SCAR=0.0
Pod documented
122 116 my $self = shift;
123  
124 116 return $self->{_location};
125 }
126  
127 #-----------------------------------------------------------------------------
128  
129 4 ▶sub line_number {
Subroutine line_number: called CC=1 SCAR=0.0
Pod documented
130 4 my ($self) = @_;
131  
132 4 return $self->location()->[$LOCATION_LINE_NUMBER];
133 }
134  
135 #-----------------------------------------------------------------------------
136  
137 14 ▶sub logical_line_number {
Subroutine logical_line_number: called CC=1 SCAR=0.0
Pod documented
138 14 my ($self) = @_;
139  
140 14 return $self->location()->[$LOCATION_LOGICAL_LINE_NUMBER];
141 }
142  
143 #-----------------------------------------------------------------------------
144  
145 2 ▶sub column_number {
Subroutine column_number: called CC=1 SCAR=0.0
Pod documented
146 2 my ($self) = @_;
147  
148 2 return $self->location()->[$LOCATION_COLUMN_NUMBER];
149 }
150  
151 #-----------------------------------------------------------------------------
152  
153 12 ▶sub visual_column_number {
Subroutine visual_column_number: called CC=1 SCAR=0.0
Pod documented
154 12 my ($self) = @_;
155  
156 12 return $self->location()->[$LOCATION_VISUAL_COLUMN_NUMBER];
157 }
158  
159 #-----------------------------------------------------------------------------
160  
161 5 ▶sub diagnostics {
Subroutine diagnostics: called CC=4 SCAR=14.0
Pod documented
162 5 my ($self) = @_;
163 5 my $transformer = $self->transformer();
164  
165 5 ▶ if ( not $diagnostics{$transformer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diagnostics{$transformer}) 2 3 5
166 2 eval {
167 2 ▶ my $module_name = ref $transformer || $transformer;
Condition33%1/3
ref $transformer || $transformer
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $transformer || $transformer
ref $transformer $transformer
Truth table33%1/3
A || B
A= ref $transformerB= $transformer
ABresult
1X1
011
000
168 2 $diagnostics{$transformer} =
169 trim_pod_section(
170 get_pod_section_for_module( $module_name, 'DESCRIPTION' )
171 );
172 };
173 2 ▶ $diagnostics{$transformer} ||= " No diagnostics available\n";
Condition100%2/2
$diagnostics{$transformer} ||= " No diagnostics available\n"
l!l
11
MC/DC100%1/1
$diagnostics{$transformer} ||= " No diagnostics available\n"
$diagnostics{$transformer}
Truth table100%2/2
A
A= $diagnostics{$transformer}
Aresult
11
00
174 }
175 5 return $diagnostics{$transformer};
176 }
177  
178 #-----------------------------------------------------------------------------
179  
180 24 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
181 24 my $self = shift;
182 24 return $self->{_description};
183 }
184  
185 #-----------------------------------------------------------------------------
186  
187 26 ▶sub explanation {
Subroutine explanation: called CC=4 SCAR=13.9
Pod documented
188 26 my $self = shift;
189 26 my $expl = $self->{_explanation};
190 26 ▶ if ( !$expl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expl) 20 6 26
191 20 $expl = '(no explanation)';
192 }
193 26 ▶ if ( ref $expl eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expl eq "ARRAY") 2 24 26
194 2 ▶ my $page = @{$expl} > 1 ? 'pages' : 'page';
Branch: 1 branch
BranchTrueFalseTotal
@{$expl;} > 1 ? : 1 1 2
195 2 $page .= $SPACE . join $COMMA, @{$expl};
196 2 $expl = "See $page of PBP";
197 }
198 26 return $expl;
199 }
200  
201 #-----------------------------------------------------------------------------
202  
203 60 ▶sub necessity {
Subroutine necessity: called CC=1 SCAR=0.0
Pod documented
204 60 my $self = shift;
205 60 return $self->{_necessity};
206 }
207  
208 #-----------------------------------------------------------------------------
209  
210 53 ▶sub transformer {
Subroutine transformer: called CC=1 SCAR=0.0
Pod documented
211 53 my $self = shift;
212 53 return $self->{_transformer};
213 }
214  
215 #-----------------------------------------------------------------------------
216  
217 4 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
218 4 my $self = shift;
219 4 return $self->{_filename};
220 }
221  
222 #-----------------------------------------------------------------------------
223  
224 4 ▶sub logical_filename {
Subroutine logical_filename: called CC=1 SCAR=0.0
Pod documented
225 4 my ($self) = @_;
226  
227 4 return $self->location()->[$LOCATION_LOGICAL_FILENAME];
228 }
229  
230 #-----------------------------------------------------------------------------
231  
232 4 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
233 4 my $self = shift;
234 4 return $self->{_source};
235 }
236  
237 #-----------------------------------------------------------------------------
238  
239 2 ▶sub element_class {
Subroutine element_class: called CC=1 SCAR=0.0
Pod documented
240 2 my ($self) = @_;
241  
242 2 return $self->{_element_class};
243 }
244  
245 #-----------------------------------------------------------------------------
246  
247 22 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
248 22 my $self = shift;
249  
250 22 my $long_transformer = $self->transformer();
251 22 (my $short_transformer = $long_transformer) =~ s/ \A Perl::ToPerl6::Transformer:: //xms;
252  
253 # Wrap the more expensive ones in sub{} to postpone evaluation
254 my %fspec = (
255 2 ▶ 'f' => sub { $self->logical_filename() },
Subroutine __ANON__: called CC=1 SCAR=0.0
256 2 ▶ 'F' => sub { basename( $self->logical_filename() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
257 2 ▶ 'g' => sub { $self->filename() },
Subroutine __ANON__: called CC=1 SCAR=0.0
258 2 ▶ 'G' => sub { basename( $self->filename() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
259 12 ▶ 'l' => sub { $self->logical_line_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
260 2 ▶ 'L' => sub { $self->line_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 10 ▶ 'c' => sub { $self->visual_column_number() },
Subroutine __ANON__: called CC=1 SCAR=0.0
262 0 ▶ 'C' => sub { $self->element_class() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 'm' => $self->description(),
264 'e' => $self->explanation(),
265 's' => $self->necessity(),
266 2 ▶ 'd' => sub { $self->diagnostics() },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 2 ▶ 'r' => sub { $self->source() },
Subroutine __ANON__: called CC=1 SCAR=0.0
268 22 'P' => $long_transformer,
269 'p' => $short_transformer,
270 );
271 22 return stringf($format, %fspec);
272 }
273  
274 #-----------------------------------------------------------------------------
275 # Apparently, some perls do not implicitly stringify overloading
276 # objects before doing a comparison. This causes a couple of our
277 # sorting tests to fail. To work around this, we overload C<cmp> to
278 # do it explicitly.
279 #
280 # 20060503 - More information: This problem has been traced to
281 # Test::Simple versions <= 0.60, not perl itself. Upgrading to
282 # Test::Simple v0.62 will fix the problem. But rather than forcing
283 # everyone to upgrade, I have decided to leave this workaround in
284 # place.
285  
286 0 ▶sub _compare { return "$_[0]" cmp "$_[1]" }
Subroutine _compare: not called CC=1 SCAR=6.9
287  
288 #-----------------------------------------------------------------------------
289  
290 42 ▶sub _line_containing_transformation {
Subroutine _line_containing_transformation: called CC=6 SCAR=19.6
291 42 my ( $elem ) = @_;
292  
293 42 ▶ my $stmnt = $elem->statement() || $elem;
Condition66%2/3
$elem->statement || $elem
l!l&&r!l&&!r
29130
MC/DC0%0/2
$elem->statement || $elem
$elem->statement $elem
Truth table66%2/3
A || B
A= $elem->statementB= $elem
ABresult
1X1
011
000
294 42 ▶ my $code_string = $stmnt->content() || $EMPTY;
Condition33%1/3
$stmnt->content || $Perl::ToPerl6::Transformation::EMPTY
l!l&&r!l&&!r
4200
MC/DC0%0/2
$stmnt->content || $Perl::ToPerl6::Transformation::EMPTY
$stmnt->content $Perl::ToPerl6::Transformation::EMPTY
Truth table33%1/3
A || B
A= $stmnt->contentB= $Perl::ToPerl6::Transformation::EMPTY
ABresult
1X1
011
000
295  
296 # Split into individual lines
297 42 my @lines = split qr{ \n\s* }xms, $code_string;
298  
299 # Take the line containing the element that is in transformation
300 42 ▶ my $inx = ( $elem->line_number() || 0 ) -
Condition50%2/4
$elem->line_number || 0
l!l
420
$stmnt->line_number || 0
l!l
420
MC/DC0%0/1
$elem->line_number || 0
$elem->line_number
MC/DC0%0/1
$stmnt->line_number || 0
$stmnt->line_number
Truth table50%1/2
A
A= $elem->line_number
Aresult
11
00
Truth table50%1/2
A
A= $stmnt->line_number
Aresult
11
00
301 ( $stmnt->line_number() || 0 );
302 42 ▶ $inx > @lines and return $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
$inx > @lines and return $Perl::ToPerl6::Transformation::EMPTY 0 42 42
303 42 return $lines[$inx];
304 }
305  
306 #-----------------------------------------------------------------------------
307  
308 43 ▶sub _chomp_periods {
Subroutine _chomp_periods: called CC=4 SCAR=13.9
309 43 my @args = @_;
310  
311 43 for (@args) {
312 90 ▶ next if not defined or ref;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or ref $_ 6 84 90
Condition100%3/3
not defined $_ or ref $_
l!l&&r!l&&!r
2484
MC/DC100%2/2
not defined $_ or ref $_
not defined $_ ref $_
Truth table100%3/3
not(A) or B
A= not defined $_B= ref $_
ABresult
1X1
011
000
313 84 s{ [.]+ \z }{}xms
314 }
315  
316 43 return @args;
317 }
318  
319 #-----------------------------------------------------------------------------
320  
321 26 1;
322  
323 #-----------------------------------------------------------------------------
324  
325 __END__
« lib/Perl/ToPerl6/Utils/POD.pm ↑ Summary lib/Perl/ToPerl6/TransformerConfig.pm »