1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-29 10:12:41 +01:00

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
This commit is contained in:
Joshua Spence 2015-12-02 14:12:53 +11:00
parent 00efcd294f
commit 09052d5247
2 changed files with 30 additions and 1 deletions

View file

@ -28,7 +28,7 @@ final class ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule
$function_name = $cast $function_name = $cast
->getChildOfType(0, 'n_SYMBOL_NAME') ->getChildOfType(0, 'n_SYMBOL_NAME')
->getConcreteString(); ->getConcreteString();
$cast_name = $cast_functions[$function_name]; $cast_name = $cast_functions[strtolower($function_name)];
$parameters = $cast->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); $parameters = $cast->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
$replacement = null; $replacement = null;
@ -40,6 +40,23 @@ final class ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule
$replacement = '('.$cast_name.')'.$parameter->getConcreteString(); $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( $this->raiseLintAtNode(
$cast, $cast,
pht( pht(

View file

@ -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;