]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Raise a lint warning on array_combine(x, x)
authorepriestley <git@epriestley.com>
Sat, 26 Jan 2013 01:06:49 +0000 (17:06 -0800)
committerepriestley <git@epriestley.com>
Sat, 26 Jan 2013 01:06:49 +0000 (17:06 -0800)
Summary: Suggest array_fuse().

Test Plan:
Unit tests, and:

  $ arc lint --lintall src/unit/engine/phutil/ArcanistPhutilTestCase.php
  >>> Lint for src/unit/engine/phutil/ArcanistPhutilTestCase.php:

     Warning  (XHP37) array_combine() Unreliable
      Prior to PHP 5.4, array_combine() fails when given empty arrays. Prefer
      to write array_combine(x, x) as array_fuse(x).

               252     $callable,
               253     $exception_class = 'Exception') {
               254     return $this->tryTestCases(
      >>>      255       array_combine(array_keys($map), array_keys($map)),
               256       array_values($map),
               257       $callable,
               258       $exception_class);

Reviewers: btrahan, vrana, chad

Reviewed By: btrahan

CC: aran
Differential Revision: https://secure.phabricator.com/D4659

src/lint/engine/PhutilLintEngine.php
src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/array-combine.lint-test [new file with mode: 0644]
src/unit/engine/phutil/ArcanistPhutilTestCase.php

index 0c806b79814075e5a9d4800ed4bee6c4166c5b7c..e7c897dbd1af8f4da70e7b225c579ce9b121f66d 100644 (file)
@@ -65,6 +65,7 @@ class PhutilLintEngine extends ArcanistLintEngine {
       ArcanistXHPASTLinter::LINT_COMMENT_SPACING          => $error,
       ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE    => $warning,
       ArcanistXHPASTLinter::LINT_TODO_COMMENT             => $advice,
+      ArcanistXHPASTLinter::LINT_ARRAY_COMBINE            => $warning,
     );
   }
 }
index 8c19dab552b1ff9af27414593cadbf1540f8712f..63b3c77d434460a6b10107d4a0487e5f91ea3529 100644 (file)
@@ -44,6 +44,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_COMMENT_SPACING           = 34;
   const LINT_PHP_54_FEATURES           = 35;
   const LINT_SLOWNESS                  = 36;
+  const LINT_ARRAY_COMBINE             = 37;
 
 
   public function getLintNameMap() {
@@ -83,6 +84,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_PHT_WITH_DYNAMIC_STRING   => 'Use of pht() on Dynamic String',
       self::LINT_COMMENT_SPACING           => 'Comment Spaces',
       self::LINT_SLOWNESS                  => 'Slow Construct',
+      self::LINT_ARRAY_COMBINE             => 'array_combine() Unreliable',
     );
   }
 
@@ -118,6 +120,10 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       // a specific minimum version.
       self::LINT_PHP_53_FEATURES           => $disabled,
       self::LINT_PHP_54_FEATURES           => $disabled,
+
+      // This message specifically recommends array_fuse(), a libphutil
+      // function.
+      self::LINT_ARRAY_COMBINE             => $disabled,
     );
   }
 
@@ -203,6 +209,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintPHT($root);
     $this->lintStrposUsedForStart($root);
     $this->lintStrstrUsedForCheck($root);
+    $this->lintArrayCombine($root);
   }
 
   public function lintStrstrUsedForCheck($root) {
@@ -1799,6 +1806,32 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     }
   }
 
+  protected function lintArrayCombine($root) {
+    $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
+    foreach ($function_calls as $call) {
+      $name = $call->getChildByIndex(0)->getConcreteString();
+      if (strtolower($name) === 'array_combine') {
+        $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+        if (count($parameter_list->getChildren()) !== 2) {
+          // Wrong number of parameters, but raise that elsewhere if we want.
+          continue;
+        }
+
+        $first = $parameter_list->getChildByIndex(0);
+        $second = $parameter_list->getChildByIndex(1);
+
+        if ($first->getConcreteString() == $second->getConcreteString()) {
+          $this->raiseLintAtNode(
+            $call,
+            self::LINT_ARRAY_COMBINE,
+            'Prior to PHP 5.4, array_combine() fails when given empty '.
+            'arrays. Prefer to write array_combine(x, x) as array_fuse(x).');
+        }
+      }
+    }
+  }
+
+
   /**
    * Exit is parsed as an expression, but using it as such is almost always
    * wrong. That is, this is valid:
diff --git a/src/lint/linter/__tests__/xhpast/array-combine.lint-test b/src/lint/linter/__tests__/xhpast/array-combine.lint-test
new file mode 100644 (file)
index 0000000..abcaac7
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+
+array_combine($x, $x);
+array_combine($x, $y);
+~~~~~~~~~~
+disabled:3:1
index e109c65a9843742a2d9271c361fac0b7b0179eab..57314bef859cc73129bcdbb55a50ccc27d9006fd 100644 (file)
@@ -252,7 +252,7 @@ abstract class ArcanistPhutilTestCase {
     $callable,
     $exception_class = 'Exception') {
     return $this->tryTestCases(
-      array_combine(array_keys($map), array_keys($map)),
+      array_fuse(array_keys($map)),
       array_values($map),
       $callable,
       $exception_class);