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

TypeDoesNotContainType - handle PHP type toggles #2266

Closed
SpacePossum opened this issue Oct 23, 2019 · 4 comments
Closed

TypeDoesNotContainType - handle PHP type toggles #2266

SpacePossum opened this issue Oct 23, 2019 · 4 comments
Labels

Comments

@SpacePossum
Copy link

Hi all,

https://psalm.dev/r/1944680e7d

<?php

function closeFile($file)
{
    fclose($file);
}

$file = fopen(__FILE__, 'r');

if (false === $file) {
    throw new \RuntimeException('foo');
}

echo "\n".gettype($file);

closeFile($file);

echo "\n".gettype($file);

if (!is_resource($file)) {
    echo "\nNot a resource";
}

output:

$ php var/tmp/psalm/test2.php 

resource
resource (closed)
Not a resource
ERROR: TypeDoesNotContainType - 20:6 - Found a contradiction when evaluating $file and trying to reconcile type 'resource' to !resource

I think this error is not correct. When running the code the type of $file becomes resource when opened and gets changed to resource (closed) because of the fclose call on it. As such the !is_resource check seems valid.

The example code is over simplified, a production sample I've got is when guzzle closes the file handle on successful download.

@muglug
Copy link
Collaborator

muglug commented Oct 23, 2019

WTF PHP

@SpacePossum
Copy link
Author

This variant also invalid triggers;

ERROR: RedundantCondition - var/tmp/psalm/test2.php:21:5 - Found a redundant condition when evaluating $file and trying to reconcile type 'resource' to resource
if (is_resource($file)) {
<?php

function closeFile($file)
{
    fclose($file);
}


$file = fopen(__FILE__, 'r');

if (false === $file) {
    throw new \RuntimeException('');
}

try {
    closeFile($file);
} catch (\Exception $e) {
    throw $e;
}

if (is_resource($file)) {

} else {
    echo 'not an resource';
}

@SpacePossum
Copy link
Author

SpacePossum commented Oct 23, 2019

yeah, the type juggling of PHP with resources is ... ehmmm.. a bit of a pain for SCA : (

@muglug muglug added the bug label Oct 23, 2019
@muglug
Copy link
Collaborator

muglug commented Oct 23, 2019

This is truly insane

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

2 participants