Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / LegacyEmptyPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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 class LegacyEmptyPass extends CodeCleanerPass
24 {
25     /**
26      * Validate use of empty in PHP < 5.5.
27      *
28      * @throws ParseErrorException if the user used empty with anything but a variable
29      *
30      * @param Node $node
31      */
32     public function enterNode(Node $node)
33     {
34         if (version_compare(PHP_VERSION, '5.5', '>=')) {
35             return;
36         }
37
38         if (!$node instanceof Empty_) {
39             return;
40         }
41
42         if (!$node->expr instanceof Variable) {
43             $msg = sprintf('syntax error, unexpected %s', $this->getUnexpectedThing($node->expr));
44
45             throw new ParseErrorException($msg, $node->expr->getLine());
46         }
47     }
48
49     private function getUnexpectedThing(Node $node)
50     {
51         switch ($node->getType()) {
52             case 'Scalar_String':
53             case 'Scalar_LNumber':
54             case 'Scalar_DNumber':
55                 return json_encode($node->value);
56
57             case 'Expr_ConstFetch':
58                 return (string) $node->name;
59
60             default:
61                 return $node->getType();
62         }
63     }
64 }