4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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\Array_;
16 use PhpParser\Node\Expr\Assign;
17 use PhpParser\Node\Expr\Empty_;
18 use PhpParser\Node\Expr\FuncCall;
19 use PhpParser\Node\Expr\Isset_;
20 use PhpParser\Node\Expr\MethodCall;
21 use PhpParser\Node\Expr\StaticCall;
22 use Psy\Exception\FatalErrorException;
25 * Validate that the functions are used correctly.
27 * @author Martin HasoĊ <martin.hason@gmail.com>
29 class FunctionReturnInWriteContextPass extends CodeCleanerPass
31 const PHP55_MESSAGE = 'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)';
32 const EXCEPTION_MESSAGE = "Can't use function return value in write context";
36 public function __construct()
38 $this->isPhp55 = version_compare(PHP_VERSION, '5.5', '>=');
42 * Validate that the functions are used correctly.
44 * @throws FatalErrorException if a function is passed as an argument reference
45 * @throws FatalErrorException if a function is used as an argument in the isset
46 * @throws FatalErrorException if a function is used as an argument in the empty, only for PHP < 5.5
47 * @throws FatalErrorException if a value is assigned to a function
51 public function enterNode(Node $node)
53 if ($node instanceof Array_ || $this->isCallNode($node)) {
54 $items = $node instanceof Array_ ? $node->items : $node->args;
55 foreach ($items as $item) {
56 if ($item->byRef && $this->isCallNode($item->value)) {
57 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
60 } elseif ($node instanceof Isset_) {
61 foreach ($node->vars as $var) {
62 if (!$this->isCallNode($var)) {
66 $msg = $this->isPhp55 ? self::PHP55_MESSAGE : self::EXCEPTION_MESSAGE;
67 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
69 } elseif ($node instanceof Empty_ && !$this->isPhp55 && $this->isCallNode($node->expr)) {
70 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
71 } elseif ($node instanceof Assign && $this->isCallNode($node->var)) {
72 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
76 private function isCallNode(Node $node)
78 return $node instanceof FuncCall || $node instanceof MethodCall || $node instanceof StaticCall;