Skip to content
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

\settype() is not handled properly #3250

Open
kiler129 opened this issue May 5, 2020 · 3 comments
Open

\settype() is not handled properly #3250

kiler129 opened this issue May 5, 2020 · 3 comments
Labels
Milestone

Comments

@kiler129
Copy link
Contributor

kiler129 commented May 5, 2020

Bug report

PHPStan seems to be blind to typecasts done via \settype() and suggests that variable type didn't change.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
 * @return string|int|float|null Based on the specified casting
 */
function castTo(string $value, string $castTo)
{
	$newValue = $value;
	settype($newValue, $castTo);
	
	return $newValue;
}

https://phpstan.org/r/20eca26c-c9a6-458e-ab03-23cf27cd57eb

Expected output

I think if type cannot be inferred (like in the example above) the return type should be simply trusted. Additional think which will be even better is suggesting the user to add declaration before settype(), e.g.: /** @var string|int|float|bool|null $newValue */

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone May 5, 2020
@ondrejmirtes
Copy link
Member

This is a matter of writing a TypeSpecifyingExtension: https://phpstan.org/developing-extensions/type-specifying-extensions

PHPStan already contains plenty of those. Feel free to send a PR :)

@phpstan-bot
Copy link
Contributor

@kiler129 PHPStan now reports different result with your code snippet:

@@ @@
+PHP 7.4 – 8.0 (1 error)
+==========
+
+6: Function castTo() has no return typehint specified.
+
+PHP 7.3 (3 errors)
+==========
+
 6: Function castTo() never returns float so it can be removed from the return typehint.
 6: Function castTo() never returns int so it can be removed from the return typehint.
-6: Function castTo() never returns null so it can be removed from the return typehint.
+6: Function castTo() never returns null so it can be removed from the return typehint.
+
+PHP 7.1 – 7.2 (1 error)
+==========
+
+6: Function castTo() has no return typehint specified.
Full report

PHP 7.4 – 8.0 (1 error)

Line Error
6 Function castTo() has no return typehint specified.

PHP 7.3 (3 errors)

Line Error
6 Function castTo() has no return typehint specified.
6 Function castTo() has no return typehint specified.
6 Function castTo() has no return typehint specified.

PHP 7.1 – 7.2 (1 error)

Line Error
6 Function castTo() never returns null so it can be removed from the return typehint.

@phpstan-bot
Copy link
Contributor

@kiler129 After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-6: Function castTo() never returns float so it can be removed from the return typehint.
-6: Function castTo() never returns int so it can be removed from the return typehint.
-6: Function castTo() never returns null so it can be removed from the return typehint.
+6: Function castTo() never returns float so it can be removed from the return type.
+6: Function castTo() never returns int so it can be removed from the return type.
+6: Function castTo() never returns null so it can be removed from the return type.
Full report
Line Error
6 Function castTo() never returns float so it can be removed from the return type.
6 Function castTo() never returns int so it can be removed from the return type.
6 Function castTo() never returns null so it can be removed from the return type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants