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