From 09052d52470d12eb395fab0dbe4e0d1da56d8765 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Wed, 2 Dec 2015 14:12:53 +1100 Subject: [PATCH] Minor fix for typecast linter rule 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 --- ...onCallShouldBeTypeCastXHPASTLinterRule.php | 19 ++++++++++++++++++- .../base.lint-test | 12 ++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/base.lint-test diff --git a/src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php index 443697ce..e39934ac 100644 --- a/src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php @@ -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 index 00000000..8514c10f --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/base.lint-test @@ -0,0 +1,12 @@ +