Mark classes as final or abstract when possible#525
Mark classes as final or abstract when possible#525stloyd wants to merge 1 commit intomeilisearch:mainfrom
final or abstract when possible#525Conversation
|
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
finalorabstractwhen 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!