-
Notifications
You must be signed in to change notification settings - Fork 519
fix parameter of strval, intval and floatval #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Khartir
wants to merge
6
commits into
phpstan:1.5.x
Choose a base branch
from
Khartir:fix-strval-parameter
base: 1.5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
41abe14
fix parameter type of *val-functions
Khartir cccde30
disallow Stringable in intval, floatval, doubleval
Khartir 0251dca
allow casting resource to float
Khartir 0238f94
fix expectedparametername in php < 8
Khartir 9918f6e
simplfy error listing
Khartir 3ab0c2b
disallow casting resource to float
Khartir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function count; | ||
use function in_array; | ||
use function sprintf; | ||
use function strtolower; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
class TypevalFamilyParametersRule implements Rule | ||
{ | ||
|
||
private const FUNCTIONS = [ | ||
'intval', | ||
'floatval', | ||
'doubleval', | ||
]; | ||
|
||
public function __construct( | ||
private RuleLevelHelper $ruleLevelHelper, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!($node->name instanceof Node\Name)) { | ||
return []; | ||
} | ||
$name = strtolower($node->name->toString()); | ||
if (!in_array($name, self::FUNCTIONS, true)) { | ||
return []; | ||
} | ||
if (count($node->getArgs()) === 0) { | ||
return []; | ||
} | ||
|
||
$paramTypeCallback = static fn (Type $type): Type => $type->toString(); | ||
|
||
$typeResult = $this->ruleLevelHelper->findTypeToCheck( | ||
$scope, | ||
$node->getArgs()[0]->value, | ||
'', | ||
static function (Type $type) use ($paramTypeCallback): bool { | ||
$paramType = $paramTypeCallback($type); | ||
|
||
return !$paramType instanceof ErrorType; | ||
}, | ||
); | ||
$type = $typeResult->getType(); | ||
$base = new ObjectType(''); | ||
|
||
if ($type instanceof ErrorType || !$base->isSuperTypeOf($type)->maybe()) { | ||
return []; | ||
} | ||
|
||
$paramType = $paramTypeCallback($type); | ||
// only check if a stringable object is given, everything else is handled by the parameter typehint | ||
if (!$paramType instanceof ErrorType) { | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Parameter #1 %s of function %s does not accept object, %s given.', | ||
PHP_VERSION_ID < 80000 ? '$var' : '$value', | ||
$name, | ||
$scope->getType($node->getArgs()[0]->value)->describe(VerbosityLevel::value()), | ||
))->line($node->getLine())->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
$array = []; | ||
$string = ''; | ||
$int = 1; | ||
$float = .1; | ||
$resource = fopen('./.env', 'r'); | ||
\assert(false !== $resource); | ||
$bool = true; | ||
$object = new stdClass(); | ||
$stringable = new class() { | ||
public function __toString(): string { | ||
return ''; | ||
} | ||
}; | ||
$null = null; | ||
/** @var mixed $mixed */ | ||
$mixed = null; | ||
|
||
echo (string) $array . PHP_EOL; | ||
echo strval($array) . PHP_EOL; | ||
|
||
echo (int) $array . PHP_EOL; | ||
echo intval($array) . PHP_EOL; | ||
|
||
echo (float) $array . PHP_EOL; | ||
echo floatval($array) . PHP_EOL; | ||
echo doubleval($array) . PHP_EOL; | ||
|
||
echo (string) $string . PHP_EOL; | ||
echo strval($string) . PHP_EOL; | ||
|
||
echo (int) $string . PHP_EOL; | ||
echo intval($string) . PHP_EOL; | ||
|
||
echo (float) $string . PHP_EOL; | ||
echo floatval($string) . PHP_EOL; | ||
echo doubleval($string) . PHP_EOL; | ||
|
||
echo (string) $int . PHP_EOL; | ||
echo strval($int) . PHP_EOL; | ||
|
||
echo (int) $int . PHP_EOL; | ||
echo intval($int) . PHP_EOL; | ||
|
||
echo (float) $int . PHP_EOL; | ||
echo floatval($int) . PHP_EOL; | ||
echo doubleval($int) . PHP_EOL; | ||
|
||
echo (string) $float . PHP_EOL; | ||
echo strval($float) . PHP_EOL; | ||
|
||
echo (int) $float . PHP_EOL; | ||
echo intval($float) . PHP_EOL; | ||
|
||
echo (float) $float . PHP_EOL; | ||
echo floatval($float) . PHP_EOL; | ||
echo doubleval($float) . PHP_EOL; | ||
|
||
echo (string) $resource . PHP_EOL; | ||
echo strval($resource) . PHP_EOL; | ||
|
||
echo (int) $resource . PHP_EOL; | ||
echo intval($resource) . PHP_EOL; | ||
|
||
echo (float) $resource . PHP_EOL; | ||
echo floatval($resource) . PHP_EOL; | ||
echo doubleval($resource) . PHP_EOL; | ||
|
||
echo (string) $bool . PHP_EOL; | ||
echo strval($bool) . PHP_EOL; | ||
|
||
echo (int) $bool . PHP_EOL; | ||
echo intval($bool) . PHP_EOL; | ||
|
||
echo (float) $bool . PHP_EOL; | ||
echo floatval($bool) . PHP_EOL; | ||
echo doubleval($bool) . PHP_EOL; | ||
|
||
echo (string) $object . PHP_EOL; | ||
echo strval($object) . PHP_EOL; | ||
|
||
echo (int) $object . PHP_EOL; | ||
echo intval($object) . PHP_EOL; | ||
|
||
echo (float) $object . PHP_EOL; | ||
echo floatval($object) . PHP_EOL; | ||
echo doubleval($object) . PHP_EOL; | ||
|
||
echo (string) $stringable . PHP_EOL; | ||
echo strval($stringable) . PHP_EOL; | ||
|
||
echo (int) $stringable . PHP_EOL; | ||
echo intval($stringable) . PHP_EOL; | ||
|
||
echo (float) $stringable . PHP_EOL; | ||
echo floatval($stringable) . PHP_EOL; | ||
echo doubleval($stringable) . PHP_EOL; | ||
|
||
echo (string) $mixed . PHP_EOL; | ||
echo strval($mixed) . PHP_EOL; | ||
|
||
echo (int) $mixed . PHP_EOL; | ||
echo intval($mixed) . PHP_EOL; | ||
|
||
echo (float) $mixed . PHP_EOL; | ||
echo floatval($mixed) . PHP_EOL; | ||
echo doubleval($mixed) . PHP_EOL; |
45 changes: 45 additions & 0 deletions
45
tests/PHPStan/Rules/Functions/TypevalFamilyParametersRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function sprintf; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<TypevalFamilyParametersRule> | ||
*/ | ||
class TypevalFamilyParametersRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createReflectionProvider(); | ||
return new TypevalFamilyParametersRule(new RuleLevelHelper($broker, true, false, true, false)); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$paramName = '$value'; | ||
if (PHP_VERSION_ID < 80000) { | ||
$paramName = '$var'; | ||
} | ||
$this->analyse([__DIR__ . '/data/typeval.php'], [ | ||
[ | ||
sprintf('Parameter #1 %s of function intval does not accept object, class@anonymous/tests/PHPStan/Rules/Functions/data/typeval.php:3 given.', $paramName), | ||
10, | ||
], | ||
[ | ||
sprintf('Parameter #1 %s of function floatval does not accept object, class@anonymous/tests/PHPStan/Rules/Functions/data/typeval.php:3 given.', $paramName), | ||
13, | ||
], | ||
[ | ||
sprintf('Parameter #1 %s of function doubleval does not accept object, class@anonymous/tests/PHPStan/Rules/Functions/data/typeval.php:3 given.', $paramName), | ||
16, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
$object = new stdClass(); | ||
$stringable = new class() { | ||
public function __toString(): string { | ||
return ''; | ||
} | ||
}; | ||
|
||
echo intval($object); | ||
echo intval($stringable); | ||
|
||
echo floatval($object); | ||
echo floatval($stringable); | ||
|
||
echo doubleval($object); | ||
echo doubleval($stringable); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way, to check if this is an instance of any object?