4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\CodeCleaner;
15 use PhpParser\Node\Expr;
16 use PhpParser\Node\Expr\Exit_;
17 use PhpParser\Node\Stmt;
18 use PhpParser\Node\Stmt\Break_;
19 use PhpParser\Node\Stmt\Expression;
20 use PhpParser\Node\Stmt\If_;
21 use PhpParser\Node\Stmt\Namespace_;
22 use PhpParser\Node\Stmt\Return_;
23 use PhpParser\Node\Stmt\Switch_;
26 * Add an implicit "return" to the last statement, provided it can be returned.
28 class ImplicitReturnPass extends CodeCleanerPass
35 public function beforeTraverse(array $nodes)
37 return $this->addImplicitReturn($nodes);
45 private function addImplicitReturn(array $nodes)
47 // If nodes is empty, it can't have a return value.
49 return [new Return_(NoReturnValue::create())];
54 // Special case a few types of statements to add an implicit return
55 // value (even though they technically don't have any return value)
56 // because showing a return value in these instances is useful and not
58 if ($last instanceof If_) {
59 $last->stmts = $this->addImplicitReturn($last->stmts);
61 foreach ($last->elseifs as $elseif) {
62 $elseif->stmts = $this->addImplicitReturn($elseif->stmts);
66 $last->else->stmts = $this->addImplicitReturn($last->else->stmts);
68 } elseif ($last instanceof Switch_) {
69 foreach ($last->cases as $case) {
70 // only add an implicit return to cases which end in break
71 $caseLast = \end($case->stmts);
72 if ($caseLast instanceof Break_) {
73 $case->stmts = $this->addImplicitReturn(\array_slice($case->stmts, 0, -1));
74 $case->stmts[] = $caseLast;
77 } elseif ($last instanceof Expr && !($last instanceof Exit_)) {
78 // @codeCoverageIgnoreStart
79 $nodes[\count($nodes) - 1] = new Return_($last, [
80 'startLine' => $last->getLine(),
81 'endLine' => $last->getLine(),
83 // @codeCoverageIgnoreEnd
84 } elseif ($last instanceof Expression && !($last->expr instanceof Exit_)) {
86 $nodes[\count($nodes) - 1] = new Return_($last->expr, [
87 'startLine' => $last->getLine(),
88 'endLine' => $last->getLine(),
90 } elseif ($last instanceof Namespace_) {
91 $last->stmts = $this->addImplicitReturn($last->stmts);
94 // Return a "no return value" for all non-expression statements, so that
95 // PsySH can suppress the `null` that `eval()` returns otherwise.
97 // Note that statements special cased above (if/elseif/else, switch)
98 // _might_ implicitly return a value before this catch-all return is
101 // We're not adding a fallback return after namespace statements,
102 // because code outside namespace statements doesn't really work, and
103 // there's already an implicit return in the namespace statement anyway.
104 if (self::isNonExpressionStmt($last)) {
105 $nodes[] = new Return_(NoReturnValue::create());
112 * Check whether a given node is a non-expression statement.
114 * As of PHP Parser 4.x, Expressions are now instances of Stmt as well, so
115 * we'll exclude them here.
121 private static function isNonExpressionStmt(Node $node)
123 return $node instanceof Stmt &&
124 !$node instanceof Expression &&
125 !$node instanceof Return_ &&
126 !$node instanceof Namespace_;