From: Joshua Spence Date: Thu, 22 Jan 2015 20:28:55 +0000 (+1100) Subject: Add a linter rule to detect call-time pass-by-reference X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1a3afa44297f8268410797dc5195a51068ceb908;p=arcanist.git Add a linter rule to detect call-time pass-by-reference Summary: Call-time [[http://php.net/manual/en/language.references.pass.php | pass-by-reference]] is an awful "feature" of PHP that is deprecated and removed in PHP 5.4 (see http://us1.php.net/manual/en/migration54.incompatible.php). Add a linter rule to `ArcanistXHPASTLinter` to detect the use of this feature and raise an error. Test Plan: Added a test case for `arc unit`. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: epriestley, Korvin Differential Revision: https://secure.phabricator.com/D10537 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index bda26b94..b36e990b 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -52,6 +52,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { const LINT_DUPLICATE_SWITCH_CASE = 50; const LINT_BLACKLISTED_FUNCTION = 51; const LINT_IMPLICIT_VISIBILITY = 52; + const LINT_CALL_TIME_PASS_BY_REF = 53; private $blacklistedFunctions = array(); private $naminghook; @@ -116,6 +117,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_DUPLICATE_SWITCH_CASE => 'Duplicate Case Statements', self::LINT_BLACKLISTED_FUNCTION => 'Use of Blacklisted Function', self::LINT_IMPLICIT_VISIBILITY => 'Implicit Method Visibility', + self::LINT_CALL_TIME_PASS_BY_REF => 'Call-Time Pass-By-Reference', ); } @@ -214,7 +216,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { public function getVersion() { // The version number should be incremented whenever a new rule is added. - return '14'; + return '15'; } protected function resolveFuture($path, Future $future) { @@ -290,6 +292,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { 'lintBlacklistedFunction' => self::LINT_BLACKLISTED_FUNCTION, 'lintMethodModifier' => self::LINT_IMPLICIT_VISIBILITY, 'lintPropertyModifier' => self::LINT_IMPLICIT_VISIBILITY, + 'lintCallTimePassByReference' => self::LINT_CALL_TIME_PASS_BY_REF, ); foreach ($method_codes as $method => $codes) { @@ -3083,6 +3086,20 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } + private function lintCallTimePassByReference(XHPASTNode $root) { + $nodes = $root->selectDescendantsOfType('n_CALL_PARAMETER_LIST'); + + foreach ($nodes as $node) { + $parameters = $node->selectDescendantsOfType('n_VARIABLE_REFERENCE'); + + foreach ($parameters as $parameter) { + $this->raiseLintAtNode( + $parameter, + self::LINT_CALL_TIME_PASS_BY_REF, + pht('Call-time pass-by-reference calls are prohibited.')); + } + } + } public function getSuperGlobalNames() { return array( diff --git a/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test b/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test new file mode 100644 index 00000000..47289b6d --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test @@ -0,0 +1,30 @@ +myfunc(&$myvar); +$this->myfunc($myvar); + +MyClass::myfunc(&$myvar); +MyClass::myfunc($myvar); + +while (testfunc($var1, &$var2, $var3, &$var4) === false) {} + +sprintf('0%o', 0777 & $p); + +$foo(&$myvar); +~~~~~~~~~~ +error:2:7 XHP19 +error:9:8 +error:12:15 +error:15:17 +error:18:24 +error:18:39 +error:22:6