Lets imagine some vendor code that we want to extend on our project level
protected function getDefaultFormClasses() {
return [
new FormClassA(),
new FormClassB(),
new FormClassC(),
...
new FormClassZ()
]
}
On project level we want exactly what this function will return as our form classes collection, except of one, it should not be in our default form collection.
Lets say FormClassK() should be absent, and we want to add another one, lets say FormClassNew()
There are two possibilities known to me to solve this:
Override the parent class, list everything again except the one you don't want and also add the new one
Call the parent method and remove the one you don't want and add the new one
Regarding the first: Seems a lot of code duplication, so ... don't do it? -> DRY?
Regarding the second: Thinking of further code extensions (for example based on my project implementation) it would be really confusing, when every child does adding and subtracting to their parents collection to get the final result ... Also: Somebody who reads the code might have a hard time to aggregate the final collection in his mind, at least if he do not debug it.
This is just a simple example but i encountered this type of extension really often and also get very different philosophies here ...
But I am interested in facts not in preferences.
So what are the pros/cons using one method over the other? Are there even principles that gets violated here?
Is there a thumb rule or even a best practise when a complete rewrite of the parents method makes more sense than extending and "bend" it to the actual requirements?
Other suggestions are also welcome.