/*
* This file is part of Psy Shell.
*
- * (c) 2012-2017 Justin Hileman
+ * (c) 2012-2018 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;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
+use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Return_;
{
// If nodes is empty, it can't have a return value.
if (empty($nodes)) {
- return array(new Return_(new New_(new FullyQualifiedName('Psy\CodeCleaner\NoReturnValue'))));
+ return [new Return_(new New_(new FullyQualifiedName('Psy\CodeCleaner\NoReturnValue')))];
}
$last = end($nodes);
}
}
} elseif ($last instanceof Expr && !($last instanceof Exit_)) {
- $nodes[count($nodes) - 1] = new Return_($last, array(
+ $nodes[count($nodes) - 1] = new Return_($last, [
'startLine' => $last->getLine(),
'endLine' => $last->getLine(),
- ));
+ ]);
+ } elseif ($last instanceof Expression && !($last->expr instanceof Exit_)) {
+ // For PHP Parser 4.x
+ $nodes[count($nodes) - 1] = new Return_($last->expr, [
+ 'startLine' => $last->getLine(),
+ 'endLine' => $last->getLine(),
+ ]);
} elseif ($last instanceof Namespace_) {
$last->stmts = $this->addImplicitReturn($last->stmts);
}
// We're not adding a fallback return after namespace statements,
// because code outside namespace statements doesn't really work, and
// there's already an implicit return in the namespace statement anyway.
- if ($last instanceof Stmt && !$last instanceof Return_ && !$last instanceof Namespace_) {
+ if (self::isNonExpressionStmt($last)) {
$nodes[] = new Return_(new New_(new FullyQualifiedName('Psy\CodeCleaner\NoReturnValue')));
}
return $nodes;
}
+
+ /**
+ * Check whether a given node is a non-expression statement.
+ *
+ * As of PHP Parser 4.x, Expressions are now instances of Stmt as well, so
+ * we'll exclude them here.
+ *
+ * @param Node $node
+ *
+ * @return bool
+ */
+ private static function isNonExpressionStmt(Node $node)
+ {
+ return $node instanceof Stmt &&
+ !$node instanceof Expression &&
+ !$node instanceof Return_ &&
+ !$node instanceof Namespace_;
+ }
}