Skip to content

\settype() is not handled properly #3250

@kiler129

Description

@kiler129

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 */

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions