| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 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\ArrayDimFetch;
|
|
|
16 |
use PhpParser\Node\Expr\Isset_;
|
|
|
17 |
use PhpParser\Node\Expr\NullsafePropertyFetch;
|
|
|
18 |
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
19 |
use PhpParser\Node\Expr\Variable;
|
|
|
20 |
use Psy\Exception\FatalErrorException;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Code cleaner pass to ensure we only allow variables, array fetch and property
|
|
|
24 |
* fetch expressions in isset() calls.
|
|
|
25 |
*/
|
|
|
26 |
class IssetPass extends CodeCleanerPass
|
|
|
27 |
{
|
|
|
28 |
const EXCEPTION_MSG = 'Cannot use isset() on the result of an expression (you can use "null !== expression" instead)';
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* @throws FatalErrorException
|
|
|
32 |
*
|
|
|
33 |
* @param Node $node
|
|
|
34 |
*
|
|
|
35 |
* @return int|Node|null Replacement node (or special return value)
|
|
|
36 |
*/
|
|
|
37 |
public function enterNode(Node $node)
|
|
|
38 |
{
|
|
|
39 |
if (!$node instanceof Isset_) {
|
|
|
40 |
return;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
foreach ($node->vars as $var) {
|
|
|
44 |
if (!$var instanceof Variable && !$var instanceof ArrayDimFetch && !$var instanceof PropertyFetch && !$var instanceof NullsafePropertyFetch) {
|
|
|
45 |
throw new FatalErrorException(self::EXCEPTION_MSG, 0, \E_ERROR, null, $node->getLine());
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
}
|