vimeo / psalm

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

`@only-named-arguments` #10373

Open someniatko opened 10 months ago

someniatko commented 10 months ago

Psalm has @no-named-arguments annotation which forces calling functions or methods by specifying parameters in their order, and allow the function maintainers to change parameter names for refactoring purposes.

However, in some cases, like constructors of DTOs with lots of fields, it might be preferable to always use parameter names instead, and in such cases parameter order does not matter. However, for housekeeping purposes sometimes it might be preferable to change this order. If we knew that no user of this DTO coupled to the parameter order, we could freely change it.

Therefore I suggest implementing something like @only-named-arguments which forces specifying param names, and emphasizes that their order might change.

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

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

kkmuffme commented 10 months ago

Can you please create examples on psalm.dev with code that should pass/fail, because then those psalm.dev examples can be used as unit tests when implementing this feature

someniatko commented 10 months ago

@kkmuffme sure!

standalone function: https://psalm.dev/r/dd628f335c instance method: https://psalm.dev/r/92fef9c820 static method: https://psalm.dev/r/98e4a57a38 constructor: https://psalm.dev/r/3697fa812a

idk if this should also work for anon functions, because @no-named-arguments doesn't: https://psalm.dev/r/030309bc72

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

I found these snippets:

https://psalm.dev/r/dd628f335c ```php
https://psalm.dev/r/92fef9c820 ```php doSomethingComplex( actor: 'admin@example.com', description: 'calculate taxes', timestamp: 1700045844, ); // should fail $entity->doSomethingComplex( 1700045844, 'dev@example.com', 'remove technical debt', ); ``` ``` Psalm output (using commit 5095f4e): No issues! ```
https://psalm.dev/r/98e4a57a38 ```php
https://psalm.dev/r/3697fa812a ```php
https://psalm.dev/r/030309bc72 ```php $haystack > $needle; $canBeSubstr(haystack: 'uwu', needle: 'wu'); ``` ``` Psalm output (using commit 5095f4e): No issues! ```