PPI/Statement/Compound.pm

filter: Statementstmt 72.9% 27 / 37 Branchbran 55.2% 21 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 65.0% 52 / 80 CC 20 SCAR 34.8
CC
20
Cov
64%
_complete
72.0
type
13.1
BEGIN
1.0
CRAP 32.6
« PPI/Lexer.pm ↑ Summary PPI/Transform/UpdateCopyright.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 63 ▶use PPI::Statement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 63 our $VERSION = '1.260'; # VERSION
57  
58 63 our @ISA = "PPI::Statement";
59  
60 # Keyword type map
61 63 my %TYPES = (
62 'if' => 'if',
63 'unless' => 'if',
64 'while' => 'while',
65 'until' => 'while',
66 'for' => 'for',
67 'foreach' => 'foreach',
68 );
69  
70 # Lexer clues
71 sub __LEXER__normal() { '' }
72  
73  
74  
75  
76  
77 #####################################################################
78 # PPI::Statement::Compound analysis methods
79  
80 =pod
81  
82 =head2 type
83  
84 The C<type> method returns the syntactic type of the compound statement.
85  
86 There are four basic compound statement types.
87  
88 The C<'if'> type includes all variations of the if and unless statements,
89 including any C<'elsif'> or C<'else'> parts of the compound statement.
90  
91 The C<'while'> type describes the standard while and until statements, but
92 again does B<not> describes simple statements with a trailing while.
93  
94 The C<'for'> type covers the C-style for loops, regardless of whether they
95 were declared using C<'for'> or C<'foreach'>.
96  
97 The C<'foreach'> type covers loops that iterate over collections,
98 regardless of whether they were declared using C<'for'> or C<'foreach'>.
99  
100 All of the compounds are a variation on one of these four.
101  
102 Returns the simple string C<'if'>, C<'for'>, C<'foreach'> or C<'while'>,
103 or C<undef> if the type cannot be determined.
104  
105 =cut
106  
107 sub type {
108 6186 ▶ my $self = shift;
Subroutine type: called CC=13 SCAR=25.8
Pod documented
109 6186 my $p = 0; # Child position
110 6186 ▶ my $Element = $self->schild($p) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = $self->schild($p) or return undef 0 6186 6186
111  
112 # A labelled statement
113 6186 ▶ if ( $Element->isa('PPI::Token::Label') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Token::Label")) 656 5530 6186
114 656 ▶ $Element = $self->schild(++$p) or return 'label';
Branch: 1 branch
BranchTrueFalseTotal
$Element = $self->schild(++$p) or return "label" 325 331 656
115 }
116  
117 # Most simple cases
118 5861 my $content = $Element->content;
119 5861 ▶ if ( $content =~ /^for(?:each)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^for(?:each)?\z/) 1405 4456 5861
120 1405 ▶ $Element = $self->schild(++$p) or return $content;
Branch: 1 branch
BranchTrueFalseTotal
$Element = $self->schild(++$p) or return $content 302 1103 1405
121 1103 ▶ if ( $Element->isa('PPI::Token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Token")) 966 137 1103
122 966 ▶ return 'foreach' if $Element->content =~ /^my|our|state\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $Element->content =~ /^my|our|state\z/ 825 141 966
123 141 ▶ return 'foreach' if $Element->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::Symbol") 115 26 141
124 26 ▶ return 'foreach' if $Element->isa('PPI::Token::QuoteLike::Words');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::QuoteLike::Words") 26 0 26
125 }
126 137 ▶ if ( $Element->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("PPI::Structure::List")) 86 51 137
127 86 return 'foreach';
128 }
129 51 return 'for';
130 }
131 4456 ▶ return $TYPES{$content} if $Element->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Token::Word") 4305 151 4456
132 151 ▶ return 'continue' if $Element->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $Element->isa("PPI::Structure::Block") 151 0 151
133  
134 # Unknown (shouldn't exist?)
135 0 undef;
136 }
137  
138  
139  
140  
141  
142 #####################################################################
143 # PPI::Node Methods
144  
145 sub scope() { 1 }
146  
147  
148  
149  
150  
151 #####################################################################
152 # PPI::Element Methods
153  
154 sub _complete {
155 0 ▶ my $self = shift;
Subroutine _complete: not called CC=8 SCAR=42.8
156 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
157  
158 # Check the different types of compound statements
159 0 ▶ if ( $type eq 'if' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'if') { } 0 0 0
elsif ($type eq 'while') { } 0 0 0
160 # Unless the last significant child is a complete
161 # block, it must be incomplete.
162 0 ▶ my $child = $self->schild(-1) or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->schild(-1) or return "" 0 0 0
163 0 ▶ $child->isa('PPI::Structure') or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->isa("PPI::Structure") or return "" 0 0 0
164 0 ▶ $child->braces eq '{}' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->braces eq "{}" or return "" 0 0 0
165 0 ▶ $child->_complete or return '';
Branch: 1 branch
BranchTrueFalseTotal
$child->_complete or return "" 0 0 0
166  
167 # It can STILL be
168 } elsif ( $type eq 'while' ) {
169 0 die "CODE INCOMPLETE";
170 } else {
171 0 die "CODE INCOMPLETE";
172 }
173 }
174  
175 63 1;
176  
177 =pod
178  
179 =head1 TO DO
180  
181 - Write unit tests for this package
182  
183 =head1 SUPPORT
184  
185 See the L<support section|PPI/SUPPORT> in the main module.
186  
187 =head1 AUTHOR
188  
189 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
190  
191 =head1 COPYRIGHT
192  
193 Copyright 2001 - 2011 Adam Kennedy.
194  
195 This program is free software; you can redistribute
196 it and/or modify it under the same terms as Perl itself.
197  
198 The full text of the license can be found in the
199 LICENSE file included with this module.
200  
201 =cut
« PPI/Lexer.pm ↑ Summary PPI/Transform/UpdateCopyright.pm »