Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14775063
D12389.id29737.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D12389.id29737.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
@@ -58,6 +58,7 @@
const LINT_UNNECESSARY_SEMICOLON = 56;
const LINT_SELF_MEMBER_REFERENCE = 57;
const LINT_LOGICAL_OPERATORS = 58;
+ const LINT_INNER_FUNCTION = 59;
private $blacklistedFunctions = array();
private $naminghook;
@@ -129,6 +130,7 @@
self::LINT_UNNECESSARY_SEMICOLON => 'Unnecessary Semicolon',
self::LINT_SELF_MEMBER_REFERENCE => 'Self Member Reference',
self::LINT_LOGICAL_OPERATORS => 'Logical Operators',
+ self::LINT_INNER_FUNCTION => 'Inner Functions',
);
}
@@ -175,6 +177,7 @@
self::LINT_UNNECESSARY_SEMICOLON => $advice,
self::LINT_SELF_MEMBER_REFERENCE => $advice,
self::LINT_LOGICAL_OPERATORS => $advice,
+ self::LINT_INNER_FUNCTION => $warning,
);
}
@@ -242,7 +245,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '21';
+ return '22';
}
protected function resolveFuture($path, Future $future) {
@@ -325,6 +328,7 @@
'lintConstantDefinitions' => self::LINT_NAMING_CONVENTIONS,
'lintSelfMemberReference' => self::LINT_SELF_MEMBER_REFERENCE,
'lintLogicalOperators' => self::LINT_LOGICAL_OPERATORS,
+ 'lintInnerFunctions' => self::LINT_INNER_FUNCTION,
);
foreach ($method_codes as $method => $codes) {
@@ -3486,6 +3490,27 @@
}
}
+ private function lintInnerFunctions(XHPASTNode $root) {
+ $function_decls = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
+
+ foreach ($function_decls as $function_declaration) {
+ $inner_functions = $function_declaration
+ ->selectDescendantsOfType('n_FUNCTION_DECLARATION');
+
+ foreach ($inner_functions as $inner_function) {
+ if ($inner_function->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
+ // Anonymous closure.
+ continue;
+ }
+
+ $this->raiseLintAtNode(
+ $inner_function,
+ self::LINT_INNER_FUNCTION,
+ pht('Avoid the use of inner functions.'));
+ }
+ }
+ }
+
/**
* Retrieve all calls to some specified function(s).
*
diff --git a/src/lint/linter/__tests__/xhpast/inner-function.lint-test b/src/lint/linter/__tests__/xhpast/inner-function.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/inner-function.lint-test
@@ -0,0 +1,14 @@
+<?php
+
+function outer() {
+ if (!function_exists('inner')) {
+ function inner() {}
+ }
+}
+
+// Closures are allowed.
+function my_func($foo) {
+ function() {};
+}
+~~~~~~~~~~
+warning:5:5
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Jan 25, 2:32 AM (2 h, 58 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7045945
Default Alt Text
D12389.id29737.diff (2 KB)
Attached To
Mode
D12389: Add a linter rule for declaring nested functions
Attached
Detach File
Event Timeline
Log In to Comment