private function recordFunctionParameterTypesUsage(Node $node) { if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) { foreach ($node->getParams() as $param) { if ($param->type instanceof Node\Name) { $this->recordUsageOf($param->type); } if (is_string($param->type)) { $this->recordUsageOfByString($param->type); } } } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if (!$node instanceof Node\FunctionLike) { return; } $parametersNames = array(); foreach ($node->getParams() as $parameter) { $currentParameterName = $parameter->name; if (!isset($parametersNames[$currentParameterName])) { $parametersNames[$currentParameterName] = false; } elseif (!$parametersNames[$currentParameterName]) { $this->addContextMessage(sprintf('Duplicate function parameter name "%s"', $currentParameterName), $node); $parametersNames[$currentParameterName] = true; } } }
/** * {@inheritdoc} */ public function leaveNode(Node $node) { if ($node instanceof Node\FunctionLike) { $params = $node->getParams(); $paramNames = array(); $hasParameterWithSameName = false; array_walk($params, function (Node\Param $param) use(&$paramNames, &$hasParameterWithSameName) { if (array_key_exists($param->name, $paramNames)) { $hasParameterWithSameName = true; } $paramNames[$param->name] = true; }); if ($hasParameterWithSameName) { $this->errorCollection->add(new Error($this->parserContext->getFilename(), $node->getLine(), 'Functions can no longer have more than one parameter with the same name.')); } } }
/** * @inheritdoc * @param \PhpParser\Node $node * @return null|\PhpParser\Node|void */ public function enterNode(Node $node) { // direct class calls if ($node instanceof Node\Expr\New_ || $node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\ClassConstFetch || $node instanceof Node\Expr\StaticPropertyFetch) { if ($node->class instanceof Node\Name) { $this->handleClassName($node->class); } if ($node instanceof Node\Expr\ClassConstFetch) { $this->handleConstantName($node->name, $node->getLine()); } } elseif ($node instanceof Node\FunctionLike) { if (!empty($node->getParams())) { foreach ($node->getParams() as $param) { if (isset($param->type) && !is_string($param->type)) { $this->handleClassName($param->type); } } } } elseif ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) { $names = []; if (!empty($node->implements)) { $names += $node->implements; } if (!empty($node->extends)) { if ($node->extends instanceof Name) { $names[] = $node->extends; } else { $names += $node->extends; } } foreach ($names as $name) { $this->handleClassName($name); } } elseif ($node instanceof Node\Expr\FuncCall && !$node->name instanceof Node\Expr) { $this->handleFunctionName($node->name); } elseif ($node instanceof Node\Expr\ConstFetch) { $this->handleConstantName($node->name); } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\Namespace_) { $this->aliases = []; return; } if ($node instanceof Node\Stmt\Use_) { foreach ($node->uses as $use) { $this->aliases[$use->alias] = (string) $use->name; } return; } if ($node instanceof Node\Stmt\ClassMethod) { $params = $node->getParams(); if (!count($params)) { return; } $param1 = $params[0]; $paramClass = $this->resolveAlias((string) $param1->type); if (is_subclass_of($paramClass, '\\Symfony\\Component\\Validator\\Context\\ExecutionContextInterface')) { $this->contextVariable = $param1->name; } return; } if ($node instanceof Node\Expr\MethodCall) { $this->parseMethodCall($node); } }