1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2025-03-29 04:28:11 +01:00
phorge-arcanist/src/lint/linter/xhpast/rules/ArcanistUselessOverridingMethodXHPASTLinterRule.php
Richard van Velzen 3ffed59bd7 Update xhpast linter rules for new function AST format
Summary: See D15678. A node containing a return type hint is added right before the statement body node. This updates all relevant linter rules to now retrieve the body from the correct index.

Test Plan: Ran `arc unit --everything`, inspected every single message to make sure all xhpast test cases succeeded. Also grepped for `getChildByIndex(5` and `getChildOfType(5`.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D15814
2016-04-29 14:38:10 +02:00

102 lines
2.5 KiB
PHP

<?php
final class ArcanistUselessOverridingMethodXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 63;
public function getLintName() {
return pht('Useless Overriding Method');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_ADVICE;
}
public function process(XHPASTNode $root) {
$methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$method_name = $method
->getChildOfType(2, 'n_STRING')
->getConcreteString();
$parameter_list = $method
->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
$parameters = array();
foreach ($parameter_list->getChildren() as $parameter) {
$default = $parameter->getChildByIndex(2);
$parameter = $parameter->getChildByIndex(1);
if ($parameter->getTypeName() == 'n_VARIABLE_REFERENCE') {
$parameter = $parameter->getChildOfType(0, 'n_VARIABLE');
}
if ($default->getTypeName() != 'n_EMPTY') {
continue 2;
}
$parameters[] = $parameter->getConcreteString();
}
$statements = $method->getChildByIndex(6);
if ($statements->getTypeName() != 'n_STATEMENT_LIST') {
continue;
}
if (count($statements->getChildren()) != 1) {
continue;
}
$statement = $statements
->getChildOfType(0, 'n_STATEMENT')
->getChildByIndex(0);
if ($statement->getTypeName() == 'n_RETURN') {
$statement = $statement->getChildByIndex(0);
}
if ($statement->getTypeName() != 'n_FUNCTION_CALL') {
continue;
}
$function = $statement->getChildByIndex(0);
if ($function->getTypeName() != 'n_CLASS_STATIC_ACCESS') {
continue;
}
$called_class = $function->getChildOfType(0, 'n_CLASS_NAME');
$called_method = $function->getChildOfType(1, 'n_STRING');
if ($called_class->getConcreteString() != 'parent') {
continue;
} else if ($called_method->getConcreteString() != $method_name) {
continue;
}
$params = $statement
->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
->getChildren();
foreach ($params as $param) {
if ($param->getTypeName() != 'n_VARIABLE') {
continue 2;
}
$expected = array_shift($parameters);
if ($param->getConcreteString() != $expected) {
continue 2;
}
}
$this->raiseLintAtNode(
$method,
pht('Useless overriding method.'));
}
}
}