9793d8c4c5cec8b4e0caf02ff1fd8e7f6ff42ce9
[yaffs-website] / vendor / psy / psysh / src / CodeCleaner / LegacyEmptyPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\CodeCleaner;
13
14 use PhpParser\Node;
15 use PhpParser\Node\Expr\Empty_;
16 use PhpParser\Node\Expr\Variable;
17 use Psy\Exception\ParseErrorException;
18
19 /**
20  * Validate that the user did not call the language construct `empty()` on a
21  * statement in PHP < 5.5.
22  *
23  * @codeCoverageIgnore
24  */
25 class LegacyEmptyPass extends CodeCleanerPass
26 {
27     private $atLeastPhp55;
28
29     public function __construct()
30     {
31         $this->atLeastPhp55 = \version_compare(PHP_VERSION, '5.5', '>=');
32     }
33
34     /**
35      * Validate use of empty in PHP < 5.5.
36      *
37      * @throws ParseErrorException if the user used empty with anything but a variable
38      *
39      * @param Node $node
40      */
41     public function enterNode(Node $node)
42     {
43         if ($this->atLeastPhp55) {
44             return;
45         }
46
47         if (!$node instanceof Empty_) {
48             return;
49         }
50
51         if (!$node->expr instanceof Variable) {
52             $msg = \sprintf('syntax error, unexpected %s', $this->getUnexpectedThing($node->expr));
53
54             throw new ParseErrorException($msg, $node->expr->getLine());
55         }
56     }
57
58     private function getUnexpectedThing(Node $node)
59     {
60         switch ($node->getType()) {
61             case 'Scalar_String':
62             case 'Scalar_LNumber':
63             case 'Scalar_DNumber':
64                 return \json_encode($node->value);
65
66             case 'Expr_ConstFetch':
67                 return (string) $node->name;
68
69             default:
70                 return $node->getType();
71         }
72     }
73 }