]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Minor fix for typecast linter rule
authorJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 03:12:53 +0000 (14:12 +1100)
committerJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 03:12:53 +0000 (14:12 +1100)
Summary: `intval($x, $y)` cannot be safely changed to `(int)$x` if `$y != 10`.

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D14611

src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php
src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/base.lint-test [new file with mode: 0644]

index 443697ce3727ff4f8ce56b6af83225a60c06d319..e39934accf04ddc45752858001696575cdac70db 100644 (file)
@@ -28,7 +28,7 @@ final class ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule
       $function_name = $cast
         ->getChildOfType(0, 'n_SYMBOL_NAME')
         ->getConcreteString();
-      $cast_name = $cast_functions[$function_name];
+      $cast_name = $cast_functions[strtolower($function_name)];
 
       $parameters  = $cast->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
       $replacement = null;
@@ -40,6 +40,23 @@ final class ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule
         $replacement = '('.$cast_name.')'.$parameter->getConcreteString();
       }
 
+      if (strtolower($function_name) == 'intval') {
+        if (count($parameters->getChildren()) >= 2) {
+          $base = $parameters->getChildByIndex(1);
+
+          if ($base->getTypeName() != 'n_NUMERIC_SCALAR') {
+            break;
+          }
+
+          if ($base->getConcreteString() != '10') {
+            continue;
+          }
+
+          $parameter   = $parameters->getChildByIndex(0);
+          $replacement = '('.$cast_name.')'.$parameter->getConcreteString();
+        }
+      }
+
       $this->raiseLintAtNode(
         $cast,
         pht(
diff --git a/src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/base.lint-test b/src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/base.lint-test
new file mode 100644 (file)
index 0000000..8514c10
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+intval($x);
+intval($x, 8);
+intval($x, 10);
+~~~~~~~~~~
+advice:2:1
+advice:4:1
+~~~~~~~~~~
+<?php
+(int)$x;
+intval($x, 8);
+(int)$x;