PPI/Statement/Sub.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 75.0% 9 / 12 Conditioncond 33.3% 4 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 76.7% 66 / 86 CC 11 SCAR 13.4
CC
11
Cov
77%
_complete
12.0
name
5.4
block
3.0
CRAP 3.8
« PPI/Statement/Include.pm ↑ Summary PPI/Util.pm »
1 package PPI::Statement::Sub;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Statement::Sub - Subroutine declaration
8  
9 =head1 INHERITANCE
10  
11 PPI::Statement::Sub
12 isa PPI::Statement
13 isa PPI::Node
14 isa PPI::Element
15  
16 =head1 DESCRIPTION
17  
18 Except for the special BEGIN, CHECK, UNITCHECK, INIT, and END subroutines
19 (which are part of L<PPI::Statement::Scheduled>) all subroutine declarations
20 are lexed as a PPI::Statement::Sub object.
21  
22 Primarily, this means all of the various C<sub foo {}> statements, but also
23 forward declarations such as C<sub foo;> or C<sub foo($);>. It B<does not>
24 include anonymous subroutines, as these are merely part of a normal statement.
25  
26 =head1 METHODS
27  
28 C<PPI::Statement::Sub> has a number of methods in addition to the standard
29 L<PPI::Statement>, L<PPI::Node> and L<PPI::Element> methods.
30  
31 =cut
32  
33 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 62 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
36 62 ▶use PPI::Statement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
39 BEGIN {
40 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 62 @ISA = 'PPI::Statement';
42 }
43  
44 # Lexer clue
45 sub __LEXER__normal() { '' }
46  
47 sub _complete {
48 0 ▶ my $child = $_[0]->schild(-1);
Subroutine _complete: not called CC=3 SCAR=24.8
49 return !! (
50 0 ▶ defined $child
Condition0%0/6
defined $child && $child->isa('PPI::Structure::Block') && $child->complete
!ll&&!rl&&r
000
defined $child and $child->isa("PPI::Structure::Block")
!ll&&!rl&&r
000
MC/DC0%0/3
defined $child && $child->isa('PPI::Structure::Block') && $child->complete
defined $child $child->isa("PPI::Structure::Block") $child->complete
Truth table0%0/4
A and B && C
A= defined $childB= $child->isa("PPI::Structure::Block")C= $child->complete
ABCresult
0XX0
10X0
1100
1111
51 and
52 $child->isa('PPI::Structure::Block')
53 and
54 $child->complete
55 );
56 }
57  
58  
59  
60  
61  
62 #####################################################################
63 # PPI::Statement::Sub Methods
64  
65 =pod
66  
67 =head2 name
68  
69 The C<name> method returns the name of the subroutine being declared.
70  
71 In some rare cases such as a naked C<sub> at the end of the file, this may return
72 false.
73  
74 =cut
75  
76 sub name {
77 52 ▶ my ($self) = @_;
Subroutine name: called CC=5 SCAR=16.8
Pod documented
78  
79 # Usually the second token is the name.
80 52 my $token = $self->schild(1);
81 52 ▶ return $token->content
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and $token->isa("PPI::Token::Word") 36 16 52
Condition100%3/3
defined $token and $token->isa("PPI::Token::Word")
!ll&&!rl&&r
41236
MC/DC100%2/2
defined $token and $token->isa("PPI::Token::Word")
defined $token $token->isa("PPI::Token::Word")
Truth table100%3/3
A and B
A= defined $tokenB= $token->isa("PPI::Token::Word")
ABresult
0X0
100
111
82 if defined $token and $token->isa('PPI::Token::Word');
83  
84 # In the case of special subs whose 'sub' can be omitted (AUTOLOAD
85 # or DESTROY), the name will be the first token.
86 16 $token = $self->schild(0);
87 16 ▶ return $token->content
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and $token->isa("PPI::Token::Word") 16 0 16
Condition33%1/3
defined $token and $token->isa("PPI::Token::Word")
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $token and $token->isa("PPI::Token::Word")
defined $token $token->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= defined $tokenB= $token->isa("PPI::Token::Word")
ABresult
0X0
100
111
88 if defined $token and $token->isa('PPI::Token::Word');
89 0 return '';
90 }
91  
92 =pod
93  
94 =head2 prototype
95  
96 If it has one, the C<prototype> method returns the subroutine's prototype.
97 It is returned in the same format as L<PPI::Token::Prototype/prototype>,
98 cleaned and removed from its brackets.
99  
100 Returns the subroutine's prototype, or undef if the subroutine does not
101 define one. Note that when the sub has an empty prototype (C<()>) the
102 return is an empty string.
103  
104 =cut
105  
106 sub prototype {
107 3 ▶ my $self = shift;
Subroutine prototype: called CC=2 SCAR=6.9
Pod documented
108 my $Prototype = List::Util::first {
109 15 ▶ _INSTANCE($_, 'PPI::Token::Prototype')
Subroutine __ANON__: called CC=1 SCAR=0.0
110 3 } $self->children;
111 3 ▶ defined($Prototype) ? $Prototype->prototype : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Prototype ? : 2 1 3
112 }
113  
114 =pod
115  
116 =head2 block
117  
118 With its name and implementation shared with L<PPI::Statement::Scheduled>,
119 the C<block> method finds and returns the actual Structure object of the
120 code block for this subroutine.
121  
122 Returns false if this is a forward declaration, or otherwise does not have a
123 code block.
124  
125 =cut
126  
127 sub block {
128 71 ▶ my $self = shift;
Subroutine block: called CC=3 SCAR=11.1
Pod documented
129 71 ▶ my $lastchild = $self->schild(-1) or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $lastchild = $self->schild(-1) or return "" 0 71 71
130 71 ▶ $lastchild->isa('PPI::Structure::Block') and $lastchild;
Branch: 1 branch
BranchTrueFalseTotal
$lastchild->isa("PPI::Structure::Block") and $lastchild 37 34 71
131 }
132  
133 =pod
134  
135 =head2 forward
136  
137 The C<forward> method returns true if the subroutine declaration is a
138 forward declaration.
139  
140 That is, it returns false if the subroutine has a code block, or true
141 if it does not.
142  
143 =cut
144  
145 sub forward {
146 5 ▶ ! shift->block;
Subroutine forward: called CC=1 SCAR=0.0
Pod documented
147 }
148  
149 =pod
150  
151 =head2 reserved
152  
153 The C<reserved> method provides a convenience method for checking to see
154 if this is a special reserved subroutine. It does not check against any
155 particular list of reserved sub names, but just returns true if the name
156 is all uppercase, as defined in L<perlsub>.
157  
158 Note that in the case of BEGIN, CHECK, UNITCHECK, INIT and END, these will be
159 defined as L<PPI::Statement::Scheduled> objects, not subroutines.
160  
161 Returns true if it is a special reserved subroutine, or false if not.
162  
163 =cut
164  
165 sub reserved {
166 68 ▶ my $self = shift;
Subroutine reserved: called CC=2 SCAR=7.1
Pod documented
167 68 ▶ my $name = $self->name or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->name or return "" 0 68 68
168 # perlsub is silent on whether reserveds can contain:
169 # - underscores;
170 # we allow them due to existing practice like CLONE_SKIP and __SUB__.
171 # - numbers; we allow them by PPI tradition.
172 68 $name eq uc $name;
173 }
174  
175 62 1;
176  
177 =pod
178  
179 =head1 SUPPORT
180  
181 See the L<support section|PPI/SUPPORT> in the main module.
182  
183 =head1 AUTHOR
184  
185 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
186  
187 =head1 COPYRIGHT
188  
189 Copyright 2001 - 2011 Adam Kennedy.
190  
191 This program is free software; you can redistribute
192 it and/or modify it under the same terms as Perl itself.
193  
194 The full text of the license can be found in the
195 LICENSE file included with this module.
196  
197 =cut
« PPI/Statement/Include.pm ↑ Summary PPI/Util.pm »