lib/Perl/ToPerl6/Transformer/ModuleSpecific/Moose.pm

filter: Statementstmt 31.6% 31 / 98 Branchbran 5.0% 2 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 73.6% 14 / 19 Podpod 30.0% 3 / 10 totaltotal 24.7% 50 / 202 CC 29 SCAR 45.9
CC
29
Cov
21%
moose_has_attribute
272.0
make_perl6_attribute
30.0
ppi_is_fat_comma
12.0
CRAP 98.8
« lib/Perl/ToPerl6/TestUtils.pm ↑ Summary lib/Perl/ToPerl6/Command.pm »
1 package Perl::ToPerl6::Transformer::ModuleSpecific::Moose;
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 Moose attributes to Perl6};
18 17 Readonly::Scalar my $EXPL => q{Transform Moose attributes to Perl6};
19  
20 #-----------------------------------------------------------------------------
21  
22 31 ▶sub run_before { return 'Operators::FormatOperators' }
Subroutine run_before: called CC=1 SCAR=0.0
Pod undocumented
23 38 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 29 ▶sub default_necessity { return $NECESSITY_HIGHEST }
Subroutine default_necessity: called CC=1 SCAR=0.0
Pod documented
25 26 ▶sub default_themes { return qw( tweaks ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
26 4 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27  
28 #-----------------------------------------------------------------------------
29  
30 0 ▶sub ppi_is_fat_comma {
Subroutine ppi_is_fat_comma: not called CC=3 SCAR=24.8
Pod undocumented
31 0 ▶ $_[1] and
Branch: 1 branch
BranchTrueFalseTotal
$_[1] and $_[1]->isa("PPI::Token::Operator") and $_[1]->content eq "=>" 0 0 0
Condition0%0/3
$_[1] and $_[1]->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1] and $_[1]->isa("PPI::Token::Operator")
$_[1] $_[1]->isa("PPI::Token::Operator")
Truth table0%0/3
A and B
A= $_[1]B= $_[1]->isa("PPI::Token::Operator")
ABresult
0X0
100
111
32 $_[1]->isa('PPI::Token::Operator') and
33 $_[1]->content eq '=>';
34 }
35  
36 0 ▶sub ppi_is_comma {
Subroutine ppi_is_comma: not called CC=3 SCAR=24.8
Pod undocumented
37 0 ▶ $_[1] and
Branch: 1 branch
BranchTrueFalseTotal
$_[1] and $_[1]->isa("PPI::Token::Operator") and $_[1]->content eq "," 0 0 0
Condition0%0/3
$_[1] and $_[1]->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1] and $_[1]->isa("PPI::Token::Operator")
$_[1] $_[1]->isa("PPI::Token::Operator")
Truth table0%0/3
A and B
A= $_[1]B= $_[1]->isa("PPI::Token::Operator")
ABresult
0X0
100
111
38 $_[1]->isa('PPI::Token::Operator') and
39 $_[1]->content eq ',';
40 }
41  
42 0 ▶sub moose_has_attribute {
Subroutine moose_has_attribute: not called CC=16 SCAR=56.1
Pod undocumented
43 0 my ($elem) = @_;
44 0 my $head = $elem;
45  
46 # In the string that follows, (( .. )) is not present in the code, but
47 # represents the nesting of an expression inside a list that PPI does.
48  
49 # ----V
50 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
51 #
52 0 $head = $head->snext_sibling;
53  
54 # --------V
55 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
56 # C<< has 'x' => (( is => 'rw', isa => 'Int' )) >>
57 #
58 0 my $name;
59 0 ▶ if ( $head->isa('PPI::Token::Word') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($head->isa('PPI::Token::Word')) { } 0 0 0
elsif ($head->isa('PPI::Token::Quote')) { } 0 0 0
60 0 $name = $head->content;
61 0 $head = $head->snext_sibling;
62 }
63 elsif ( $head->isa('PPI::Token::Quote') ) {
64 0 $name = $head->string;
65 0 $head = $head->snext_sibling;
66 }
67 else {
68 0 return;
69 }
70  
71 0 my $attributes;
72  
73 # ----------V
74 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
75 # C<< has x , (( is => 'rw', isa => 'Int' )) >>
76 #
77 0 ▶ return unless ppi_is_fat_comma(undef,$head) or
Branch: 1 branch
BranchTrueFalseTotal
unless ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head) 0 0 0
Condition0%0/3
ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head)
l!l&&r!l&&!r
000
MC/DC0%0/2
ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head)
ppi_is_fat_comma(undef, $head) ppi_is_comma(undef, $head)
Truth table0%0/3
A or B
A= ppi_is_fat_comma(undef, $head)B= ppi_is_comma(undef, $head)
ABresult
1X1
011
000
78 ppi_is_comma(undef,$head);
79 0 $head = $head->snext_sibling;
80  
81 # -------------V
82 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
83 #
84 0 ▶ if ( $head->isa('PPI::Structure::List') and
Branch: 1 branch
BranchTrueFalseTotal
if ($head->isa("PPI::Structure::List") and $head->start->content eq "(") 0 0 0
Condition0%0/3
$head->isa("PPI::Structure::List") and $head->start->content eq "("
!ll&&!rl&&r
000
MC/DC0%0/2
$head->isa("PPI::Structure::List") and $head->start->content eq "("
$head->isa("PPI::Structure::List") $head->start->content eq "("
Truth table0%0/3
A and B
A= $head->isa("PPI::Structure::List")B= $head->start->content eq "("
ABresult
0X0
100
111
85 $head->start->content eq '(' ) {
86 0 $head = $head->schild(0);
87 }
88  
89 # --------------V
90 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
91 #
92 0 ▶ if ( $head->isa('PPI::Statement::Expression') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->isa("PPI::Statement::Expression")) 0 0 0
93 0 $head = $head->schild(0);
94 }
95  
96 0 ▶ while ( $head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head) 0 0 0
97 # ----------------V
98 # C<< has x => (( is => 'rw' )) >>
99 # C<< has x => (( 'is' , 'rw' )) >>
100 #
101 0 my $key;
102 0 ▶ if ( $head->isa('PPI::Token::Word') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($head->isa('PPI::Token::Word')) { } 0 0 0
elsif ($head->isa('PPI::Token::Quote')) { } 0 0 0
103 0 $key = $head->content;
104 0 $head = $head->snext_sibling;
105 }
106 elsif ( $head->isa('PPI::Token::Quote') ) {
107 0 $key = $head->string;
108 0 $head = $head->snext_sibling;
109 }
110 else {
111 0 warn "Unknown term >" . $head->content . "< found while processing Moose attribute, please tell the author.";
112 0 return;
113 }
114  
115 # -------------------V
116 # C<< has x => (( is => 'rw' )) >>
117 # C<< has x => (( 'is' , 'rw' )) >>
118 #
119 0 ▶ return unless ppi_is_fat_comma(undef,$head) or
Branch: 1 branch
BranchTrueFalseTotal
unless ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head) 0 0 0
Condition0%0/3
ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head)
l!l&&r!l&&!r
000
MC/DC0%0/2
ppi_is_fat_comma(undef, $head) or ppi_is_comma(undef, $head)
ppi_is_fat_comma(undef, $head) ppi_is_comma(undef, $head)
Truth table0%0/3
A or B
A= ppi_is_fat_comma(undef, $head)B= ppi_is_comma(undef, $head)
ABresult
1X1
011
000
120 ppi_is_comma(undef,$head);
121 0 $head = $head->snext_sibling;
122  
123 # ----------------------V
124 # C<< has x => (( is => 'rw' )) >>
125 #
126 0 ▶ if ( $key eq 'default' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'default') { } 0 0 0
127 0 $attributes->{$key} = $head->clone;
128 }
129 else {
130 0 $attributes->{$key} = $head->string;
131 }
132 0 $head = $head->snext_sibling;
133  
134 # --------------------------V
135 # C<< has x => (( is => 'rw', isa => 'Int' )) >>
136 #
137 0 ▶ last unless ppi_is_comma(undef,$head) or
Branch: 1 branch
BranchTrueFalseTotal
unless ppi_is_comma(undef, $head) or ppi_is_fat_comma(undef, $head) 0 0 0
Condition0%0/3
ppi_is_comma(undef, $head) or ppi_is_fat_comma(undef, $head)
l!l&&r!l&&!r
000
MC/DC0%0/2
ppi_is_comma(undef, $head) or ppi_is_fat_comma(undef, $head)
ppi_is_comma(undef, $head) ppi_is_fat_comma(undef, $head)
Truth table0%0/3
A or B
A= ppi_is_comma(undef, $head)B= ppi_is_fat_comma(undef, $head)
ABresult
1X1
011
000
138 ppi_is_fat_comma(undef,$head);
139 0 $head = $head->snext_sibling;
140 }
141  
142 0 return ( $name, $attributes );
143 }
144  
145 0 ▶sub make_perl6_attribute {
Subroutine make_perl6_attribute: not called CC=5 SCAR=34.0
Pod undocumented
146 0 my ($name, $attributes) = @_;
147 0 my $statement = PPI::Statement->new;
148 0 $statement->add_element(
149 PPI::Token::Word->new('has')
150 );
151  
152 0 ▶ if ( $attributes->{isa} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"isa"}) 0 0 0
153 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
154 $statement->add_element(
155 PPI::Token::Word->new( $attributes->{isa} )
156 0 );
157 }
158  
159 # Insert '$.<attribute name>'
160 #
161 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
162 0 $statement->add_element(
163 PPI::Token::Symbol->new('$.' . $name)
164 );
165  
166 # If we have a read-write attribute flag, add that here.
167 #
168 0 ▶ if ( $attributes->{is} and $attributes->{is} eq 'rw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"is"} and $attributes->{"is"} eq "rw") 0 0 0
Condition0%0/3
$attributes->{"is"} and $attributes->{"is"} eq "rw"
!ll&&!rl&&r
000
MC/DC0%0/2
$attributes->{"is"} and $attributes->{"is"} eq "rw"
$attributes->{"is"} $attributes->{"is"} eq "rw"
Truth table0%0/3
A and B
A= $attributes->{"is"}B= $attributes->{"is"} eq "rw"
ABresult
0X0
100
111
169 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
170 0 $statement->add_element(
171 PPI::Token::Word->new('is')
172 );
173 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
174 0 $statement->add_element(
175 PPI::Token::Word->new('rw')
176 );
177 }
178  
179 # If we have a default attribute, add that here.
180 #
181 0 ▶ if ( $attributes->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"default"}) 0 0 0
182 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
183 0 $statement->add_element(
184 PPI::Token::Operator->new('=')
185 );
186 0 $statement->add_element( PPI::Token::Whitespace->new(' ') );
187 0 $statement->add_element( $attributes->{default} );
188  
189 }
190 $statement->add_element(
191 0 PPI::Token::Structure->new(';')
192 );
193 0 $statement->add_element( PPI::Token::Whitespace->new("\n") );
194  
195 0 return $statement;
196 }
197  
198 4 ▶sub transform {
Subroutine transform: called CC=4 SCAR=23.7
Pod undocumented
199 4 my ($self, $elem, $doc) = @_;
200 32 ▶ return unless $doc->find( sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
201 32 ▶ $_[1]->isa('PPI::Statement::Include') and
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and $_[1]->schild(1)->content =~ / ^ Moose /x 0 32 32
202 $_[1]->schild(1)->content =~ m< ^ Moose >x
203 4 ▶ } );
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->find(sub { $_[1]->isa("PPI::Statement::Include") and $_[1]->schild(1)->content =~ / ^ Moose /x; } ) 4 0 4
204  
205 0 ▶ my $attribute_keywords = $doc->find( sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
206 0 ▶ $_[1]->isa('PPI::Token::Word') and
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "has" 0 0 0
207 $_[1]->content eq 'has'
208 0 } );
209  
210 0 for my $has ( @$attribute_keywords ) {
211 0 my ( $name, $attributes ) = moose_has_attribute( $has );
212 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
213  
214 0 my $attribute = make_perl6_attribute( $name, $attributes );
215 0 $has->parent->insert_after( $attribute );
216 0 $has->parent->insert_after(
217 PPI::Token::Whitespace->new("\n")
218 );
219 }
220  
221 0 return $self->transformation( $DESC, $EXPL, $elem );
222 }
223  
224 17 1;
225  
226 #-----------------------------------------------------------------------------
227  
228 __END__
« lib/Perl/ToPerl6/TestUtils.pm ↑ Summary lib/Perl/ToPerl6/Command.pm »