Shopsys: Using extended classes in project

Created on 27 Mar 2019  路  4Comments  路  Source: shopsys/shopsys

What is happening

Model situation:
In my project, I extend ArticleFacade, add there new public myNewFunction(), and configure my new service as an alias for the original one in services.yml, something like this:

Shopsys\FrameworkBundle\Model\Article\ArticleFacade: '@Shopsys\ShopBundle\Model\Article\ArticleFacade'

Then, I want to use ArticleFacade::myNewFunction() in Controller\Front\ArticleController that has a dependency on ArticleFacade from the core (ie. FrameworkBundle).

At the moment, I am able to use the new function thanks to the DI container configuration, however, the IDE is not aware of it as the dependency is still typehinted and annotated as the original ArticleFacade from the core, and therefore I am not able to click on the function myNewFunction and navigate to the proper class. This might be very confusing from a long-term maintainability point of view.

Possible solutions

  • solution 1 - I can just overwrite the property annotation:
/**
- * @var \Shopsys\FrameworkBundle\Model\Article\ArticleFacade
+ * @var \Shopsys\ShopBundle\Model\Article\ArticleFacade
 */
private $articleFacade;
  • solution 2 - I can change the dependency completely so the Controller is dependent on my own ArticleFacade (ie. the descendant implemented by me in ShopBundle) indeed:
- public function __construct(\Shopsys\FrameworkBundle\Model\Article\ArticleFacade $articleFacade)
+ public function __construct(\Shopsys\ShopBundle\Model\Article\ArticleFacade $articleFacade)
{
    $this->articleFacade = $articleFacade;
}

Personally, I prefer the solution 2. I am thinking about implementation of some fixer, that would automatically change all the dependencies in project-base in such a case, maybe it could add the entry to services.yml as well. Basically, if you extended a class, the fixer would ensure that it is used everywhere in your project instead of the parent class.

More on that, we should cover the situation when I extend some class that is dependent on a class that I have already extended:

  • ShopBundle\ArticleFacade extends FrameworkBundle\ArticleFacade
  • FrameworkBundle\Admin\ArticleController is dependent on FrameworkBundle\ArticleFacade - everything ok so far
  • ShopBundle\Admin\ArticleController extends FrameworkBundle\Admin\ArticleController - now I should probably ovewrite the dependency on FrameworkBundle\ArticleFacade and start using my ShopBundle\ArticleFacade

I would like to validate the fixer idea so I am asking here for your opinions or other suggestions.

Expected result

There is a consensus on the proper usage of extended classes in project.

Help wanted

Most helpful comment

Is it possible to provide more related articles, docs, etc. covering your mind map about planned "Service extension-ability"? I have found articles and docs only about entity related extensions, I am not sure if it is same

Provided examples are too simple from my point of view and therefore little bit misleading

for example I want to use some third party "module" (or package), which also extends ArticleFacade with method extendedModuleFunction() and I want to add my new function/extension called myDreamArticleFunction() on top of this extension and now I want to use in my controller both functions

  • is it even expected to support usage like this?
  • with this example, you can not use your preferred solution by specifying type in constructor (or can you?)
  • phpdoc hint with both class names does not seem as good idea either

if it is only about IDE support and autocomplete; I would suggest to have a look at
.phpstorm.meta.php to help IDE to understand how extension works in SSFW

All 4 comments

Is it possible to provide more related articles, docs, etc. covering your mind map about planned "Service extension-ability"? I have found articles and docs only about entity related extensions, I am not sure if it is same

Provided examples are too simple from my point of view and therefore little bit misleading

for example I want to use some third party "module" (or package), which also extends ArticleFacade with method extendedModuleFunction() and I want to add my new function/extension called myDreamArticleFunction() on top of this extension and now I want to use in my controller both functions

  • is it even expected to support usage like this?
  • with this example, you can not use your preferred solution by specifying type in constructor (or can you?)
  • phpdoc hint with both class names does not seem as good idea either

if it is only about IDE support and autocomplete; I would suggest to have a look at
.phpstorm.meta.php to help IDE to understand how extension works in SSFW

This issue has been automatically marked as stale because there was no activity within the last 4 months (and it is quite a long time). It will be closed if no further activity occurs. Thank you for your contributions.

up

Hi, @simara-svatopluk is already analyzing possible solutions for this :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grossmannmartin picture grossmannmartin  路  3Comments

ondrejbohac picture ondrejbohac  路  3Comments

TomasLudvik picture TomasLudvik  路  4Comments

TomasLudvik picture TomasLudvik  路  3Comments

simara-svatopluk picture simara-svatopluk  路  5Comments