PPI/Statement/Compound.pm

filter: Statementstmt 75.0% 30 / 40 Branchbran 55.2% 21 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 67.0% 57 / 85 CC 20 SCAR 34.0
CC
20
Cov
65%
_complete
72.0
type
13.1
BEGIN
1.0
CRAP 30.0
« PPI/Lexer.pm ↑ Summary PPI/Find.pm »
1 package PPI::Statement::Compound;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Statement::Compound - Describes all compound statements
8  
9 =head1 SYNOPSIS
10  
11 # A compound if statement
12 if ( foo ) {
13 bar();
14 } else {
15 baz();
16 }
17  
18 # A compound loop statement
19 foreach ( @list ) {
20 bar($_);
21 }
22  
23 =head1 INHERITANCE
24  
25 PPI::Statement::Compound
26 isa PPI::Statement
27 isa PPI::Node
28 isa PPI::Element
29  
30 =head1 DESCRIPTION
31  
32 C<PPI::Statement::Compound> objects are used to describe all current forms
33 of compound statements, as described in L<perlsyn>.
34  
35 This covers blocks using C<if>, C<unless>, C<for>, C<foreach>, C<while>,
36 and C<continue>. Please note this does B<not> cover "simple" statements
37 with trailing conditions. Please note also that "do" is also not part of
38 a compound statement.
39  
40 # This is NOT a compound statement
41 my $foo = 1 if $condition;
42  
43 # This is also not a compound statement
44 do { ... } until $condition;
45  
46 =head1 METHODS
47  
48 C<PPI::Statement::Compound> has a number of methods in addition to the
49 standard L<PPI::Statement>, L<PPI::Node> and L<PPI::Element> methods.
50  
51 =cut
52  
53 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 62 ▶use PPI::Statement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 62 ▶use vars qw{$VERSION @ISA %TYPES};
Subroutine BEGIN: called CC=1 SCAR=0.0
57 BEGIN {
58 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 62 @ISA = 'PPI::Statement';
60  
61 # Keyword type map
62 62 %TYPES = (
63 'if' => 'if',
64 'unless' => 'if',
65 'while' => 'while',
66 'until' => 'while',
67 'for' => 'for',
68 'foreach' => 'foreach',
69 );
70 }
71  
72 # Lexer clues
73 sub __LEXER__normal() { '' }
74  
75  
76  
77  
78  
79 #####################################################################
80 # PPI::Statement::Compound analysis methods
81  
82 =pod
83  
84 =head2 type
85  
86 The C<type> method returns the syntactic type of the compound statement.
87  
88 There are four basic compound statement types.
89  
90 The C<'if'> type includes all variations of the if and unless statements,
91 including any C<'elsif'> or C<'else'> parts of the compound statement.
92  
93 The C<'while'> type describes the standard while and until statements, but
94 again does B<not> describes simple statements with a trailing while.
95  
96 The C<'for'> type covers the C-style for loops, regardless of whether they
97 were declared using C<'for'> or C<'foreach'>.
98  
99 The C<'foreach'> type covers loops that iterate over collections,
100 regardless of whether they were declared using C<'for'> or C<'foreach'>.
101  
102 All of the compounds are a variation on one of these four.
103  
104 Returns the simple string C<'if'>, C<'for'>, C<'foreach'> or C<'while'>,
105 or C<undef> if the type cannot be determined.
106  
107 =cut
108  
109 sub type {
110 5992 ▶ my $self = shift;
Subroutine type: called CC=13 SCAR=25.8
Pod documented
111 5992 my $p = 0; # Child position
112 5992 ▶ my $Element = $self->schild($p) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = $self->schild($p) or return undef 0 5992 5992
113  
114 # A labelled statement
115 5992 ▶ if ( $Element->isa('PPI::Token::Label') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Token::Label")) 644 5348 5992
116 644 ▶ $Element = $self->schild(++$p) or return 'label';
Branch: 1 branch
BranchTrueFalseTotal
$Element = $self->schild(++$p) or return "label" 317 327 644
117 }
118  
119 # Most simple cases
120 5675 my $content = $Element->content;
121 5675 ▶ if ( $content =~ /^for(?:each)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^for(?:each)?\z/) 1381 4294 5675
122 1381 ▶ $Element = $self->schild(++$p) or return $content;
Branch: 1 branch
BranchTrueFalseTotal
$Element = $self->schild(++$p) or return $content 301 1080 1381
123 1080 ▶ if ( $Element->isa('PPI::Token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Token")) 920 160 1080
124 920 ▶ return 'foreach' if $Element->content =~ /^my|our|state\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $Element->content =~ /^my|our|state\z/ 779 141 920
125 141 ▶ return 'foreach' if $Element->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::Symbol") 115 26 141
126 26 ▶ return 'foreach' if $Element->isa('PPI::Token::QuoteLike::Words');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::QuoteLike::Words") 26 0 26
127 }
128 160 ▶ if ( $Element->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Structure::List")) 115 45 160
129 115 return 'foreach';
130 }
131 45 return 'for';
132 }
133 4294 ▶ return $TYPES{$content} if $Element->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::Word") 4147 147 4294
134 147 ▶ return 'continue' if $Element->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Structure::Block") 147 0 147
135  
136 # Unknown (shouldn't exist?)
137 0 undef;
138 }
139  
140  
141  
142  
143  
144 #####################################################################
145 # PPI::Node Methods
146  
147 sub scope() { 1 }
148  
149  
150  
151  
152  
153 #####################################################################
154 # PPI::Element Methods
155  
156 sub _complete {
157 0 ▶ my $self = shift;
Subroutine _complete: not called CC=8 SCAR=42.8
158 0 ▶ my $type = $self->type or die "Illegal compound statement type";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->type or die "Illegal compound statement type" 0 0 0
159  
160 # Check the different types of compound statements
161 0 ▶ if ( $type eq 'if' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'if') { } 0 0 0
elsif ($type eq 'while') { } 0 0 0
162 # Unless the last significant child is a complete
163 # block, it must be incomplete.
164 0 ▶ my $child = $self->schild(-1) or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->schild(-1) or return "" 0 0 0
165 0 ▶ $child->isa('PPI::Structure') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->isa("PPI::Structure") or return "" 0 0 0
166 0 ▶ $child->braces eq '{}' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->braces eq "{}" or return "" 0 0 0
167 0 ▶ $child->_complete or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->_complete or return "" 0 0 0
168  
169 # It can STILL be
170 } elsif ( $type eq 'while' ) {
171 0 die "CODE INCOMPLETE";
172 } else {
173 0 die "CODE INCOMPLETE";
174 }
175 }
176  
177 62 1;
178  
179 =pod
180  
181 =head1 TO DO
182  
183 - Write unit tests for this package
184  
185 =head1 SUPPORT
186  
187 See the L<support section|PPI/SUPPORT> in the main module.
188  
189 =head1 AUTHOR
190  
191 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
192  
193 =head1 COPYRIGHT
194  
195 Copyright 2001 - 2011 Adam Kennedy.
196  
197 This program is free software; you can redistribute
198 it and/or modify it under the same terms as Perl itself.
199  
200 The full text of the license can be found in the
201 LICENSE file included with this module.
202  
203 =cut
« PPI/Lexer.pm ↑ Summary PPI/Find.pm »