-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mark classes as final
or abstract
when possible
#525
Conversation
this is a library package, also a meta package since a lot of packages extend from it. making classes final brings a lot of pain for the downstream libraries and even for us to do testing. what are the advantages of making classes final in our case? |
please give some examples where this brings pain to you |
Just to elaborate my point
final class DocumentQuery {
}
final class HandlesDocumentQuery {
public function post (DocumentQuery $documentQuery) {
}
}
final class DocumentResult {
}
class TestFinalClass {
public function testICanHandleDocumentQuery () {
$documentQueryMock = $this->createMock(DocumentQuery::class);
$documentResultMock = $this->createMock(DocumentResult::class)
$this->createMock(HandlesDocumentQuery::class)
->expects(static::once())
->method('post')
->with($documentQueryMock)
->willReturn($documentResultMock);
// None of the above test works because you can't mock any of it, you will have to implement interfaces of each class
}
}
|
Also if you really need to mock something, I'd suggest to mock http client responses, not the meilisearch library parts |
Long story short: don't mock what you don't own.
|
I will try to justify a little. Does not want to take it as a final/non-final argument chain. Final classes remove the flexibility of extending and modifying the behavior of classes. This can be limiting for developers who have legitimate use cases where they need to extend or customize certain classes. (Which I have explained as a sample in my earlier reply). Why should I mock a class? its should be upto me, maybe i don't want to initialize all the parameters, maybe I like the flexibility of Assuming that the library's way of coding is the correct or desired way may not align with developers' preferences or needs. Allowing more flexibility in class extension empowers developers to make their own decisions and find the best solutions for their use cases. we may require the ability to mock final classes. This could be due to limitations or dependencies in their testing frameworks or specific testing needs. By marking classes as final, the library imposes restrictions that may conflict with these requirements and make it harder for developers to test their code effectively. When a library is less flexible and less accommodating to various use cases, it may limit its adoption within the community. Allowing for more flexibility can attract a wider range of developers and increase the library's overall usage and contributions. In the end keeping a balance between control and flexibility is important. I don't think allowing code extension doesn't mean losing control over the SDK; rather, it enables developers to extend and modify depending on their use case. Also interesting to see what are the benefits of making these classes final! |
library is not responsible that you want to mock everything even VO's that doesn't make sense, but that's why I suggested to add just phpdoc with final annotations instead of making them really final, so we could see what could be reverted later. and I suggest for you to write an integration test instead for the search because with mocks you don't ensure that it really works ❗ |
Pull Request
Related issue
Fixes n/a
What does this PR do?
Mark classes as
final
orabstract
when possible, as part of the road for hard BC break ( #518) we can do less hard one with phpdoc declarations.PR checklist
Please check if your PR fulfills the following requirements:
Thank you so much for contributing to Meilisearch!