Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Make some classes final out of the box #1594

Closed
MladenJanjetovic opened this issue Apr 10, 2019 · 5 comments
Closed

[Proposal] Make some classes final out of the box #1594

MladenJanjetovic opened this issue Apr 10, 2019 · 5 comments

Comments

@MladenJanjetovic
Copy link

MladenJanjetovic commented Apr 10, 2019

It would be nice to follow good practice and make most of the classes final.
There are several facts that this can improve code quality and make development easier and safer.
Here are some nice reads:
https://ocramius.github.io/blog/when-to-declare-classes-final/
https://matthiasnoback.nl/2018/09/final-classes-by-default-why/

I guess that these classes can be made Final out of the box, and when making them with CLI:

  1. Controllers
  2. Tests
  3. Form Requests
  4. Mails
  5. Seeders
  6. Migrations
  7. Resources
  8. Middlewares
  9. Commands

Just for example, imagine when you want to extend your custom class in IDE and you don't get all of these above listed as autocomplete :)

These are just off the top of my head. I can create PRs for this.
@taylorotwell Let me know

Best,

@ludo237
Copy link

ludo237 commented Apr 10, 2019

Final is a pain in the ass then it comes to tests and mocking stuff

@MladenJanjetovic
Copy link
Author

MladenJanjetovic commented Apr 10, 2019

Final is a pain in the ass then it comes to tests and mocking stuff

@ludo237 I didn't experience that. Can you provide some example please? Tnx

@srdjanmarjanovic
Copy link

srdjanmarjanovic commented Apr 10, 2019

@MladenJanjetovic @ludo237 is right, mocking final classes is not possible with contemporary versions od PHPUnit. See sebastianbergmann/phpunit-mock-objects#388 (comment), albeit I can not think of a use case when you would need to mock a final class as I usually mock interfaces in general PHP projects. No experience of how this would influence Laravel testing workflow though.

@MladenJanjetovic
Copy link
Author

MladenJanjetovic commented Apr 10, 2019

@srdjanmarjanovic Agree. But when will you mock any of the classes I listed above in proposal?

@taylorotwell
Copy link
Member

taylorotwell commented Apr 10, 2019

If you want to make your own classes final, Laravel gives you the freedom to do that. I think final classes make sense in certain scenarios, especially when doing package development and you really want to limit what people can extend and modify, but I don't buy into the opinions of some PHP developers that every class should be final by default.

@laravel laravel locked as resolved and limited conversation to collaborators Apr 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants