Chris@0: name instanceof Expr || $node->name instanceof Variable) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $name = (string) $node->name; Chris@0: Chris@0: if ($name === 'array_multisort') { Chris@0: return $this->validateArrayMultisort($node); Chris@0: } Chris@0: Chris@0: try { Chris@0: $refl = new \ReflectionFunction($name); Chris@0: } catch (\ReflectionException $e) { Chris@0: // Well, we gave it a shot! Chris@0: return; Chris@0: } Chris@0: Chris@0: foreach ($refl->getParameters() as $key => $param) { Chris@0: if (array_key_exists($key, $node->args)) { Chris@0: $arg = $node->args[$key]; Chris@0: if ($param->isPassedByReference() && !$this->isPassableByReference($arg)) { Chris@0: throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine()); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: private function isPassableByReference(Node $arg) Chris@0: { Chris@0: // FuncCall, MethodCall and StaticCall are all PHP _warnings_ not fatal errors, so we'll let Chris@0: // PHP handle those ones :) Chris@0: return $arg->value instanceof ClassConstFetch || Chris@0: $arg->value instanceof PropertyFetch || Chris@0: $arg->value instanceof Variable || Chris@0: $arg->value instanceof FuncCall || Chris@0: $arg->value instanceof MethodCall || Chris@0: $arg->value instanceof StaticCall; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Because array_multisort has a problematic signature... Chris@0: * Chris@0: * The argument order is all sorts of wonky, and whether something is passed Chris@0: * by reference or not depends on the values of the two arguments before it. Chris@0: * We'll do a good faith attempt at validating this, but err on the side of Chris@0: * permissive. Chris@0: * Chris@0: * This is why you don't design languages where core code and extensions can Chris@0: * implement APIs that wouldn't be possible in userland code. Chris@0: * Chris@0: * @throws FatalErrorException for clearly invalid arguments Chris@0: * Chris@0: * @param Node $node Chris@0: */ Chris@0: private function validateArrayMultisort(Node $node) Chris@0: { Chris@0: $nonPassable = 2; // start with 2 because the first one has to be passable by reference Chris@0: foreach ($node->args as $arg) { Chris@0: if ($this->isPassableByReference($arg)) { Chris@0: $nonPassable = 0; Chris@0: } elseif (++$nonPassable > 2) { Chris@0: // There can be *at most* two non-passable-by-reference args in a row. This is about Chris@0: // as close as we can get to validating the arguments for this function :-/ Chris@0: throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine()); Chris@0: } Chris@0: } Chris@0: } Chris@0: }