lib/PHP/Decode/Tokenizer.pm

filter: Statementstmt 77.1% 246 / 319 Branchbran 74.3% 165 / 222 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 66.6% 8 / 12 Podpod 28.5% 2 / 7 totaltotal 74.4% 439 / 590 CC 118 SCAR 49.8
CC
118
Cov
76%
tokenize_line
207.5
expand_str
54.3
inc_linenum
2.0
CRAP 146.0
« lib/PHP/Decode/Op.pm ↑ Summary blib/lib/PHP/Decode.pm »
1 #
2 # tokenize PHP source files
3 #
4 package PHP::Decode::Tokenizer;
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '1.48';
10  
11 # Initialize PHP::Decode::Tokenizer
12 # {inscript} - set to indicate already inside of script
13 # {warn} - warning message handler
14 #
15 821 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 821 my ($class, %args) = @_;
17 my $self = bless {
18 inscript => 0, # look for start of script
19 0 ▶ warn => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
20 821 %args, # might override preceding keys
21 }, $class;
22 821 return $self;
23 }
24  
25 1073 ▶sub inc_linenum {
Subroutine inc_linenum: called CC=2 SCAR=6.9
Pod undocumented
26 1073 my ($self, $s) = @_;
27  
28 1073 my $off = 0;
29 1073 ▶ while (($off = index($s, "\n", $off)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($off = index($s, "\n", $off)) != -1) 4 1073 1077
30 4 $self->add_white("\n");
31 4 $off++;
32 }
33 1073 return;
34 }
35  
36 # extract variables from interpolated strings
37 #
38 215 ▶sub expand_str {
Subroutine expand_str: called CC=37 SCAR=40.0
Pod undocumented
39 215 my ($self, $s, $off) = @_;
40 215 my @word = ();
41 215 my $cont;
42  
43 # http://php.net/manual/en/language.types.string.php#language.types.string.parsing
44 # for variable names see: https://www.php.net/manual/en/language.variables.basics.php
45 #
46 215 while (1) {
47 269 ▶ unless ($s =~ /\G([^\\\$\{]*)([\\\$\{]|$)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /\G([^\\\$\{]*)([\\\$\{]|$)/cgs) 1 268 269
48 1 last;
49 }
50 #print ">>> '$1' '$2'\n";
51 #$self->inc_linenum($1);
52 268 push(@word, $1);
53 268 ▶ if ($2 ne "\\") {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 ne "\\") 249 19 268
54 # http://php.net/manual/en/language.types.string.php#language.types.string.parsing
55 249 ▶ if ($2 eq '$') {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 eq '$') { } 22 227 249
elsif ($2 eq '{') { } 13 214 227
56 # expand simple variable
57 22 ▶ if ($s =~ /\G([a-zA-Z_\x80-\xff][\w\x80-\xff]*)((\:\:|\-\>)([a-zA-Z_\x80-\xff][\w\x80-\xff]*))?/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G([a-zA-Z_\x80-\xff][\w\x80-\xff]*)((\:\:|\-\>)([a-zA-Z_\x80-\xff][\w\x80-\xff]*))?/cgs) { } 19 3 22
58 19 my $w = join('', @word);
59 19 ▶ if (defined $cont) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cont) 4 15 19
60 4 $self->add('.');
61 }
62 #if ($w ne '') {
63 19 $self->add_str($w);
64 19 $self->add('.');
65 #}
66 # just simple object references here
67 #
68 19 $self->add_var($1, $off);
69 19 ▶ if (defined $2 && ($2 ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and $2 ne "") 0 19 19
Condition33%1/3
defined $2 and $2 ne ""
!ll&&!rl&&r
1900
MC/DC0%0/2
defined $2 and $2 ne ""
defined $2 $2 ne ""
Truth table33%1/3
A and B
A= defined $2B= $2 ne ""
ABresult
0X0
100
111
70 0 $self->add($3);
71 0 $self->add_sym($4, $off);
72 }
73 19 ▶ if ($s =~ /\G(\[)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G(\[)/cgs) 4 15 19
74 4 ▶ if ($s =~ /\G([^"]*?)\]/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G([^"]*?)\]/cgs) { } 4 0 4
75 4 $self->add_open('[');
76 # todo: split index special chars
77 4 my $i = $1;
78 4 ▶ if ($i =~ /^([0-9]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i =~ /^([0-9]+)$/) { } 2 2 4
elsif ($i =~ /^'(.*)'$/) { } 0 2 2
79 2 $self->add_num($1);
80 } elsif ($i =~ /^'(.*)'$/) {
81 0 $self->add_str($1);
82 } else {
83 2 $self->add_sym($i, $off);
84 }
85 4 $self->add_close(']');
86 } else {
87 0 push(@word, '[');
88 }
89 }
90 19 $cont = 1;
91 19 @word = ();
92 } else {
93 3 push(@word, '$');
94 }
95 22 next;
96 } elsif ($2 eq '{') {
97 # expand complex variable
98 13 ▶ if ($s =~ /\G(\$)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G(\$)/cgs) { } 5 8 13
99 5 ▶ if ($s =~ /\G([^"]*?)\}/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G([^"]*?)\}/cgs) { } 5 0 5
100 5 my $w = join('', @word);
101 5 ▶ if (defined $cont) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cont) 1 4 5
102 1 $self->add('.');
103 }
104 5 ▶ if ($w ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w ne "") 4 1 5
105 4 $self->add_str($w);
106 4 $self->add('.');
107 }
108 5 my $v = $1;
109 5 ▶ if ($v =~ /^([a-zA-Z_\x80-\xff][\w\x80-\xff]*)((\:\:|\-\>)([a-zA-Z_\x80-\xff][\w\x80-\xff]*))?(\[(.*?)\])?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^([a-zA-Z_\x80-\xff][\w\x80-\xff]*)((\:\:|\-\>)([a-zA-Z_\x80-\xff][\w\x80-\xff]*))?(\[(.*?)\])?$/) { } 5 0 5
110 # split object references
111 #
112 5 $self->add_var($1, $off);
113 5 ▶ if (defined $2 && ($2 ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and $2 ne "") 0 5 5
Condition33%1/3
defined $2 and $2 ne ""
!ll&&!rl&&r
500
MC/DC0%0/2
defined $2 and $2 ne ""
defined $2 $2 ne ""
Truth table33%1/3
A and B
A= defined $2B= $2 ne ""
ABresult
0X0
100
111
114 0 $self->add($3);
115 0 $self->add_sym($4, $off);
116 }
117 5 ▶ if (defined $5) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $5) 2 3 5
118 2 my $i = $6;
119 2 $self->add_open('[');
120 # todo: split var special chars
121 2 ▶ if ($i =~ /^([0-9]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i =~ /^([0-9]+)$/) { } 2 0 2
elsif ($i =~ /^'(.*)'$/) { } 0 0 0
122 2 $self->add_num($1);
123 } elsif ($i =~ /^'(.*)'$/) {
124 0 $self->add_str($1);
125 } else {
126 0 $self->add_sym($i, $off);
127 }
128 2 $self->add_close(']');
129 }
130 } else {
131 0 $self->add_var($v, $off);
132 }
133 5 $cont = 1;
134 5 @word = ();
135 } else {
136 0 push(@word, '{$');
137 }
138 } else {
139 8 push(@word, '{');
140 }
141 13 next;
142 }
143 214 last;
144 }
145 19 $s =~ /\G(.)/sgc;
146 # escape characters in double quoted strings:
147 # http://php.net/manual/de/language.types.string.php
148 # http://perldoc.perl.org/perlrebackslash.html
149 #
150 19 ▶ if ($1 eq 'n') {
Branch: 10 branches
BranchTrueFalseTotal
if ($1 eq 'n') { } 0 19 19
elsif ($1 eq 'r') { } 0 19 19
elsif ($1 eq 't') { } 0 19 19
elsif ($1 eq 'v') { } 0 19 19
elsif ($1 eq 'f') { } 0 19 19
elsif ($1 eq '\\') { } 5 14 19
elsif ($1 eq '$') { } 0 14 14
elsif ($1 eq '"') { } 6 8 14
elsif ($1 eq 'x') { } 5 3 8
elsif ($1 ge '0' and $1 le '7') { } 2 1 3
151 0 push(@word, "\n");
152 } elsif ($1 eq 'r') {
153 0 push(@word, "\r");
154 } elsif ($1 eq 't') {
155 0 push(@word, "\t");
156 } elsif ($1 eq 'v') {
157 0 push(@word, chr(0x0b));
158 } elsif ($1 eq 'f') {
159 0 push(@word, "\f");
160 } elsif ($1 eq "\\") {
161 5 push(@word, "\\");
162 } elsif ($1 eq '$') {
163 0 push(@word, '$');
164 } elsif ($1 eq '"') {
165 6 push(@word, '"');
166 } elsif ($1 eq 'x') {
167 5 $s =~ /\G([0-9a-fA-F]{1,2})/sgc;
168 5 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 5 0 5
169 5 push(@word, chr(hex($1)));
170 } else {
171 0 push(@word, "\\".'x');
172 }
173 ▶ } elsif (($1 ge '0') && ($1 le '7')) {
Condition66%2/3
$1 ge 0 and $1 le 7
!ll&&!rl&&r
012
MC/DC50%1/2
$1 ge 0 and $1 le 7
$1 ge 0 $1 le 7
Truth table66%2/3
A and B
A= $1 ge 0B= $1 le 7
ABresult
0X0
100
111
174 2 my $o = $1;
175 2 $s =~ /\G([0-7]{0,2})/sgc;
176 2 $o .= $1;
177 2 push(@word, chr(oct($o)));
178 } else {
179 1 push(@word, "\\".$1);
180 }
181 }
182 215 my $w = join('', @word);
183 215 ▶ if (defined $cont) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cont) { } 19 196 215
184 19 ▶ if ($w ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w ne "") 8 11 19
185 8 $self->add('.');
186 8 $self->add_str($w, $off);
187 }
188 } else {
189 196 $self->add_str($w, $off);
190 }
191 215 return;
192 }
193  
194 # split into tokens, unquote quoted fields, and allow escapes
195 #
196 811 ▶sub tokenize_line {
Subroutine tokenize_line: called CC=81 SCAR=53.4
Pod documented
197 811 my ($self, $line, $quote) = @_;
198  
199 # use regex to tokenize (https://perldoc.perl.org/perlrequick)
200 #
201 # The most interesting behaviour of /PAT/g is when not in list context,
202 # but in scalar context. In that case, the next regexp can continue
203 # where the previous one left off, using the \G anchor. Conceptually,
204 # it is the same as the ^ anchor, which anchors at the beginning of the
205 # string - except now it anchors on the current value of pos(), for
206 # this string, which is at the end of where the previous pattern
207 # matched. The /c modifier prevents reset of the pos pointer # to 0
208 # when the match fails.
209 #
210 # Such a lexer could look like this:
211 #
212 # $_ = 'unquoted text: "quoted text."';
213 # while (1) {
214 # /\G\s*(?=\S)/gc or last;
215 # if (/\G(\w+)/gc) {
216 # print "Found word: $1\n";
217 # } elsif (/\G(['"])/gc) {
218 # print "Found quote: $1\n";
219 # } elsif (/\G([.,;:!?])/gc) {
220 # print "Found punctuation: $1\n";
221 # } else {
222 # /\G(?=(\S+))/gc;
223 # printf "Unknown token: %s +(pos %d)", $1, pos;
224 # }
225 # }
226 #
227 811 $_ = $line;
228 811 WORD: while (1) {
229 18988 ▶ if (!$self->{inscript}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inscript"}) 23 18965 18988
230 23 my @seq = ();
231 23 my $sym;
232  
233 # parse everything between ?> and <? as string
234 # and convert to 'echo #str;'
235 #
236 23 NOSCRIPT: while (1) {
237 37 ▶ if (defined $quote) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $quote) { } 2 35 37
elsif (/\G(["'`])/cgs) { } 2 33 35
elsif (/\G(\\)/cgs) { } 0 33 33
elsif (/\G(<)/cgs) { } 14 19 33
238 2 my @word = ();
239 2 ▶ if ($quote eq '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq q[']) { } 1 1 2
240 1 while (1) {
241 3 ▶ unless (/\G([^$quote\\]*)([$quote\\])/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G([^$quote\\]*)([$quote\\])/cgs) 0 3 3
242 0 push(@seq, $quote);
243 #push(@seq, join("", @word));
244 0 push(@seq, @word);
245 0 $self->add_noscript(join('', @seq), pos);
246 0 last NOSCRIPT;
247 }
248 3 $self->inc_linenum($1);
249 3 push(@word, $1);
250 3 ▶ last if ($2 ne "\\");
Branch: 1 branch
BranchTrueFalseTotal
if $2 ne "\\" 1 2 3
251 2 /\G(.)/sgc;
252 2 push(@word, "\\".$1);
253 }
254 } else {
255 1 while (1) {
256 # strings are expanded single quoted, so
257 # escape single quotes in other strings
258 #
259 3 ▶ unless (/\G([^$quote'\\]*)([$quote'\\])/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G([^$quote'\\]*)([$quote'\\])/cgs) 0 3 3
260 0 push(@seq, $quote);
261 #push(@seq, join("", @word));
262 0 push(@seq, @word);
263 0 $self->add_noscript(join('', @seq), pos);
264 0 last NOSCRIPT;
265 }
266 3 $self->inc_linenum($1);
267 3 push(@word, $1);
268 3 ▶ if ($2 eq '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "'") 0 3 3
269 0 push(@word, "\\".$2);
270 0 next;
271 }
272 3 ▶ last if ($2 ne "\\");
Branch: 1 branch
BranchTrueFalseTotal
if $2 ne "\\" 1 2 3
273 2 /\G(.)/sgc;
274 2 push(@word, "\\".$1);
275 }
276 }
277 2 push(@seq, $quote);
278 2 push(@seq, @word);
279 2 push(@seq, $quote);
280 2 $quote = undef;
281 } elsif (/\G(["'`])/sgc) {
282 2 $quote = $1;
283 } elsif (/\G(\\)/sgc) {
284 0 /\G(.)/sgc;
285 0 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
286 0 push(@seq, "\\".$1);
287 } else {
288 0 $self->add_noscript(join('', @seq), pos);
289 0 last;
290 }
291 } elsif (/\G(<)/sgc) {
292 14 my $cur = pos;
293 14 ▶ if (/\G(\?php)/sgci) {
Branch: 4 branches
BranchTrueFalseTotal
if (/\G(\?php)/cgis) { } 7 7 14
elsif (/\G(\?)/cgs) { } 6 1 7
elsif (/\G(\?=)/cgs) { } 0 1 1
elsif (/\G(script\s*language\s*=\s*["']php["']\s*>)/cgis) { } 1 0 1
294 7 my $s = join('', @seq);
295 7 ▶ if ($s ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 0 7 7
296 0 $self->add_noscript($s, $cur-2);
297 }
298 7 $self->add_script_start('<?php', $cur-1);
299 7 $self->{inscript} = 1;
300 7 last;
301 } elsif (/\G(\?)/sgc) {
302 6 my $s = join('', @seq);
303 6 ▶ if ($s ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 3 3 6
304 3 $self->add_noscript($s, $cur-2);
305 }
306 6 $self->add_script_start('<?', $cur-1);
307 6 $self->{inscript} = 1;
308 6 last;
309 } elsif (/\G(\?=)/sgc) {
310 # short_open_tag for echo since php-5.4 always avail
311 0 my $s = join('', @seq);
312 0 ▶ if ($s ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 0 0 0
313 0 $self->add_noscript($s, $cur-2);
314 }
315 0 $self->add_script_start('<?', $cur-1);
316 0 $self->add_sym('echo');
317 0 $self->{inscript} = 1;
318 0 last;
319 } elsif (/\G(script\s*language\s*=\s*["']php["']\s*>)/sgci) {
320 # <script language='php'> .. </script>
321 #
322 1 my $s = join('', @seq);
323 1 ▶ if ($s ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 0 1 1
324 0 $self->add_noscript($s, $cur-2);
325 }
326 1 $self->add_script_start('<?php', $cur-1);
327 1 $self->{inscript} = 2;
328 1 last;
329 } else {
330 0 push(@seq, '<');
331 }
332 } else {
333 19 ▶ unless (/\G([^"'`'<\\]+)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G([^"'`'<\\]+)/cgs) 9 10 19
334 #/\G(.)/sgc;
335 #printf ">> WARN: parse end after: [0x%02x] %s\n", ord($1), $self->tok_dump();
336 9 my $s = join('', @seq);
337 9 ▶ if ($s ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 5 4 9
338 5 $self->add_noscript($s, pos);
339 }
340 9 last;
341 }
342 10 push(@seq, $1);
343 }
344 }
345 }
346 #printf ">>>>> [pos: %d] %s\n", pos, $self->tok_dump();
347 18988 my $cur = pos;
348  
349 18988 ▶ if (defined $quote) {
Branch: 17 branches
BranchTrueFalseTotal
if (defined $quote) { } 1027 17961 18988
elsif (/\G(["'`])/cgs) { } 1027 16934 17961
elsif (m[\G(/)]cgs) { } 9 16925 16934
elsif (/\G(#)/cg) { } 3 16922 16925
elsif (/\G(\n)/cgs) { } 2 16920 16922
elsif (/\G([^\S\n]+)/cgs) { } 3588 13332 16920
elsif (/\G([\x01-\x1f\x7f])/cgs) { } 0 13332 13332
elsif (/\G(<)/cgs) { } 48 13284 13332
elsif (/\G(\?)/cgs) { } 41 13243 13284
elsif (/\G(\0)/cgs) { } 0 13243 13243
elsif (/\G([\[\(\{])/cgs) { } 2319 10924 13243
elsif (/\G([\}\)\]])/cgs) { } 2311 8613 10924
elsif (/\G([>\;\=\,\.\:\&\-\+\|\^\~\%\!\\])/cgs) { } 3080 5533 8613
elsif (/\G(\$)/cgs) { } 1896 3637 5533
elsif (/\G(\@)/cgs) { } 1 3636 3637
elsif (/\G([0-9]+)/cgs) { } 568 3068 3636
elsif (/\G([\w\x80-\xff]+)/cgs) { } 2256 812 3068
350 # handle quotes
351 #
352 1027 my @word = ();
353  
354 # before php 5.3 php silently truncated strings
355 # after a 0-byte (0-byte poisioning)
356 # https://bugs.php.net/bug.php?id=39863
357 #
358 1027 ▶ if ($quote eq "\"") {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq '"') { } 213 814 1027
359 # In the first pass, just scan for the end of the string, and
360 # in the second pass expand variables and other escape codes.
361 # (keep escapes intact here)
362 #
363 213 while (1) {
364 232 ▶ unless (/\G([^$quote\\]*)([$quote\\])/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G([^$quote\\]*)([$quote\\])/cgs) 0 232 232
365 0 last WORD;
366 }
367 232 $self->inc_linenum($1);
368 232 push(@word, $1);
369 232 ▶ last if ($2 ne "\\");
Branch: 1 branch
BranchTrueFalseTotal
if $2 ne "\\" 213 19 232
370 19 /\G(.)/sgc;
371 19 push(@word, "\\".$1);
372 }
373 213 $self->expand_str(join('', @word), $cur);
374 } else {
375 814 while (1) {
376 828 ▶ unless (/\G([^$quote\\]*)([$quote\\])/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G([^$quote\\]*)([$quote\\])/cgs) 0 828 828
377 0 last WORD;
378 }
379 828 $self->inc_linenum($1);
380 828 push(@word, $1);
381 828 ▶ last if ($2 ne "\\");
Branch: 1 branch
BranchTrueFalseTotal
if $2 ne "\\" 814 14 828
382 14 /\G(.)/sgc;
383 14 ▶ if (($1 eq '\\') || ($1 eq $quote)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '\\' or $1 eq $quote) { } 9 5 14
Condition100%3/3
$1 eq "\\" or $1 eq $quote
l!l&&r!l&&!r
275
MC/DC100%2/2
$1 eq "\\" or $1 eq $quote
$1 eq "\\" $1 eq $quote
Truth table100%3/3
A or B
A= $1 eq "\\"B= $1 eq $quote
ABresult
1X1
011
000
384 9 push(@word, $1);
385 } else {
386 5 push(@word, "\\".$1);
387 }
388 }
389 814 $self->add_str(join('', @word), $cur);
390 }
391 1027 $quote = undef;
392 } elsif (/\G(["'`])/sgc) {
393 1027 $quote = $1;
394 } elsif (/\G(\/)/sgc) {
395 9 ▶ if (/\G(\*)/sgc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G(\*)/cgs) { } 1 8 9
elsif (m[\G(/)]cg) { } 7 1 8
396 # parse /* ... */ comments
397 1 ▶ unless (/\G(.*?)\*\//sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (m[\G(.*?)\*/]cgs) 0 1 1
398 0 $quote = '*';
399 0 last WORD;
400 }
401 # insert as string without newlines
402 1 my $s = $1;
403 1 $self->inc_linenum($s);
404 1 $s =~ s/[\r\n]/ /g;
405 1 $self->add_comment($s, pos);
406 } elsif (/\G(\/)/gc) {
407 # skip comments
408 # parse // comments (up to ?> or line end)
409 #
410 # ?> tags have a higher priority and stop the comment
411 # http://php.net/manual/en/language.basic-syntax.comments.php
412 #
413 7 ▶ unless (/\G(.*?)(\?>|\n|\r\n|\0|$)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G(.*?)(\?>|\n|\r\n|\0|$)/cg) 0 7 7
414 0 last WORD;
415 }
416 7 $self->add_comment($1, pos);
417 7 ▶ if ($2 eq '?>') {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 eq '?>') { } 1 6 7
elsif ($2 eq "\n") { } 0 6 6
418 1 $self->add_script_end('?>', pos);
419 1 $self->{inscript} = 0;
420 } elsif ($2 eq "\n") {
421 0 $self->add_white($2);
422 }
423 } else {
424 1 $self->add('/');
425 }
426 } elsif (/\G(#)/gc) {
427 #if (/\G((str|num|const|arr|fun|class|trait|call|elem|expr|stmt|blk|ref|obj|scope|ns)\d+)/gc) {
428 # # parse inserted #str symbols (might be useful for sub evals)
429 # $self->add('#'.$1);
430 #} else {
431 # parse # comments (up to ?> or line end)
432 #
433 3 ▶ unless (/\G(.*?)(\?>|\n|\r\n|\0)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G(.*?)(\?>|\n|\r\n|\0)/cg) 2 1 3
434 2 last WORD;
435 }
436 1 $self->add_comment($1, pos);
437 1 ▶ if ($2 eq '?>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "?>") 1 0 1
438 1 my $cur = pos;
439 1 ▶ if (/\G(\n|\r\n)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\n|\r\n)/cgs) { } 1 0 1
440 1 $self->add_script_end("?>\n", pos);
441 } else {
442 0 $self->add_script_end('?>', $cur);
443 }
444 1 $self->{inscript} = 0;
445 }
446 #}
447 } elsif (/\G(\n)/sgc) {
448 # skip whitespace token
449 2 $self->add_white($1);
450 } elsif (/\G([^\S\n]+)/sgc) {
451 # skip whitespace token
452 3588 $self->add_white($1);
453 } elsif (/\G([\x01-\x1f\x7f])/sgc) {
454 # skip non printable token
455 0 $self->add_white($1);
456 } elsif (/\G(<)/sgc) {
457 48 ▶ if (/\G(\?php)/sgci) {
Branch: 6 branches
BranchTrueFalseTotal
if (/\G(\?php)/cgis) { } 0 48 48
elsif (/\G(\?)/cgs) { } 0 48 48
elsif (/\G(\?=)/cgs) { } 0 48 48
elsif (/\G(script\s*language\s*=\s*["']php["']\s*>)/cgis) { } 0 48 48
elsif ($self->{'inscript'} == 2 and m[\G(/script\s*>)]cgis) { } 1 47 48
elsif (/\G(<<)/cgs) { } 2 45 47
458 0 ▶ if ($self->tok_count() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tok_count > 0) 0 0 0
459 0 $self->add_bad_open('<?php');
460 }
461 } elsif (/\G(\?)/sgc) {
462 0 ▶ if ($self->tok_count() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tok_count > 0) 0 0 0
463 0 $self->add_bad_open('<?');
464 }
465 } elsif (/\G(\?=)/sgc) {
466 0 ▶ if ($self->tok_count() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tok_count > 0) 0 0 0
467 0 $self->add_bad_open('<?=');
468 }
469 } elsif (/\G(script\s*language\s*=\s*["']php["']\s*>)/sgci) {
470 0 ▶ if ($self->tok_count() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tok_count > 0) 0 0 0
471 0 $self->add_bad_open('<script language=\'php\'>');
472 }
473 ▶ } elsif (($self->{inscript} == 2) && /\G(\/script\s*>)/sgci) {
Condition66%2/3
$self->{"inscript"} == 2 and m[\G(/script\s*>)]cgis
!ll&&!rl&&r
4701
MC/DC50%1/2
$self->{"inscript"} == 2 and m[\G(/script\s*>)]cgis
$self->{"inscript"} == 2 m[\G(/script\s*>)]cgis
Truth table66%2/3
A and B
A= $self->{"inscript"} == 2B= m[\G(/script\s*>)]cgis
ABresult
0X0
100
111
474 # <script language='php'> .. </script>
475 #
476 1 my $cur = pos;
477 1 ▶ if (/\G(\n|\r\n)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\n|\r\n)/cgs) { } 1 0 1
478 1 $self->add_script_end("?>\n", pos);
479 } else {
480 0 $self->add_script_end('?>', $cur);
481 }
482 1 $self->{inscript} = 0;
483 } elsif (/\G(<<)/sgc) {
484 # heredoc or nowdoc
485 # http://php.net/manual/de/language.types.string.php
486 #
487 2 ▶ if (/\G([\w\pL]+)(\n|\r\n)/sgc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G([\w\pL]+)(\n|\r\n)/cgs) { } 2 0 2
elsif (/\G\'([\w\pL]+)\'(\n|\r\n)/cgs) { } 0 0 0
488 2 my $e = $1;
489 2 $self->inc_linenum("\n");
490 2 ▶ unless (/\G(.*?\n)$e(\;)?(\n|\r\n|\0|$)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G(.*?\n)$e(\;)?(\n|\r\n|\0|$)/cgs) 0 2 2
491 0 last WORD;
492 }
493 2 $self->inc_linenum($1);
494 2 $self->expand_str($1, $cur);
495 #$self->add_str($1);
496 2 $self->inc_linenum($3);
497 } elsif (/\G\'([\w\pL]+)\'(\n|\r\n)/sgc) {
498 0 my $e = $1;
499 0 $self->inc_linenum("\n");
500 0 ▶ unless (/\G(.*?\n)$e(\;)?(\n|\r\n|\0|$)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G(.*?\n)$e(\;)?(\n|\r\n|\0|$)/cgs) 0 0 0
501 0 last WORD;
502 }
503 0 $self->inc_linenum($1);
504 0 $self->add_str($1);
505 0 $self->inc_linenum($3);
506 } else {
507 0 $self->add('<<<');
508 }
509 } else {
510 45 $self->add('<');
511 }
512 } elsif (/\G(\?)/sgc) {
513 41 ▶ if (/\G(>)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(>)/cgs) { } 17 24 41
514 # http://php.net/manual/en/language.basic-syntax.instruction-separation.php
515 # the closing tag includes an optional immediately following newline
516 #
517 17 my $cur = pos;
518 17 ▶ if (/\G(\n|\r\n)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\n|\r\n)/cgs) { } 2 15 17
519 2 $self->add_script_end("?>\n", pos);
520 } else {
521 15 $self->add_script_end('?>', $cur);
522 }
523 17 $self->{inscript} = 0;
524 } else {
525 24 $self->add('?');
526 }
527 } elsif (/\G(\0)/sgc) {
528 0 $self->add_script_end($1, pos);
529 0 $self->{inscript} = 0;
530 } elsif (/\G([\[\(\{])/sgc) {
531 2319 $self->add_open($1, $cur);
532 } elsif (/\G([\}\)\]])/sgc) {
533 2311 $self->add_close($1, $cur);
534 } elsif (/\G([>\;\=\,\.\:\&\-\+\|\^\~\%\!\\])/sgc) {
535 # backslash is php namespace separator (not escape)
536 #
537 3080 $self->add($1);
538 } elsif (/\G(\$)/sgc) {
539 # variable $var or '${'
540 #
541 1896 ▶ if (/\G([a-zA-Z_\x80-\xff][\w\x80-\xff]*)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G([a-zA-Z_\x80-\xff][\w\x80-\xff]*)/cgs) { } 1812 84 1896
542 1812 $self->add_var($1, $cur);
543 } else {
544 84 $self->add('$');
545 }
546 } elsif (/\G(\@)/sgc) {
547 # '@' is allowed in php-identifiers
548 # it suppresses error messages
549 #
550 1 $self->add_white('@');
551 } elsif (/\G([0-9]+)/sgc) {
552 568 my $v = $1;
553 568 ▶ if (($v eq '0') && /\G([xX][0-9a-fA-F]+)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq '0' and /\G([xX][0-9a-fA-F]+)/cgs) { } 4 564 568
Condition100%3/3
$v eq 0 and /\G([xX][0-9a-fA-F]+)/cgs
!ll&&!rl&&r
4411234
MC/DC100%2/2
$v eq 0 and /\G([xX][0-9a-fA-F]+)/cgs
$v eq 0 /\G([xX][0-9a-fA-F]+)/cgs
Truth table100%3/3
A and B
A= $v eq 0B= /\G([xX][0-9a-fA-F]+)/cgs
ABresult
0X0
100
111
554 4 $self->add_num('0'.$1); # hex
555 } else {
556 564 ▶ if (/\G(\.[0-9]+)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\.[0-9]+)/cgs) 7 557 564
557 7 $v .= $1; # float
558 }
559 564 ▶ if (/\G([eE][\+\-]?[0-9]+)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G([eE][\+\-]?[0-9]+)/cgs) 0 564 564
560 0 $v .= $1; # exponent
561 }
562 564 $self->add_num($v); # dec/oct/float
563 }
564 } elsif (/\G([\w\x80-\xff]+)/sgc) {
565 2256 $self->add_sym($1, $cur);
566 } else {
567 812 ▶ unless (/\G([^"'`<>\\\/#\s\w\[\]\(\)\{\}\$\?\;\=\,\.\:\&\-\+\|\^\~\%\!]+)/sgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (m<\G([^"'`<>\\/#\s\w\[\]\(\)\{\}\$\?\;\=\,\.\:\&\-\+\|\^\~\%\!]+)>cgs) 809 3 812
568 #/\G(.)/sgc;
569 #printf ">> WARN: parse end after: [0x%02x] %s\n", ord($1), $self->tok_dump();
570 809 last;
571 }
572 3 $self->add($1);
573 }
574 }
575 811 ▶ if ($self->{inscript}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inscript"}) 802 9 811
576 802 $self->add_script_end('', pos);
577 }
578 811 return $quote;
579 }
580  
581 # set up method stubs
582 # (when just the add()-method is overridden, then all
583 # other handlers call this subclass method)
584 #
585 0 ▶sub add { }
Subroutine add: not called CC=1 SCAR=0.0
Pod undocumented
586 4 ▶sub _add { my ($self, $sym) = @_; $self->add($sym); return; }
Subroutine _add: called CC=1 SCAR=0.0
587 7 *add_open = \&_add;
588 7 *add_close = \&_add;
589 7 *add_white = \&_add;
590 7 *add_comment = \&_add;
591 7 *add_sym = \&_add;
592 7 *add_var = \&_add;
593 7 *add_str = \&_add;
594 7 *add_num = \&_add;
595 7 *add_script_start = \&_add;
596 7 *add_script_end = \&_add;
597 7 *add_noscript = \&_add;
598 7 *add_bad_open = \&_add;
599  
600 0 ▶sub tok_dump { return ''; }
Subroutine tok_dump: not called CC=1 SCAR=6.9
Pod undocumented
601 0 ▶sub tok_count { return 0; }
Subroutine tok_count: not called CC=1 SCAR=6.9
Pod undocumented
602  
603 778 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
604 778 my $self = shift;
605 778 return;
606 }
607  
608 7 1;
609  
610  
611 __END__
« lib/PHP/Decode/Op.pm ↑ Summary blib/lib/PHP/Decode.pm »