]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule to detect call-time pass-by-reference
authorJoshua Spence <josh@joshuaspence.com>
Thu, 22 Jan 2015 20:28:55 +0000 (07:28 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Thu, 22 Jan 2015 20:56:58 +0000 (07:56 +1100)
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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test [new file with mode: 0644]

index bda26b9473c9e1cc8dc5e1aa35f3ba471fb38b48..b36e990bea76756bbc6c315c5ab3b1fb8e112c3f 100644 (file)
@@ -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 (file)
index 0000000..47289b6
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+class MyClass {
+    public function myfunc($var) {
+        echo $var;
+    }
+}
+
+$myvar = true;
+myfunc(&$myvar);
+myfunc($myvar);
+
+$this->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