lib/Perl/ToPerl6/Transformer/CompoundStatements/RewriteLoops.pm

filter: Statementstmt 61.7% 29 / 47 Branchbran 8.3% 1 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 12 / 15 Podpod 60.0% 3 / 5 totaltotal 53.5% 45 / 84 CC 8 SCAR 22.2
CC
8
Cov
48%
_is_c_style
30.0
_structure_has_semicolon
12.0
__ANON__
2.1
CRAP 9.2
« lib/Perl/ToPerl6.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Instances/RewriteCreation.pm »
1 package Perl::ToPerl6::Transformer::CompoundStatements::RewriteLoops;
2  
3 17 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Perl::ToPerl6::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Perl::ToPerl6::Utils::PPI qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 insert_trailing_whitespace
11 17 };
12  
13 17 ▶use base 'Perl::ToPerl6::Transformer';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #-----------------------------------------------------------------------------
16  
17 17 Readonly::Scalar my $DESC => q{Transform C-style 'for()' to 'loop ()'};
18 17 Readonly::Scalar my $EXPL => q{C-style for() is now 'loop'};
19  
20 #-----------------------------------------------------------------------------
21  
22 38 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
23 27 ▶sub default_necessity { return $NECESSITY_HIGHEST }
Subroutine default_necessity: called CC=1 SCAR=0.0
Pod documented
24 24 ▶sub default_themes { return qw( core ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
25  
26 #-----------------------------------------------------------------------------
27  
28 17 my %conditional = (
29 for => 'loop',
30 foreach => 'loop',
31 );
32  
33 0 ▶sub _structure_has_semicolon {
Subroutine _structure_has_semicolon: not called CC=3 SCAR=24.8
34 0 my ($elem) = @_;
35 0 $elem = $elem->child(1);
36 0 ▶ while ( $elem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 0 0 0
37 0 ▶ return 1 if $elem->isa('PPI::Token::Structure');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Structure") 0 0 0
38 0 $elem = $elem->next_sibling;
39 }
40 0 return;
41 }
42  
43 0 ▶sub _is_c_style {
Subroutine _is_c_style: not called CC=5 SCAR=34.0
44 0 my ($elem) = @_;
45 0 $elem = $elem->child(1);
46 0 ▶ while ( $elem->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->next_sibling) 0 0 0
47 0 ▶ return 1 if $elem->isa('PPI::Statement::Null');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Null") 0 0 0
48 0 ▶ return 1 if $elem->isa('PPI::Statement') and
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement") and _structure_has_semicolon($elem) 0 0 0
Condition0%0/3
$elem->isa("PPI::Statement") and _structure_has_semicolon($elem)
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa("PPI::Statement") and _structure_has_semicolon($elem)
$elem->isa("PPI::Statement") _structure_has_semicolon($elem)
Truth table0%0/3
A and B
A= $elem->isa("PPI::Statement")B= _structure_has_semicolon($elem)
ABresult
0X0
100
111
49 _structure_has_semicolon($elem);
50 0 $elem = $elem->next_sibling;
51 }
52 0 return;
53 }
54  
55 #
56 # There's a tradeoff at work here.
57 #
58 # While the PPI::Structure::For object is the correct type to look for
59 # (specifically, it accurately matches the 'for ( ) ...' construct)
60 # it matches the '(...)' element, not the structure containing the emtire 'for'
61 # block.
62 #
63 # So we search for the PPI::Structure::For object, and when the time comes
64 # to transform the object, we just move our "pointer" up to the parent,
65 # which contains the entire 'for () {}' construct.
66 #
67  
68 5 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.2
Pod documented
69 78 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
70 78 ▶ $_[1]->isa('PPI::Structure::For') and
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Structure::For") and _is_c_style($_[1]) 0 78 78
71 _is_c_style($_[1])
72 }
73 5 }
74  
75 #-----------------------------------------------------------------------------
76  
77 # Note to reader: (after moving $elem up one layer) the structure looks like
78 #
79 # $elem (PPI::Statement::Compound)
80 # \
81 # \ 0 1 # count by child()
82 # \0 1 # count by schild()
83 # +----+
84 # | |
85 # for ( )
86 #
87 # After changing child(0):
88 #
89 # $elem (PPI::Statement::Compound)
90 # \
91 # \ 0 1 # count by child()
92 # \0 1 # count by schild()
93 # +----+
94 # | |
95 # loop ( )
96 #
97 # After inserting ' ' after schild(0):
98 #
99 # $elem (PPI::Statement::Compound)
100 # \
101 # \ 0 1 2 # count by child()
102 # \0 1 # count by schild()
103 # +----+----+
104 # | | |
105 # loop ' ' ( )
106  
107 0 ▶sub transform {
Subroutine transform: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ($self, $elem, $doc) = @_;
109 0 $elem = $elem->parent;
110  
111 0 $elem->schild(0)->set_content('loop');
112  
113 0 insert_trailing_whitespace($elem->schild(0));
114  
115 0 return $self->transformation( $DESC, $EXPL, $elem );
116 }
117  
118 17 1;
119  
120 #-----------------------------------------------------------------------------
121  
122 __END__
« lib/Perl/ToPerl6.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Instances/RewriteCreation.pm »