PPI/Token/Attribute.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 94.4% 17 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 97.1% 69 / 71 CC 10 SCAR 12.1
CC
10
Cov
97%
__TOKENIZER__scan_for_end
6.0
__TOKENIZER__on_char
3.0
identifier
2.0
CRAP 3.4
« PPI/Statement/Package.pm ↑ Summary PPI/Token/Number/Octal.pm »
1 package PPI::Token::Attribute;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Token::Attribute - A token for a subroutine attribute
8  
9 =head1 INHERITANCE
10  
11 PPI::Token::Attribute
12 isa PPI::Token
13 isa PPI::Element
14  
15 =head1 DESCRIPTION
16  
17 In Perl, attributes are a relatively recent addition to the language.
18  
19 Given the code C< sub foo : bar(something) {} >, the C<bar(something)>
20 part is the attribute.
21  
22 A C<PPI::Token::Attribute> token represents the entire of the attribute,
23 as the braces and its contents are not parsed into the tree, and are
24 treated by Perl (and thus by us) as a single string.
25  
26 =head1 METHODS
27  
28 This class provides some additional methods beyond those provided by its
29 L<PPI::Token> and L<PPI::Element> parent classes.
30  
31 =cut
32  
33 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 62 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
37 BEGIN {
38 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 62 @ISA = 'PPI::Token';
40 }
41  
42  
43  
44  
45 #####################################################################
46 # PPI::Token::Attribute Methods
47  
48 =pod
49  
50 =head2 identifier
51  
52 The C<identifier> attribute returns the identifier part of the attribute.
53  
54 That is, for the attribute C<foo(bar)>, the C<identifier> method would
55 return C<"foo">.
56  
57 =cut
58  
59 sub identifier {
60 684 ▶ my $self = shift;
Subroutine identifier: called CC=2 SCAR=6.9
Pod documented
61 684 ▶ $self->{content} =~ /^(.+?)\(/ ? $1 : $self->{content};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'content'} =~ /^(.+?)\(/ ? : 310 374 684
62 }
63  
64 =pod
65  
66 =head2 parameters
67  
68 The C<parameters> method returns the parameter string for the attribute.
69  
70 That is, for the attribute C<foo(bar)>, the C<parameters> method would
71 return C<"bar">.
72  
73 Returns the parameters as a string (including the null string C<''> for
74 the case of an attribute such as C<foo()>.)
75  
76 Returns C<undef> if the attribute does not have parameters.
77  
78 =cut
79  
80 sub parameters {
81 684 ▶ my $self = shift;
Subroutine parameters: called CC=2 SCAR=6.9
Pod documented
82 684 ▶ $self->{content} =~ /\((.*)\)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'content'} =~ /\((.*)\)$/ ? : 310 374 684
83 }
84  
85  
86  
87  
88  
89 #####################################################################
90 # Tokenizer Methods
91  
92 sub __TOKENIZER__on_char {
93 1062 ▶ my $class = shift;
Subroutine __TOKENIZER__on_char: called CC=3 SCAR=11.0
94 1062 my $t = shift;
95 1062 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
96  
97 # Unless this is a '(', we are finished.
98 1062 ▶ unless ( $char eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($char eq "(") 591 471 1062
99 # Finalise and recheck
100 591 return $t->_finalize_token->__TOKENIZER__on_char( $t );
101 }
102  
103 # This is a bar(...) style attribute.
104 # We are currently on the ( so scan in until the end.
105 # We finish on the character AFTER our end
106 471 my $string = $class->__TOKENIZER__scan_for_end( $t );
107 471 ▶ if ( ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 24 447 471
108 # EOF
109 24 $t->{token}->{content} .= $$string;
110 24 $t->_finalize_token;
111 24 return 0;
112 }
113  
114 # Found the end of the attribute
115 447 $t->{token}->{content} .= $string;
116 447 $t->_finalize_token->__TOKENIZER__on_char( $t );
117 }
118  
119 # Scan for a close braced, and take into account both escaping,
120 # and open close bracket pairs in the string. When complete, the
121 # method leaves the line cursor on the LAST character found.
122 sub __TOKENIZER__scan_for_end {
123 471 ▶ my $t = $_[1];
Subroutine __TOKENIZER__scan_for_end: called CC=6 SCAR=17.9
124  
125 # Loop as long as we can get new lines
126 471 my $string = '';
127 471 my $depth = 0;
128 471 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 1281 0 1281
129 # Get the search area
130 1281 pos $t->{line} = $t->{line_cursor};
131  
132 # Look for a match
133 1281 ▶ unless ( $t->{line} =~ /\G((?:\\.|[^()])*?[()])/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line"} =~ /\G((?:\\.|[^()])*?[()])/cg) 65 1216 1281
134 # Load in the next line and push to first character
135 65 $string .= substr( $t->{line}, $t->{line_cursor} );
136 65 ▶ $t->_fill_line(1) or return \$string;
Branch: 1 branch
BranchTrueFalseTotal
$t->_fill_line(1) or return \$string 24 41 65
137 41 $t->{line_cursor} = 0;
138 41 next;
139 }
140  
141 # Add to the string
142 1216 $string .= $1;
143 1216 $t->{line_cursor} += length $1;
144  
145 # Alter the depth and continue if we aren't at the end
146 1216 ▶ $depth += ($1 =~ /\($/) ? 1 : -1 and next;
Branch: 2 branches
BranchTrueFalseTotal
$depth += $1 =~ /\($/ ? 1 : -1 and next 769 447 1216
$1 =~ /\($/ ? : 623 593 1216
147  
148 # Found the end
149 447 return $string;
150 }
151  
152 # Returning the string as a reference indicates EOF
153 0 \$string;
154 }
155  
156 62 1;
157  
158 =pod
159  
160 =head1 SUPPORT
161  
162 See the L<support section|PPI/SUPPORT> in the main module.
163  
164 =head1 AUTHOR
165  
166 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
167  
168 =head1 COPYRIGHT
169  
170 Copyright 2001 - 2011 Adam Kennedy.
171  
172 This program is free software; you can redistribute
173 it and/or modify it under the same terms as Perl itself.
174  
175 The full text of the license can be found in the
176 LICENSE file included with this module.
177  
178 =cut
« PPI/Statement/Package.pm ↑ Summary PPI/Token/Number/Octal.pm »