Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\CodeCleaner;
|
Chris@13
|
13
|
Chris@13
|
14 use PhpParser\Node;
|
Chris@13
|
15 use PhpParser\Node\Scalar\DNumber;
|
Chris@13
|
16 use PhpParser\Node\Scalar\LNumber;
|
Chris@13
|
17 use PhpParser\Node\Stmt\Break_;
|
Chris@13
|
18 use PhpParser\Node\Stmt\Continue_;
|
Chris@13
|
19 use PhpParser\Node\Stmt\Do_;
|
Chris@13
|
20 use PhpParser\Node\Stmt\For_;
|
Chris@13
|
21 use PhpParser\Node\Stmt\Foreach_;
|
Chris@13
|
22 use PhpParser\Node\Stmt\Switch_;
|
Chris@13
|
23 use PhpParser\Node\Stmt\While_;
|
Chris@13
|
24 use Psy\Exception\FatalErrorException;
|
Chris@13
|
25
|
Chris@13
|
26 /**
|
Chris@13
|
27 * The loop context pass handles invalid `break` and `continue` statements.
|
Chris@13
|
28 */
|
Chris@13
|
29 class LoopContextPass extends CodeCleanerPass
|
Chris@13
|
30 {
|
Chris@13
|
31 private $loopDepth;
|
Chris@13
|
32
|
Chris@13
|
33 /**
|
Chris@13
|
34 * {@inheritdoc}
|
Chris@13
|
35 */
|
Chris@13
|
36 public function beforeTraverse(array $nodes)
|
Chris@13
|
37 {
|
Chris@13
|
38 $this->loopDepth = 0;
|
Chris@13
|
39 }
|
Chris@13
|
40
|
Chris@13
|
41 /**
|
Chris@13
|
42 * @throws FatalErrorException if the node is a break or continue in a non-loop or switch context
|
Chris@13
|
43 * @throws FatalErrorException if the node is trying to break out of more nested structures than exist
|
Chris@13
|
44 * @throws FatalErrorException if the node is a break or continue and has a non-numeric argument
|
Chris@13
|
45 * @throws FatalErrorException if the node is a break or continue and has an argument less than 1
|
Chris@13
|
46 *
|
Chris@13
|
47 * @param Node $node
|
Chris@13
|
48 */
|
Chris@13
|
49 public function enterNode(Node $node)
|
Chris@13
|
50 {
|
Chris@13
|
51 switch (true) {
|
Chris@13
|
52 case $node instanceof Do_:
|
Chris@13
|
53 case $node instanceof For_:
|
Chris@13
|
54 case $node instanceof Foreach_:
|
Chris@13
|
55 case $node instanceof Switch_:
|
Chris@13
|
56 case $node instanceof While_:
|
Chris@13
|
57 $this->loopDepth++;
|
Chris@13
|
58 break;
|
Chris@13
|
59
|
Chris@13
|
60 case $node instanceof Break_:
|
Chris@13
|
61 case $node instanceof Continue_:
|
Chris@13
|
62 $operator = $node instanceof Break_ ? 'break' : 'continue';
|
Chris@13
|
63
|
Chris@13
|
64 if ($this->loopDepth === 0) {
|
Chris@17
|
65 $msg = \sprintf("'%s' not in the 'loop' or 'switch' context", $operator);
|
Chris@13
|
66 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
|
Chris@13
|
67 }
|
Chris@13
|
68
|
Chris@13
|
69 if ($node->num instanceof LNumber || $node->num instanceof DNumber) {
|
Chris@13
|
70 $num = $node->num->value;
|
Chris@13
|
71 if ($node->num instanceof DNumber || $num < 1) {
|
Chris@17
|
72 $msg = \sprintf("'%s' operator accepts only positive numbers", $operator);
|
Chris@13
|
73 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
|
Chris@13
|
74 }
|
Chris@13
|
75
|
Chris@13
|
76 if ($num > $this->loopDepth) {
|
Chris@17
|
77 $msg = \sprintf("Cannot '%s' %d levels", $operator, $num);
|
Chris@13
|
78 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
|
Chris@13
|
79 }
|
Chris@13
|
80 } elseif ($node->num) {
|
Chris@17
|
81 $msg = \sprintf("'%s' operator with non-constant operand is no longer supported", $operator);
|
Chris@13
|
82 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
|
Chris@13
|
83 }
|
Chris@13
|
84 break;
|
Chris@13
|
85 }
|
Chris@13
|
86 }
|
Chris@13
|
87
|
Chris@13
|
88 /**
|
Chris@13
|
89 * @param Node $node
|
Chris@13
|
90 */
|
Chris@13
|
91 public function leaveNode(Node $node)
|
Chris@13
|
92 {
|
Chris@13
|
93 switch (true) {
|
Chris@13
|
94 case $node instanceof Do_:
|
Chris@13
|
95 case $node instanceof For_:
|
Chris@13
|
96 case $node instanceof Foreach_:
|
Chris@13
|
97 case $node instanceof Switch_:
|
Chris@13
|
98 case $node instanceof While_:
|
Chris@13
|
99 $this->loopDepth--;
|
Chris@13
|
100 break;
|
Chris@13
|
101 }
|
Chris@13
|
102 }
|
Chris@13
|
103 }
|