DaveLiddament / php-language-extensions

Attributes to define new PHP language features (that are enforced by static analysis)
MIT License
135 stars 5 forks source link

RestrictTraitTo vs require-extends and require-implements #37

Closed staabm closed 2 months ago

staabm commented 2 months ago

I wonder what the difference of RestrictTraitTo Is compared to require-extends and require-implements ?

DaveLiddament commented 2 months ago

Thanks for raising this issue @staabm. The honest, and somewhat embarrassing, answer is I didn't realise that PHPStan already has this functionality!

I was talking about the PHP language extension library at a conference and someone asked for a RestrictTraitTo feature. I remember saying that Psalm has something similar and, at the time, PHPStan didn't have this feature. Before implementing RestrictTraitTo I failed to check that PHPStan now has that feature.

That said I will continue to add to this library, the corresponding PHPStan extension and, hopefully one day, a Psalm plugin. Some of the features I will add will be functionality that already exists in PHPStan and Psalm.

My vision is that we should move from annotations to attributes for most cases (there are exceptions outlined below). However, I completely understand and see a lot of sense as to why annotations are still the default for tools like PHPStan.

In my ideal world...

  1. Docblocks are purely for humans (so annotations would not be used). Attributes are for humans, static analysis and limited run time inspection.

  2. The attribute definitions should live in a library that contains just the definitions (similar to this library) and the SA tools provide their own plugins separate libraries to implement validation.

  3. The attribute definitions would be managed in a similar way, at least in principle, to PSRs.

The current state of the PHP language means there are a things that are still best done as annotations, hopefully this will change:

I hope that all makes sense, even if you disagree with some/all of what I've written.

staabm commented 2 months ago

Thanks for taking the time to answer. Thats totally fine, I was just asking myself what might be different :-).

Btw I noticed psalm development seem to kind of did not move noticeably in the last months. It might mean vimeo reduced resources on the project - but thats just my personal perception/guesswork.