Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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;
16
use PhpParser\Node\Expr\Array_;
17
use PhpParser\Node\Expr\ArrayDimFetch;
18
use PhpParser\Node\Expr\ClassConstFetch;
19
use PhpParser\Node\Expr\FuncCall;
20
use PhpParser\Node\Expr\MethodCall;
21
use PhpParser\Node\Expr\PropertyFetch;
22
use PhpParser\Node\Expr\StaticCall;
23
use PhpParser\Node\Expr\Variable;
24
use Psy\Exception\FatalErrorException;
25
 
26
/**
27
 * Validate that only variables (and variable-like things) are passed by reference.
28
 */
29
class PassableByReferencePass extends CodeCleanerPass
30
{
31
    const EXCEPTION_MESSAGE = 'Only variables can be passed by reference';
32
 
33
    /**
34
     * @throws FatalErrorException if non-variables are passed by reference
35
     *
36
     * @param Node $node
37
     *
38
     * @return int|Node|null Replacement node (or special return value)
39
     */
40
    public function enterNode(Node $node)
41
    {
42
        // @todo support MethodCall and StaticCall as well.
43
        if ($node instanceof FuncCall) {
44
            // if function name is an expression or a variable, give it a pass for now.
45
            if ($node->name instanceof Expr || $node->name instanceof Variable) {
46
                return;
47
            }
48
 
49
            $name = (string) $node->name;
50
 
51
            if ($name === 'array_multisort') {
52
                return $this->validateArrayMultisort($node);
53
            }
54
 
55
            try {
56
                $refl = new \ReflectionFunction($name);
57
            } catch (\ReflectionException $e) {
58
                // Well, we gave it a shot!
59
                return;
60
            }
61
 
62
            foreach ($refl->getParameters() as $key => $param) {
63
                if (\array_key_exists($key, $node->args)) {
64
                    $arg = $node->args[$key];
65
                    if ($param->isPassedByReference() && !$this->isPassableByReference($arg)) {
66
                        throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, \E_ERROR, null, $node->getLine());
67
                    }
68
                }
69
            }
70
        }
71
    }
72
 
73
    private function isPassableByReference(Node $arg): bool
74
    {
75
        // Unpacked arrays can be passed by reference
76
        if ($arg->value instanceof Array_) {
77
            return $arg->unpack;
78
        }
79
 
80
        // FuncCall, MethodCall and StaticCall are all PHP _warnings_ not fatal errors, so we'll let
81
        // PHP handle those ones :)
82
        return $arg->value instanceof ClassConstFetch ||
83
            $arg->value instanceof PropertyFetch ||
84
            $arg->value instanceof Variable ||
85
            $arg->value instanceof FuncCall ||
86
            $arg->value instanceof MethodCall ||
87
            $arg->value instanceof StaticCall ||
88
            $arg->value instanceof ArrayDimFetch;
89
    }
90
 
91
    /**
92
     * Because array_multisort has a problematic signature...
93
     *
94
     * The argument order is all sorts of wonky, and whether something is passed
95
     * by reference or not depends on the values of the two arguments before it.
96
     * We'll do a good faith attempt at validating this, but err on the side of
97
     * permissive.
98
     *
99
     * This is why you don't design languages where core code and extensions can
100
     * implement APIs that wouldn't be possible in userland code.
101
     *
102
     * @throws FatalErrorException for clearly invalid arguments
103
     *
104
     * @param Node $node
105
     */
106
    private function validateArrayMultisort(Node $node)
107
    {
108
        $nonPassable = 2; // start with 2 because the first one has to be passable by reference
109
        foreach ($node->args as $arg) {
110
            if ($this->isPassableByReference($arg)) {
111
                $nonPassable = 0;
112
            } elseif (++$nonPassable > 2) {
113
                // There can be *at most* two non-passable-by-reference args in a row. This is about
114
                // as close as we can get to validating the arguments for this function :-/
115
                throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, \E_ERROR, null, $node->getLine());
116
            }
117
        }
118
    }
119
}