4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\CodeCleaner;
15 use PhpParser\Node\Expr;
16 use PhpParser\Node\Expr\ClassConstFetch;
17 use PhpParser\Node\Expr\FuncCall;
18 use PhpParser\Node\Expr\MethodCall;
19 use PhpParser\Node\Expr\PropertyFetch;
20 use PhpParser\Node\Expr\StaticCall;
21 use PhpParser\Node\Expr\Variable;
22 use Psy\Exception\FatalErrorException;
25 * Validate that only variables (and variable-like things) are passed by reference.
27 class PassableByReferencePass extends CodeCleanerPass
29 const EXCEPTION_MESSAGE = 'Only variables can be passed by reference';
32 * @throws FatalErrorException if non-variables are passed by reference
36 public function enterNode(Node $node)
38 // @todo support MethodCall and StaticCall as well.
39 if ($node instanceof FuncCall) {
40 // if function name is an expression or a variable, give it a pass for now.
41 if ($node->name instanceof Expr || $node->name instanceof Variable) {
45 $name = (string) $node->name;
47 if ($name === 'array_multisort') {
48 return $this->validateArrayMultisort($node);
52 $refl = new \ReflectionFunction($name);
53 } catch (\ReflectionException $e) {
54 // Well, we gave it a shot!
58 foreach ($refl->getParameters() as $key => $param) {
59 if (array_key_exists($key, $node->args)) {
60 $arg = $node->args[$key];
61 if ($param->isPassedByReference() && !$this->isPassableByReference($arg)) {
62 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
69 private function isPassableByReference(Node $arg)
71 // FuncCall, MethodCall and StaticCall are all PHP _warnings_ not fatal errors, so we'll let
72 // PHP handle those ones :)
73 return $arg->value instanceof ClassConstFetch ||
74 $arg->value instanceof PropertyFetch ||
75 $arg->value instanceof Variable ||
76 $arg->value instanceof FuncCall ||
77 $arg->value instanceof MethodCall ||
78 $arg->value instanceof StaticCall;
82 * Because array_multisort has a problematic signature...
84 * The argument order is all sorts of wonky, and whether something is passed
85 * by reference or not depends on the values of the two arguments before it.
86 * We'll do a good faith attempt at validating this, but err on the side of
89 * This is why you don't design languages where core code and extensions can
90 * implement APIs that wouldn't be possible in userland code.
92 * @throws FatalErrorException for clearly invalid arguments
96 private function validateArrayMultisort(Node $node)
98 $nonPassable = 2; // start with 2 because the first one has to be passable by reference
99 foreach ($node->args as $arg) {
100 if ($this->isPassableByReference($arg)) {
102 } elseif (++$nonPassable > 2) {
103 // There can be *at most* two non-passable-by-reference args in a row. This is about
104 // as close as we can get to validating the arguments for this function :-/
105 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());