PPI/Statement/Variable.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 63.6% 14 / 22 Conditioncond 53.3% 8 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 76.5% 75 / 98 CC 17 SCAR 18.9
CC
17
Cov
80%
symbols
10.4
_local_variable
6.2
type
4.6
CRAP 6.6
« PPI/Token/Number/Exp.pm ↑ Summary PPI/Structure/For.pm »
1 package PPI::Statement::Variable;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Statement::Variable - Variable declaration statements
8  
9 =head1 SYNOPSIS
10  
11 # All of the following are variable declarations
12 my $foo = 1;
13 my ($foo, $bar) = (1, 2);
14 our $foo = 1;
15 local $foo;
16 local $foo = 1;
17 LABEL: my $foo = 1;
18  
19 =head1 INHERITANCE
20  
21 PPI::Statement::Variable
22 isa PPI::Statement::Expression
23 isa PPI::Statement
24 isa PPI::Node
25 isa PPI::Element
26  
27 =head1 DESCRIPTION
28  
29 The main intent of the C<PPI::Statement::Variable> class is to describe
30 simple statements that explicitly declare new local or global variables.
31  
32 Note that this does not make it exclusively the only place where variables
33 are defined, and later on you should expect that the C<variables> method
34 will migrate deeper down the tree to either L<PPI::Statement> or
35 L<PPI::Node> to recognise this fact, but for now it stays here.
36  
37 =head1 METHODS
38  
39 =cut
40  
41 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 63 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
43 63 ▶use PPI::Statement::Expression ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 63 our $VERSION = '1.260'; # VERSION
46  
47 63 our @ISA = "PPI::Statement::Expression";
48  
49 =pod
50  
51 =head2 type
52  
53 The C<type> method checks and returns the declaration type of the statement,
54 which will be one of 'my', 'local', 'our', or 'state'.
55  
56 Returns a string of the type, or C<undef> if the type cannot be detected
57 (which is probably a bug).
58  
59 =cut
60  
61 sub type {
62 5 ▶ my $self = shift;
Subroutine type: called CC=4 SCAR=15.2
Pod documented
63  
64 # Get the first significant child
65 5 my @schild = grep { $_->significant } $self->children;
66  
67 # Ignore labels
68 5 ▶ shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label');
Branch: 1 branch
BranchTrueFalseTotal
if &_INSTANCE($schild[0], "PPI::Token::Label") 0 5 5
69  
70 # Get the type
71 5 ▶ (_INSTANCE($schild[0], 'PPI::Token::Word') and $schild[0]->content =~ /^(my|local|our|state)$/)
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($schild[0], 'PPI::Token::Word') && $schild[0]->content =~ /^(my|local|our|state)$/ ? : 5 0 5
Condition33%1/3
&_INSTANCE($schild[0], "PPI::Token::Word") and $schild[0]->content =~ /^(my|local|our|state)$/
!ll&&!rl&&r
005
MC/DC0%0/2
&_INSTANCE($schild[0], "PPI::Token::Word") and $schild[0]->content =~ /^(my|local|our|state)$/
&_INSTANCE($schild[0], "PPI::Token::Word") $schild[0]->content =~ /^(my|local|our|state)$/
Truth table33%1/3
A and B
A= &_INSTANCE($schild[0], "PPI::Token::Word")B= $schild[0]->content =~ /^(my|local|our|state)$/
ABresult
0X0
100
111
72 ? $schild[0]->content
73 : undef;
74 }
75  
76 =pod
77  
78 =head2 variables
79  
80 As for several other PDOM Element types that can declare variables, the
81 C<variables> method returns a list of the canonical forms of the variables
82 defined by the statement.
83  
84 Returns a list of the canonical string forms of variables, or the null list
85 if it is unable to find any variables.
86  
87 =cut
88  
89 sub variables {
90 8 ▶ map { $_->canonical } $_[0]->symbols;
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
91 }
92  
93 =pod
94  
95 =head2 symbols
96  
97 Returns a list of the variables defined by the statement, as
98 L<PPI::Token::Symbol>s.
99  
100 =cut
101  
102 sub symbols {
103 8 ▶ my $self = shift;
Subroutine symbols: called CC=9 SCAR=23.4
Pod documented
104  
105 # Get the children we care about
106 8 my @schild = grep { $_->significant } $self->children;
107 8 ▶ shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label');
Branch: 1 branch
BranchTrueFalseTotal
if &_INSTANCE($schild[0], "PPI::Token::Label") 0 8 8
108  
109 # If the second child is a symbol, return its name
110 8 ▶ if ( _INSTANCE($schild[1], 'PPI::Token::Symbol') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($schild[1], "PPI::Token::Symbol")) 5 3 8
111 5 return $schild[1];
112 }
113  
114 # If it's a list, return as a list
115 3 ▶ if ( _INSTANCE($schild[1], 'PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($schild[1], "PPI::Structure::List")) 3 0 3
116 3 my $Expression = $schild[1]->schild(0);
117 3 ▶ $Expression and
Branch: 1 branch
BranchTrueFalseTotal
$Expression and $Expression->isa("PPI::Statement::Expression") or return () 0 3 3
Condition33%1/3
$Expression and $Expression->isa("PPI::Statement::Expression")
!ll&&!rl&&r
003
MC/DC0%0/2
$Expression and $Expression->isa("PPI::Statement::Expression")
$Expression $Expression->isa("PPI::Statement::Expression")
Truth table33%1/3
A and B
A= $ExpressionB= $Expression->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
118 $Expression->isa('PPI::Statement::Expression') or return ();
119  
120 # my and our are simpler than local
121 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "my" or $self->type eq "our" or $self->type eq "state") 0 1 1
Condition66%4/6
$self->type eq "my" or $self->type eq "our" or $self->type eq "state"
l!l&&r!l&&!r
201
$self->type eq "my" or $self->type eq "our"
l!l&&r!l&&!r
201
MC/DC33%1/3
$self->type eq "my" or $self->type eq "our" or $self->type eq "state"
$self->type eq "my" $self->type eq "our" $self->type eq "state"
Truth table50%2/4
A or B or C
A= $self->type eq "my"B= $self->type eq "our"C= $self->type eq "state"
ABCresult
1XX1
01X1
0011
0000
122 $self->type eq 'my'
123 or
124 $self->type eq 'our'
125 or
126 $self->type eq 'state'
127 ) {
128 return grep {
129 2 $_->isa('PPI::Token::Symbol')
130 } $Expression->schildren;
131 }
132  
133 # Local is much more icky (potentially).
134 # Not that we are actually going to deal with it now,
135 # but having this separate is likely going to be needed
136 # for future bug reports about local() things.
137  
138 # This is a slightly better way to check.
139 return grep {
140 4 $self->_local_variable($_)
141 } grep {
142 1 $_->isa('PPI::Token::Symbol')
143 } $Expression->schildren;
144 }
145  
146 # erm... this is unexpected
147 0 ();
148 }
149  
150 sub _local_variable {
151 4 ▶ my ($self, $el) = @_;
Subroutine _local_variable: called CC=6 SCAR=18.2
152  
153 # The last symbol should be a variable
154 4 ▶ my $n = $el->snext_sibling or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $n = $el->snext_sibling or return 1 1 3 4
155 3 my $p = $el->sprevious_sibling;
156 3 ▶ if ( ! $p or $p eq ',' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p or $p eq ",") 1 2 3
Condition66%2/3
not $p or $p eq ","
l!l&&r!l&&!r
102
MC/DC50%1/2
not $p or $p eq ","
not $p $p eq ","
Truth table66%2/3
not(A) or B
A= not $pB= $p eq ","
ABresult
1X1
011
000
157 # In the middle of a list
158 1 ▶ return 1 if $n eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "," 0 1 1
159  
160 # The first half of an assignment
161 1 ▶ return 1 if $n eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "=" 1 0 1
162 }
163  
164 # Lets say no for know... additional work
165 # should go here.
166 2 return '';
167 }
168  
169 63 1;
170  
171 =pod
172  
173 =head1 TO DO
174  
175 - Write unit tests for this
176  
177 =head1 SUPPORT
178  
179 See the L<support section|PPI/SUPPORT> in the main module.
180  
181 =head1 AUTHOR
182  
183 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
184  
185 =head1 COPYRIGHT
186  
187 Copyright 2001 - 2011 Adam Kennedy.
188  
189 This program is free software; you can redistribute
190 it and/or modify it under the same terms as Perl itself.
191  
192 The full text of the license can be found in the
193 LICENSE file included with this module.
194  
195 =cut
« PPI/Token/Number/Exp.pm ↑ Summary PPI/Structure/For.pm »