Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / ImplicitReturnPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\CodeCleaner;
13
14 use PhpParser\Node\Expr;
15 use PhpParser\Node\Expr\Exit_;
16 use PhpParser\Node\Stmt\Return_ as ReturnStmt;
17
18 /**
19  * Add an implicit "return" to the last statement, provided it can be returned.
20  */
21 class ImplicitReturnPass extends CodeCleanerPass
22 {
23     /**
24      * @param array $nodes
25      */
26     public function beforeTraverse(array $nodes)
27     {
28         $last = end($nodes);
29
30         if ($last instanceof Expr && !($last instanceof Exit_)) {
31             $nodes[count($nodes) - 1] = new ReturnStmt($last, array(
32                 'startLine' => $last->getLine(),
33                 'endLine'   => $last->getLine(),
34             ));
35         }
36
37         return $nodes;
38     }
39 }