How do you handle PHP 8 resources-which-got-converted-to-objects? #4212
-
For example this: https://phpstan.org/r/6463971d-0d60-4183-b577-d486bf602e63 On PHP 8, socket_create returns a Socket. Should one type |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
I was trying to convice Ondrej to provide php-version-aware ignore comments to allow this but it seems that Ondrej's preferred way is including proper configs with ignored errors. No-go for me. |
Beta Was this translation helpful? Give feedback.
-
Hi, Currently the best way to run PHPStan on such codebases is to run it twice, for both PHP 7 and PHP 8. You'll have to run it with different config files as some code is marked as invalid for PHP 7 (for example the Socket class doesn't exist there), and some code is not necessary on PHP 8, like checking for Unfortunately that's currently the best way to do it, but not ideal. PHPStan currently supports only a single PHP version to consider for a codebase, not a range, as I explained here at length: #4060 (comment) At least you can reuse the common parts of configuration files with I also promised that if there's enough requests for this, I'll make the considered PHP versions a range, so that, but it will require some rewriting of PHPStan core. |
Beta Was this translation helpful? Give feedback.
-
My preferred solution (which avoids masking false positives): phpstan.php7.neon
phpstan.php8.neon:
and then annotate using |
Beta Was this translation helpful? Give feedback.
-
@ondrejmirtes would it be possible to make PHPStan ignore content inside blocks guarded by version checks? /** @var mixed */
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (version_compare(PHP_VERSION, '8.0.0', '<') {
if (!is_resource($socket)) return;
} else {
if (!$socket instanceof \Socket) return;
}
socket_accept($socket); That way we can have each run use the same config and just have a simple version matrix in the CI. |
Beta Was this translation helpful? Give feedback.
Hi,
yeah,
Socket|resource|false
is most precise if you need to support both PHP 7 and 8.Currently the best way to run PHPStan on such codebases is to run it twice, for both PHP 7 and PHP 8. You'll have to run it with different config files as some code is marked as invalid for PHP 7 (for example the Socket class doesn't exist there), and some code is not necessary on PHP 8, like checking for
=== false
for functions that no longer return false.Unfortunately that's currently the best way to do it, but not ideal. PHPStan currently supports only a single PHP version to consider for a codebase, not a range, as I explained here at length: #4060 (comment)
At least you can reuse the common part…