Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 200 | 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\ArrayDimFetch;
16
use PhpParser\Node\Expr\Assign;
17
use Psy\Exception\FatalErrorException;
18
 
19
/**
20
 * Validate empty brackets are only used for assignment.
21
 */
22
class EmptyArrayDimFetchPass extends CodeCleanerPass
23
{
24
    const EXCEPTION_MESSAGE = 'Cannot use [] for reading';
25
 
26
    private $theseOnesAreFine = [];
27
 
28
    /**
29
     * @return Node[]|null Array of nodes
30
     */
31
    public function beforeTraverse(array $nodes)
32
    {
33
        $this->theseOnesAreFine = [];
34
    }
35
 
36
    /**
37
     * @throws FatalErrorException if the user used empty empty array dim fetch outside of assignment
38
     *
39
     * @param Node $node
40
     *
41
     * @return int|Node|null Replacement node (or special return value)
42
     */
43
    public function enterNode(Node $node)
44
    {
45
        if ($node instanceof Assign && $node->var instanceof ArrayDimFetch) {
46
            $this->theseOnesAreFine[] = $node->var;
47
        }
48
 
49
        if ($node instanceof ArrayDimFetch && $node->dim === null) {
50
            if (!\in_array($node, $this->theseOnesAreFine)) {
51
                throw new FatalErrorException(self::EXCEPTION_MESSAGE, $node->getLine());
52
            }
53
        }
54
    }
55
}