Skip to content

Conversation

VincentLanglet
Copy link
Contributor

Closes phpstan/phpstan#13297

Currently, even doing a simple Enum::from(1) where 1 is a value valid, reports missing @throws phpdoc.

@VincentLanglet VincentLanglet force-pushed the fix/13297 branch 2 times, most recently from 8746ed7 to 105dc08 Compare July 25, 2025 07:34
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't want people to discourage from defensive code. I don't want to report an error when someone catches an exception because the argument type is only guarded with a PHPDoc for example.

return $methodReflection->getThrowType();
}

$valueType = $scope->getType($arguments[0]->value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNativeType please

@VincentLanglet
Copy link
Contributor Author

I really don't want people to discourage from defensive code. I don't want to report an error when someone catches an exception because the argument type is only guarded with a PHPDoc for example.

If I use getNative type, it will report a lot of false-positive when using ::from, as exemple this method https://github.com/phpstan/phpstan-src/pull/4156/files#diff-45aec6a97db9e1a9901c9edd9dbe1859275e5e55b5cfd4f373f051dfe040110eR12 will require to add @throws ValueError while I don't want to because I DO trust my phpdoc, since I'm the only user of my methods.

This is the same than DynamicReturnTypeExtension which remove false from the return type of a method based on the Type (and not the nativeType) which means that an extra-safety check === false will be reported as never true.

getNativeType please

Shouldn't it be at least conditioned by treatphpdoctypesascertain ? Other way it won't remove lot of useless @throws...

Or maybe DynamicMethodThrowTypeExtension should be thought differently...
I understand that you want to allow

try {
      Enum::from($foo);
} catch (...) {}

but what I try to avoid is the error

Method Foo::bar() throws checked exception

Can't the DynamicMethodThrowTypeExtension return a "BenevolentThrowType" then ? Something like an unchecked exception or an implicit throw point ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum::from(value-of<Enum>) should not throw an error
2 participants