Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14618440
D12419.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D12419.diff
View Options
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
@@ -62,6 +62,7 @@
const LINT_DEFAULT_PARAMETERS = 60;
const LINT_LOWERCASE_FUNCTIONS = 61;
const LINT_CLASS_NAME_LITERAL = 62;
+ const LINT_USELESS_OVERRIDING_METHOD = 63;
private $blacklistedFunctions = array();
private $naminghook;
@@ -194,6 +195,8 @@
=> pht('Lowercase Functions'),
self::LINT_CLASS_NAME_LITERAL
=> pht('Class Name Literal'),
+ self::LINT_USELESS_OVERRIDING_METHOD
+ => pht('Useless Overriding Method'),
);
}
@@ -244,6 +247,7 @@
self::LINT_DEFAULT_PARAMETERS => $warning,
self::LINT_LOWERCASE_FUNCTIONS => $advice,
self::LINT_CLASS_NAME_LITERAL => $advice,
+ self::LINT_USELESS_OVERRIDING_METHOD => $advice,
);
}
@@ -311,7 +315,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '25';
+ return '26';
}
protected function resolveFuture($path, Future $future) {
@@ -400,6 +404,7 @@
'lintDefaultParameters' => self::LINT_DEFAULT_PARAMETERS,
'lintLowercaseFunctions' => self::LINT_LOWERCASE_FUNCTIONS,
'lintClassNameLiteral' => self::LINT_CLASS_NAME_LITERAL,
+ 'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
);
foreach ($method_codes as $method => $codes) {
@@ -3766,6 +3771,85 @@
}
}
+ 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 @@
+<?php
+
+final class MyClass extends SomeOtherClass {
+ public function __construct() {
+ parent::__construct();
+ }
+
+ public function uselessMethod($x, array $y) {
+ return parent::uselessMethod($x, $y);
+ }
+
+ public function usefulMethod($x, array $y) {
+ return parent::usefulMethod($x, null);
+ }
+}
+~~~~~~~~~~
+error:3:13
+advice:4:3
+advice:8:3
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Jan 10, 7:49 PM (15 h, 4 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6983971
Default Alt Text
D12419.diff (4 KB)
Attached To
Mode
D12419: Add a linter rule for useless overriding methods
Attached
Detach File
Event Timeline
Log In to Comment