diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -61,6 +61,7 @@ const LINT_INNER_FUNCTION = 59; const LINT_DEFAULT_PARAMETERS = 60; const LINT_LOWERCASE_FUNCTIONS = 61; + const LINT_USELESS_OVERRIDING_METHOD = 62; private $blacklistedFunctions = array(); private $naminghook; @@ -191,6 +192,8 @@ => pht('Default Parameters'), self::LINT_LOWERCASE_FUNCTIONS => pht('Lowercase Functions'), + self::LINT_USELESS_OVERRIDING_METHOD + => pht('Useless Overriding Method'), ); } @@ -240,6 +243,7 @@ self::LINT_INNER_FUNCTION => $warning, self::LINT_DEFAULT_PARAMETERS => $warning, self::LINT_LOWERCASE_FUNCTIONS => $advice, + self::LINT_USELESS_OVERRIDING_METHOD => $advice, ); } @@ -307,7 +311,7 @@ public function getVersion() { // The version number should be incremented whenever a new rule is added. - return '24'; + return '25'; } protected function resolveFuture($path, Future $future) { @@ -395,6 +399,7 @@ 'lintInnerFunctions' => self::LINT_INNER_FUNCTION, 'lintDefaultParameters' => self::LINT_DEFAULT_PARAMETERS, 'lintLowercaseFunctions' => self::LINT_LOWERCASE_FUNCTIONS, + 'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD, ); foreach ($method_codes as $method => $codes) { @@ -3719,6 +3724,86 @@ } } + private function lintUselessOverridingMethods(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) { + $parameters[] = $parameter + ->getChildOfType(1, 'n_VARIABLE') + ->getConcreteString(); + } + + $statements = $method->getChildByIndex(5); + + 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, + self::LINT_USELESS_OVERRIDING_METHOD, + pht('Useless overriding method.')); + + } + } + /** * Retrieve all calls to some specified function(s). * diff --git a/src/lint/linter/__tests__/xhpast/useless-overriding-method.lint-test b/src/lint/linter/__tests__/xhpast/useless-overriding-method.lint-test new file mode 100644 --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/useless-overriding-method.lint-test @@ -0,0 +1,19 @@ +