From 587f7440e3f9fc164f6969ca8dc008231d6c07aa Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Tue, 11 Aug 2015 06:49:54 +1000 Subject: [PATCH] Fix self member reference rule for PHP 5.3 Summary: Fixes T8674. Currently, `ArcanistSelfMemberReferenceXHPASTLinterRule` warns if a fully-qualified class name is used in place of `self`. This is fine in most cases, but in some specific scenarios fails for PHP 5.3 because `self` (and also `$this`) cannot be used in an anonymous closure (see [[http://php.net/manual/en/functions.anonymous.php | anonymous functions]] and [[https://wiki.php.net/rfc/closures/removal-of-this | removal of `$this` in closures]]). In order to do this, I also had to modify the manner in which configuration was passed to individual linter rule. Previously, it was only possible or an individual linter rule to be set with a configuration value. Once the linter rule had set this value, there was no means by which to allow this value to be shared amongst linter rules. Depends on D13819. Test Plan: Added unit tests. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin Maniphest Tasks: T8674 Differential Revision: https://secure.phabricator.com/D13820 --- src/lint/linter/ArcanistXHPASTLinter.php | 9 +++- .../self-member-references-php53.lint-test | 19 ++++++++ .../self-member-references-php54.lint-test | 30 ++++++++++++ .../xhpast/ArcanistXHPASTLinterRule.php | 48 ++++++++++++++++++- ...canistPHPCompatibilityXHPASTLinterRule.php | 31 ------------ ...istSelfMemberReferenceXHPASTLinterRule.php | 24 +++++++--- 6 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 src/lint/linter/__tests__/xhpast/self-member-references-php53.lint-test create mode 100644 src/lint/linter/__tests__/xhpast/self-member-references-php54.lint-test diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 3a036f2f..985f0950 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -67,14 +67,21 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } public function setLinterConfigurationValue($key, $value) { + $matched = false; + foreach ($this->rules as $rule) { foreach ($rule->getLinterConfigurationOptions() as $k => $spec) { if ($k == $key) { - return $rule->setLinterConfigurationValue($key, $value); + $matched = true; + $rule->setLinterConfigurationValue($key, $value); } } } + if ($matched) { + return; + } + return parent::setLinterConfigurationValue($key, $value); } diff --git a/src/lint/linter/__tests__/xhpast/self-member-references-php53.lint-test b/src/lint/linter/__tests__/xhpast/self-member-references-php53.lint-test new file mode 100644 index 00000000..5d06f638 --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/self-member-references-php53.lint-test @@ -0,0 +1,19 @@ +setAncestorClass(__CLASS__) @@ -47,10 +50,29 @@ abstract class ArcanistXHPASTLinterRule extends Phobject { } public function getLinterConfigurationOptions() { - return array(); + return array( + 'xhpast.php-version' => array( + 'type' => 'optional string', + 'help' => pht('PHP version to target.'), + ), + 'xhpast.php-version.windows' => array( + 'type' => 'optional string', + 'help' => pht('PHP version to target on Windows.'), + ), + ); } - public function setLinterConfigurationValue($key, $value) {} + public function setLinterConfigurationValue($key, $value) { + switch ($key) { + case 'xhpast.php-version': + $this->version = $value; + return; + + case 'xhpast.php-version.windows': + $this->windowsVersion = $value; + return; + } + } abstract public function process(XHPASTNode $root); @@ -138,6 +160,28 @@ abstract class ArcanistXHPASTLinterRule extends Phobject { /* -( Utility )------------------------------------------------------------ */ + /** + * Retrieve all anonymous closure(s). + * + * Returns all descendant nodes which represent an anonymous function + * declaration. + * + * @param XHPASTNode Root node. + * @return AASTNodeList + */ + protected function getAnonymousClosures(XHPASTNode $root) { + $func_decls = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); + $nodes = array(); + + foreach ($func_decls as $func_decl) { + if ($func_decl->getChildByIndex(2)->getTypeName() == 'n_EMPTY') { + $nodes[] = $func_decl; + } + } + + return AASTNodeList::newFromTreeAndNodes($root->getTree(), $nodes); + } + /** * Retrieve all calls to some specified function(s). * diff --git a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php index 7697356e..5ee6f3fe 100644 --- a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php @@ -5,41 +5,10 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule const ID = 45; - private $version; - private $windowsVersion; - public function getLintName() { return pht('PHP Compatibility'); } - public function getLinterConfigurationOptions() { - return parent::getLinterConfigurationOptions() + array( - 'xhpast.php-version' => array( - 'type' => 'optional string', - 'help' => pht('PHP version to target.'), - ), - 'xhpast.php-version.windows' => array( - 'type' => 'optional string', - 'help' => pht('PHP version to target on Windows.'), - ), - ); - } - - public function setLinterConfigurationValue($key, $value) { - switch ($key) { - case 'xhpast.php-version': - $this->version = $value; - return; - - case 'xhpast.php-version.windows': - $this->windowsVersion = $value; - return; - - default: - return parent::setLinterConfigurationValue($key, $value); - } - } - public function process(XHPASTNode $root) { static $compat_info; diff --git a/src/lint/linter/xhpast/rules/ArcanistSelfMemberReferenceXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistSelfMemberReferenceXHPASTLinterRule.php index 4c3399b7..bfec780e 100644 --- a/src/lint/linter/xhpast/rules/ArcanistSelfMemberReferenceXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistSelfMemberReferenceXHPASTLinterRule.php @@ -23,6 +23,7 @@ final class ArcanistSelfMemberReferenceXHPASTLinterRule $class_static_accesses = $class_declaration ->selectDescendantsOfType('n_CLASS_STATIC_ACCESS'); + $closures = $this->getAnonymousClosures($class_declaration); foreach ($class_static_accesses as $class_static_access) { $double_colons = $class_static_access @@ -35,12 +36,23 @@ final class ArcanistSelfMemberReferenceXHPASTLinterRule $class_ref_name = $class_ref->getConcreteString(); if (strtolower($class_name) == strtolower($class_ref_name)) { - $this->raiseLintAtNode( - $class_ref, - pht( - 'Use `%s` for local static member references.', - 'self::'), - 'self'); + $in_closure = false; + + foreach ($closures as $closure) { + if ($class_ref->isDescendantOf($closure)) { + $in_closure = true; + break; + } + } + + if (version_compare($this->version, '5.4.0', '>=') || !$in_closure) { + $this->raiseLintAtNode( + $class_ref, + pht( + 'Use `%s` for local static member references.', + 'self::'), + 'self'); + } } static $self_refs = array( -- 2.43.0