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\Empty_;
16 use PhpParser\Node\Expr\Variable;
17 use Psy\Exception\ParseErrorException;
20 * Validate that the user did not call the language construct `empty()` on a
21 * statement in PHP < 5.5.
25 class LegacyEmptyPass extends CodeCleanerPass
27 private $atLeastPhp55;
29 public function __construct()
31 $this->atLeastPhp55 = \version_compare(PHP_VERSION, '5.5', '>=');
35 * Validate use of empty in PHP < 5.5.
37 * @throws ParseErrorException if the user used empty with anything but a variable
41 public function enterNode(Node $node)
43 if ($this->atLeastPhp55) {
47 if (!$node instanceof Empty_) {
51 if (!$node->expr instanceof Variable) {
52 $msg = \sprintf('syntax error, unexpected %s', $this->getUnexpectedThing($node->expr));
54 throw new ParseErrorException($msg, $node->expr->getLine());
58 private function getUnexpectedThing(Node $node)
60 switch ($node->getType()) {
62 case 'Scalar_LNumber':
63 case 'Scalar_DNumber':
64 return \json_encode($node->value);
66 case 'Expr_ConstFetch':
67 return (string) $node->name;
70 return $node->getType();