]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Warn before using `strpos($a, $b) === 0`
authorvrana <jakubv@fb.com>
Wed, 15 Aug 2012 19:56:36 +0000 (12:56 -0700)
committervrana <jakubv@fb.com>
Wed, 15 Aug 2012 20:20:25 +0000 (13:20 -0700)
Summary:
I'm not sure if we are interested in this kind of linters ("Don't use slow function if there is a fast alternative").
I didn't find a case where `strpos() === 0` could be useful except [[ http://www.php.net/manual/en/mbstring.overload.php | mbstring.func_overload ]] craziness.

Test Plan:
New unit test.
Linted Arcanist, libphutil, Phabricator repositories, found no false positive and one real positive.

Reviewers: btrahan, epriestley

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D3296

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/slowness.lint-test [new file with mode: 0644]

index 372b7ddb4c1bc9b4e62c8d00e13b4b1dd816c468..40569074841245258fc95f5edf70afc069849367 100644 (file)
@@ -59,6 +59,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_PHT_WITH_DYNAMIC_STRING   = 33;
   const LINT_COMMENT_SPACING           = 34;
   const LINT_PHP_54_FEATURES           = 35;
+  const LINT_SLOWNESS                  = 36;
 
 
   public function getLintNameMap() {
@@ -97,6 +98,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_REUSED_AS_ITERATOR        => 'Variable Reused As Iterator',
       self::LINT_PHT_WITH_DYNAMIC_STRING   => 'Use of pht() on Dynamic String',
       self::LINT_COMMENT_SPACING           => 'Comment Spaces',
+      self::LINT_SLOWNESS                  => 'Slow Construct',
     );
   }
 
@@ -127,6 +129,8 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
         => ArcanistLintSeverity::SEVERITY_WARNING,
       self::LINT_PHT_WITH_DYNAMIC_STRING
         => ArcanistLintSeverity::SEVERITY_WARNING,
+      self::LINT_SLOWNESS
+        => ArcanistLintSeverity::SEVERITY_WARNING,
 
       self::LINT_COMMENT_SPACING
         => ArcanistLintSeverity::SEVERITY_ADVICE,
@@ -216,6 +220,50 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintPHP53Features($root);
     $this->lintPHP54Features($root);
     $this->lintPHT($root);
+    $this->lintStrposUsedForStart($root);
+  }
+
+  public function lintStrposUsedForStart($root) {
+    $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
+    foreach ($expressions as $expression) {
+      $operator = $expression->getChildOfType(1, 'n_OPERATOR');
+      $operator = $operator->getConcreteString();
+
+      if ($operator != '===' && $operator != '!==') {
+        continue;
+      }
+
+      $zero = $expression->getChildByIndex(0);
+      if ($zero->getTypeName() == 'n_NUMERIC_SCALAR' &&
+          $zero->getConcreteString() == '0') {
+        $strpos = $expression->getChildByIndex(2);
+      } else {
+        $strpos = $zero;
+        $zero = $expression->getChildByIndex(2);
+        if ($zero->getTypeName() != 'n_NUMERIC_SCALAR' ||
+            $zero->getConcreteString() != '0') {
+          continue;
+        }
+      }
+
+      if ($strpos->getTypeName() != 'n_FUNCTION_CALL') {
+        continue;
+      }
+
+      $name = strtolower($strpos->getChildByIndex(0)->getConcreteString());
+      if ($name == 'strpos') {
+        $this->raiseLintAtNode(
+          $strpos,
+          self::LINT_SLOWNESS,
+          "Use strncmp() for checking if the string starts with something.");
+      } else if ($name == 'stripos') {
+        $this->raiseLintAtNode(
+          $strpos,
+          self::LINT_SLOWNESS,
+          "Use strncasecmp() for checking if the string starts with ".
+            "something.");
+      }
+    }
   }
 
   public function lintPHT($root) {
diff --git a/src/lint/linter/__tests__/xhpast/slowness.lint-test b/src/lint/linter/__tests__/xhpast/slowness.lint-test
new file mode 100644 (file)
index 0000000..60da4d8
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+(strpos('a', 'b') === 0);
+(strpos('a', 'b') !== 0);
+(0 === strpos('a', 'b'));
+(1 + 0 === strpos('a', 'b'));
+~~~~~~~~~~
+warning:3:2
+warning:4:2
+warning:5:8