mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-25 16:22:42 +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:
parent
00efcd294f
commit
09052d5247
2 changed files with 30 additions and 1 deletions
|
@ -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(
|
||||
|
|
|
@ -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;
|
Loading…
Reference in a new issue