I have a section of code as follows
$decodedCredentials = json_decode($credentials, false);
if (!$this->isValidCredentials($decodedCredentials)) {
return null;
}
$credential = new Credential();
$credential->username = $decodedCredentials->username;
$credential->password = $decodedCredentials->password;
$credential->line = $decodedCredentials->line;
Further down in the class I have the following
private function isValidCredentials(mixed $credentials): bool
{
if (!is_object($credentials)) {
return false;
}
return is_object($credentials) &&
property_exists($credentials, 'username') &&
property_exists($credentials, 'password') &&
property_exists($credentials, 'line');
}
The complaint from PHPStan
------ --------------------------------------------
Line Authentication.php
------ --------------------------------------------
:146 Cannot access property $username on mixed.
:147 Cannot access property $password on mixed.
:148 Cannot access property $line on mixed.
------ --------------------------------------------
Perhaps in the normal civilian part of my brain this code makes sense that it should pass PHPStan but it doesn't
We are calling isValidCredentials before trying to access those properties on the object and the check verifies all the things that PHPStan is complaining about.
I know that moving that check into the current function would solve the issue but that seems rather messy to me and I feel like this really should work and maybe I am missing something obvious. Bug perhaps?
mixedis too unspecific for phpstan. You should use Reflection instead.