phpstan-nette icon indicating copy to clipboard operation
phpstan-nette copied to clipboard

Nette 3.2 Unreachable statement after adding types to __get method in Nette\Database\Row

Open Sahtii opened this issue 1 year ago • 7 comments

Unreachable statement - code above always terminates. PHPStan wrongly assumes that this code in Nette\Database\Row always throws Exception and assumes that the further code will not execute. This was not a case in Nette 3.1, problem occured after updating to Nette 3.2

public function __get(mixed $key): never
 {
 $hint = Nette\Utils\Helpers::getSuggestion(array_map('strval', array_keys((array) $this)), $key);
 throw new Nette\MemberAccessException("Cannot read an undeclared column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'));
 }

This happens for example for code

code
$row->variable 
code

Sahtii avatar May 23 '24 11:05 Sahtii

Looks like it works as designed: the __get always throws an exception (and returns never), so:

code 1
$row->variable 
code 2

"code 2" in this example will not be executed.

What here I do not understand?

ondrejmirtes avatar May 23 '24 14:05 ondrejmirtes

Anyway, feel free to create a small reproducing repository, so I can better understand the problem. Thanks.

ondrejmirtes avatar May 23 '24 14:05 ondrejmirtes

Problem is that $row can have property variable (it is universal objevt crate) but PHPStan thinks it always terminates.

MartinMystikJonas avatar May 24 '24 06:05 MartinMystikJonas

@MartinMystikJonas Exactly, $row->variable works without any issues and doesn't throw any errors (in our code), because the column variable is selected from the database.

kropacekt avatar May 24 '24 10:05 kropacekt

I understand now. The class doesn't have __set so properties are assigned as "dynamic" properties. The __get is only executed for properties that do not exist.

I say this class needs to be removed from universalObjectCratesClasses (https://github.com/phpstan/phpstan-nette/blob/8af94743efcc6d1e685f2ffd7ab279e39c96429c/extension.neon#L31) and needs its own PropertiesClassReflectionExtension.

ondrejmirtes avatar May 24 '24 12:05 ondrejmirtes

And is this work for you or for someone else?

kropacekt avatar May 27 '24 05:05 kropacekt

Anyone who wants it fixed ;) (I'll probably not get to it anytime soon.)

ondrejmirtes avatar May 27 '24 05:05 ondrejmirtes

Hey @Sahtii, can you check if it still happens for you with PHPStan 1.12? I had the same issue but after updating to 1.12 the problem seems gone.

spaze avatar Aug 30 '24 01:08 spaze

Hi @spaze , unfortunately it still happens for my code. What version of phpstan-nette do you use ? If there is some magic with interaction of versions of these two libraries.

Sahtii avatar Sep 06 '24 14:09 Sahtii

Hey @Sahtii, it stopped when I'm updated to PHPStan 1.12.0 (latest is 1.12.2) and phpstan-nette extension 1.3.8 (the current latest). I also use the bleeding edge configuration. See this commit in my repo where I had to unignore the error https://github.com/spaze/michalspacek.cz/pull/389/commits/2ff7216a14afd620333575d8674bc997e9685e4e

Update: also tested with the latest 1.12.2, with or without bleeding edge, still not getting this error.

spaze avatar Sep 06 '24 15:09 spaze

@spaze I have tried it with both versions for PHPStan (1.12.0 and 1.12.2) and latest version of phpstan-nette extension 1.3.8 but unfortunately it still have not work for me. What is your Nette version ? Mine is 3.2.5

Sahtii avatar Sep 16 '24 07:09 Sahtii

I'm using Nette "by packages", trying to keep up to date. My nette/database is 3.2.4, the latest stable release, see https://github.com/spaze/michalspacek.cz/blob/2866af7286a337726da7582ab08682bf5a16f20c/site/vendor/composer/installed.php#L141

spaze avatar Sep 16 '24 12:09 spaze

Yes, update to 3.2.4 helped.

So for anyone searching for this: You just need nette/database: 3.2.4

Sahtii avatar Oct 25 '24 14:10 Sahtii

For completeness, seems that this change in nette/database 3.2.4 (or earlier 3.2.3 which was removed later and replaced with 3.2.4) https://github.com/nette/database/issues/303 is what fixed this issue.

spaze avatar Oct 25 '24 21:10 spaze

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Nov 26 '24 00:11 github-actions[bot]