-
Notifications
You must be signed in to change notification settings - Fork 26
Add deprecation rules for AbstractModel classes #36
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
Add deprecation rules for AbstractModel classes #36
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing tests for these rules is also really easy, you need to extend RuleTestCase
. It looks like this: https://github.com/phpstan/phpstan-src/blob/master/tests/PHPStan/Rules/Exceptions/CaughtExceptionExistenceRuleTest.php
src/bitExpert/PHPStan/Magento/Rules/AbstractModelRetrieveCollectionViaFactoryRule.php
Outdated
Show resolved
Hide resolved
src/bitExpert/PHPStan/Magento/Rules/AbstractModelUseServiceContractRule.php
Outdated
Show resolved
Hide resolved
@ondrejmirtes thanks for your valuable feedback and your work on phpstan, obviously :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a few more things :)
src/bitExpert/PHPStan/Magento/Rules/AbstractModelRetrieveCollectionViaFactoryRule.php
Outdated
Show resolved
Hide resolved
src/bitExpert/PHPStan/Magento/Rules/AbstractModelRetrieveCollectionViaFactoryRule.php
Outdated
Show resolved
Hide resolved
4ca967e
to
6e718a5
Compare
@hostep @sprankhub could you both also please test this against your modules? Would love to know how much breaks :) Thanks! |
These are the error I'm getting right now on https://github.com/baldwin-agency/magento2-module-url-data-integrity-checker/tree/d71e782f7e570ba30d2ab2b2f2c460967eb429bb:
I don't think there are service contracts available in Magento for these 2 things (and probably a bunch of others as well)? |
|
That seems to solve the reported issues indeed, thanks @ondrejmirtes! (I had to run I'm a bit worried about that previous |
@ondrejmirtes can you explain the difference to me? Is it because of |
@hostep thanks for your analysis. I'll check where the problem with |
@shochdoerfer You need to imagine types in a venn diagram. The largest circle (which contains all the types) is mixed - which is the top type. So mixed is super type of all the types. If one smaller circle is inside a bigger circle (like Same for objects. If two types are totally unrelated (like string and int), then What happened in your case: (new ObjectType('Magento\Framework\Model\AbstractModel'))->isSuperTypeOf($type) Abstract model might be a super type of mixed. And CacheInterface must be somehow related to AbstractModel, it's its parent or something like that... |
@ondrejmirtes thx, your insights are very much appreciated! |
Same here: Just tested this. The new issues I get through this seem to be caused by the However, if this PR raises errors for all usages of |
@sprankhub I get your point. Would it make sense to add a whitelist configuration of classes or namespaces that should be excluded in this check? Not sure about how many classes we are talking about, though. @hostep what do you think? I would really love to establish a few rules but I fear the current state of Magento that does not allow that without dozens of exceptions :( |
I fear a whitelist is hard to do, because it is also version-specific... But yeah, if it is easy to whitelist specific classes / namespaces in the |
A configurable whitelist in the But then I realised we can already add exceptions by default with phpstan using something like:
So it's not really necessary since it's boiling down to the same thing I guess? |
That's not a good idea because if someone doesn't have that code in their app, it would get marked as unused ignore... It's a better idea to add a whitelist parameter to the rule itself. |
@sprankhub the problem with the Magento version is already a problem since the stubs/mock I added to fix a few "doc block" issues are not compatible anymore with older Magento versions (2.0 and 2.1 I think). Since we now would rely on rules that have been introduced by a specific Magento version, I have to add the minimum @hostep I'll check the logic once again. The cache interface clearly does not extend AbstractModel, thus that error should not come up. I feel there's still a logic problem somewhere that lists way too many errors. If that is solved we might not need a whitelist by default. |
Access to save(), load(), delete() and getCollection() is deprecated.
Since the rules don't have any autowirable arguments, they can be moved to the rules section.
This avoids problems when running phpstan within phpunit and Magento classes not being loadable.
6e718a5
to
6dd02cc
Compare
This way we can make sure the respective version of the bitexpert/phpstan-magento package can only be installed for Magento versions matching the minimum requirements.
Updated the rules logic with all the hints provided by Ondřej. Ran tests against a log of open source Magento modules and the only problem I was running into was the problem mentioned by @hostep in regards of To avoid some "noise" in some circumstances, I added 2 config options to be able to disable both rules independently. This plus the possibility to ignore errors via PHPStan should be "enough" to configure the extension to behave like you want. We still could add a whitelist later if it makes sense. |
Thanks for all your help with this and your insights. Would love to add more rules in the future. Feel free to open new issues if you have ideas. |
parameters: | ||
magento: | ||
checkCollectionViaFactory: true | ||
checkServiceContracts: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shochdoerfer Instead of adding options to rules themselves to turn them off by not reporting anything, instead you can use the conditionalTags
section like here: https://github.com/Roave/no-floaters/blob/master/rules.neon#L10-L12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nice, even better. Thanks!
Access to save(), load(), delete() and getCollection() is deprecated.