vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 660 forks source link

How to suppress FalsableReturnStatement issue for specific method? #10282

Open kernusr opened 11 months ago

kernusr commented 11 months ago

In xsd config for FalsableReturnStatement issue set type is IssueHandlerType https://github.com/vimeo/psalm/blob/f1fc9c4887e4d362b0455ef2ad56670550a492f2/config.xsd#L235C62-L235C78

IssueHandlerType only allows suppression for folder and file. https://github.com/vimeo/psalm/blob/f1fc9c4887e4d362b0455ef2ad56670550a492f2/config.xsd#L506C1-L519C23

How to suppress this issue for just one method in a class or one function in a file?

psalm-github-bot[bot] commented 11 months ago

Hey @kernusr, can you reproduce the issue on https://psalm.dev ?

robchett commented 11 months ago

Can you use a line annotation /* @psalm-suppress FalsableReturnStatement / ?

kernusr commented 11 months ago

Abstract example code I'm using: https://psalm.dev/r/6cd6dc751a

ModelInterface, ItemModel, Table is parts of Joomla CMS

I can't get rid of return false, because this is a rule of the framework. All models must return item data object on success, false on failure.

psalm-github-bot[bot] commented 11 months ago

I found these snippets:

https://psalm.dev/r/6cd6dc751a ```php load($pk); } } ``` ``` Psalm output (using commit 9b00ac0): ERROR: FalsableReturnStatement - 33:16 - The declared return type 'object' for MyItemModel::getItem does not allow false, but the function returns 'false|object' ERROR: InvalidFalsableReturnType - 30:21 - The declared return type 'object' for MyItemModel::getItem does not allow false, but 'false|object' contains false ```
kernusr commented 11 months ago

Can you use a line annotation /* @psalm-suppress FalsableReturnStatement / ?

I could use this solution But similar code occurs 98 times in the project and I wanted to suppress the issue by config via XML file

kernusr commented 11 months ago

Of course, it would be more correct to make changes in all places where a similar problem occurs, but now it is too expensive for us

I chose a different path First I want to suppress all not too critical issues and gradually solve them in the future

weirdan commented 7 months ago

So what you're saying is that vendor-supplied ModelInterface has an incorrect docblock? Then you should be able to override it with a stub.