Skip to content

Calling trim() narrows variable type to string #13552

@derrabus

Description

@derrabus

Bug report

After upgrading from 2.1.27 to 2.1.28, PHPStan started to report strange false positives on my projects. Apparently, when comparing the result of a trim($value) operation, the $value variable is narrowed to a string.

function doSomething(mixed $value): void
{
	if (trim($value) === '') {
		return;
	}
	\PHPStan\dumpType($value);
}

Here, PHPStan believes that $value must be a non-empty-string which I believe is wrong. $value could be anything that can be cast to a string, such as an integer or a Stringable object.

Code snippet that reproduces the problem

https://phpstan.org/r/8cbb0de1-d0c3-4c13-bcf5-7cbd1b1fd69a

Expected output

$value should still be mixed.

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions