Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / ImplicitReturnPass.php
diff --git a/vendor/psy/psysh/src/Psy/CodeCleaner/ImplicitReturnPass.php b/vendor/psy/psysh/src/Psy/CodeCleaner/ImplicitReturnPass.php
new file mode 100644 (file)
index 0000000..431b9c4
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of Psy Shell.
+ *
+ * (c) 2012-2017 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Psy\CodeCleaner;
+
+use PhpParser\Node\Expr;
+use PhpParser\Node\Expr\Exit_;
+use PhpParser\Node\Stmt\Return_ as ReturnStmt;
+
+/**
+ * Add an implicit "return" to the last statement, provided it can be returned.
+ */
+class ImplicitReturnPass extends CodeCleanerPass
+{
+    /**
+     * @param array $nodes
+     */
+    public function beforeTraverse(array $nodes)
+    {
+        $last = end($nodes);
+
+        if ($last instanceof Expr && !($last instanceof Exit_)) {
+            $nodes[count($nodes) - 1] = new ReturnStmt($last, array(
+                'startLine' => $last->getLine(),
+                'endLine'   => $last->getLine(),
+            ));
+        }
+
+        return $nodes;
+    }
+}