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\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.
23 class LegacyEmptyPass extends CodeCleanerPass
26 * Validate use of empty in PHP < 5.5.
28 * @throws ParseErrorException if the user used empty with anything but a variable
32 public function enterNode(Node $node)
34 if (version_compare(PHP_VERSION, '5.5', '>=')) {
38 if (!$node instanceof Empty_) {
42 if (!$node->expr instanceof Variable) {
43 $msg = sprintf('syntax error, unexpected %s', $this->getUnexpectedThing($node->expr));
45 throw new ParseErrorException($msg, $node->expr->getLine());
49 private function getUnexpectedThing(Node $node)
51 switch ($node->getType()) {
53 case 'Scalar_LNumber':
54 case 'Scalar_DNumber':
55 return json_encode($node->value);
57 case 'Expr_ConstFetch':
58 return (string) $node->name;
61 return $node->getType();