Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15483803
D12855.id30925.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
D12855.id30925.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
@@ -64,6 +64,7 @@
const LINT_CLASS_NAME_LITERAL = 62;
const LINT_USELESS_OVERRIDING_METHOD = 63;
const LINT_NO_PARENT_SCOPE = 64;
+ const LINT_INVALID_DEFAULT_PARAMETER = 65;
private $blacklistedFunctions = array();
private $naminghook;
@@ -200,6 +201,8 @@
=> pht('Useless Overriding Method'),
self::LINT_NO_PARENT_SCOPE
=> pht('No Parent Scope'),
+ self::LINT_INVALID_DEFAULT_PARAMETER
+ => pht('Invalid Default Parameter'),
);
}
@@ -318,7 +321,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '26';
+ return '27';
}
protected function resolveFuture($path, Future $future) {
@@ -409,6 +412,7 @@
'lintClassNameLiteral' => self::LINT_CLASS_NAME_LITERAL,
'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
+ 'lintInvalidDefaultParameters' => self::LINT_INVALID_DEFAULT_PARAMETER,
);
foreach ($method_codes as $method => $codes) {
@@ -3887,6 +3891,76 @@
}
}
+ private function lintInvalidDefaultParameters(XHPASTNode $root) {
+ $parameters = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER');
+
+ foreach ($parameters as $parameter) {
+ $type = $parameter->getChildByIndex(0);
+ $default = $parameter->getChildByIndex(2);
+
+ if ($type->getTypeName() == 'n_EMPTY') {
+ continue;
+ }
+
+ if ($default->getTypeName() == 'n_EMPTY') {
+ continue;
+ }
+
+ $default_is_null = $default->getTypeName() == 'n_SYMBOL_NAME' &&
+ strtolower($default->getConcreteString()) == 'null';
+
+ switch (strtolower($type->getConcreteString())) {
+ case 'array':
+ if ($default->getTypeName() == 'n_ARRAY_LITERAL') {
+ break;
+ }
+ if ($default_is_null) {
+ break;
+ }
+
+ $this->raiseLintAtNode(
+ $default,
+ self::LINT_INVALID_DEFAULT_PARAMETER,
+ pht(
+ 'Default value for parameters with %s type hint '.
+ 'can only be an %s or %s.',
+ 'array',
+ 'array',
+ 'null'));
+ break;
+
+ case 'callable':
+ if ($default_is_null) {
+ break;
+ }
+
+ $this->raiseLintAtNode(
+ $default,
+ self::LINT_INVALID_DEFAULT_PARAMETER,
+ pht(
+ 'Default value for parameters with %s type hint can only be %s.',
+ 'callable',
+ 'null'));
+ break;
+
+ default:
+ // Class/interface parameter.
+ if ($default_is_null) {
+ break;
+ }
+
+ $this->raiseLintAtNode(
+ $default,
+ self::LINT_INVALID_DEFAULT_PARAMETER,
+ pht(
+ 'Default value for parameters with a class type hint '.
+ 'can only be %s.',
+ 'null'));
+ break;
+ }
+ }
+ }
+
/**
* Retrieve all calls to some specified function(s).
diff --git a/src/lint/linter/__tests__/xhpast/invalid-default-paramter.lint-test b/src/lint/linter/__tests__/xhpast/invalid-default-paramter.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/invalid-default-paramter.lint-test
@@ -0,0 +1,17 @@
+<?php
+function func_one(array $x) {}
+function func_two(array $x = null) {}
+function func_three(array $x = array()) {}
+function func_four(array $x = false) {}
+
+function func_five(callable $x) {}
+function func_six(callable $x = null) {}
+function func_seven(callable $x = false) {}
+
+function func_eight(stdClass $x) {}
+function func_nine(stdClass $x = null) {}
+function func_ten(stdClass $x = array()) {}
+~~~~~~~~~~
+error:5:31
+error:9:35
+error:13:33
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Apr 10, 2:31 PM (5 d, 3 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7710278
Default Alt Text
D12855.id30925.diff (4 KB)
Attached To
Mode
D12855: Add a linter rule for invalid default parameters
Attached
Detach File
Event Timeline
Log In to Comment