Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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\Array_;
16
use PhpParser\Node\Expr\BinaryOp;
17
use PhpParser\Node\Expr\ClassConstFetch;
18
use PhpParser\Node\Expr\ConstFetch;
19
use PhpParser\Node\Expr\Instanceof_;
20
use PhpParser\Node\Scalar;
21
use PhpParser\Node\Scalar\Encapsed;
22
use Psy\Exception\FatalErrorException;
23
 
24
/**
25
 * Validate that the instanceof statement does not receive a scalar value or a non-class constant.
26
 *
27
 * @author Martin Hasoň <martin.hason@gmail.com>
28
 */
29
class InstanceOfPass extends CodeCleanerPass
30
{
31
    const EXCEPTION_MSG = 'instanceof expects an object instance, constant given';
32
 
33
    private $atLeastPhp73;
34
 
35
    public function __construct()
36
    {
37
        $this->atLeastPhp73 = \version_compare(\PHP_VERSION, '7.3', '>=');
38
    }
39
 
40
    /**
41
     * Validate that the instanceof statement does not receive a scalar value or a non-class constant.
42
     *
43
     * @throws FatalErrorException if a scalar or a non-class constant is given
44
     *
45
     * @param Node $node
46
     *
47
     * @return int|Node|null Replacement node (or special return value)
48
     */
49
    public function enterNode(Node $node)
50
    {
51
        // Basically everything is allowed in PHP 7.3 :)
52
        if ($this->atLeastPhp73) {
53
            return;
54
        }
55
 
56
        if (!$node instanceof Instanceof_) {
57
            return;
58
        }
59
 
60
        if (($node->expr instanceof Scalar && !$node->expr instanceof Encapsed) ||
61
            $node->expr instanceof BinaryOp ||
62
            $node->expr instanceof Array_ ||
63
            $node->expr instanceof ConstFetch ||
64
            $node->expr instanceof ClassConstFetch
65
        ) {
66
            throw new FatalErrorException(self::EXCEPTION_MSG, 0, \E_ERROR, null, $node->getLine());
67
        }
68
    }
69
}