-
Notifications
You must be signed in to change notification settings - Fork 10
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
Rule: No Object Manager in classes, except for factories, proxies, builders #8
Comments
What about the use of ObejectManager::getInstance and the new keyword ? |
The call |
Why for factories, proxies, builders? You mean just inside those classes object manager can be used? Or for creation factory we can use object manager? If second option - I'm totally disagree |
Yes, I meant the first. Whenever, you create a factory, proxy or builder class, there is an injection of the object manager in its constructor. For reference, see the source code of Magento its core factories and proxies (generated or not). The same applies to builders. So the design pattern more or less makes it legal to inject the object manager. For other classes, there is no such pattern. I'm not sure what you mean with creation factory. Do you have a code sample for this? |
I guess, "for creation factory" was meant as "to create a factory". @ihor-sviziev this was not the intention. Just that if you create a custom factory, proxy or builder, you are allowed to use the object manager in there. Technically the rule is: never use "ObjectManager" except in classes with one of these suffixes:
(did I miss any?) |
@schmengler i will work on this |
I guess ObjectManager usage should also be allowed in tests. |
But maybe it's a broader discussion if the ruleset should even run over tests at all (maybe it just falls back to PSR-2 for example). |
@fooman i will check the include ObjectManager must be allowed for unit tests |
Magento themselves care about coding standards in tests - at least for PHPCS/PHPMD - so this probably should too |
Maybe it'll be helpful https://github.com/magento/marketplace-eqp/blob/master/MEQP2/Sniffs/Classes/ObjectManagerSniff.php There are no exceptions in this sniff, but they can be easily added. |
@lenaorobei The EQP2 rule mentioned could be included as well, but simply checks whether a variable with the name To all, I have now created a PR for a rule that checks the constructor instead: #48 |
I have added a label for debate, because I have added some additional utilities ( |
As discussed in the hangout, please go forward with this approach. |
The Object Manager should not be injected directly through constructor DI into a class, except for when that classes appears to be a factory, proxy, builder or similar pattern.
The text was updated successfully, but these errors were encountered: