Skip to content

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

Merged

Conversation

shochdoerfer
Copy link
Member

Access to save(), load(), delete() and getCollection() is deprecated.

Copy link

@ondrejmirtes ondrejmirtes left a 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

@shochdoerfer
Copy link
Member Author

@ondrejmirtes thanks for your valuable feedback and your work on phpstan, obviously :)

Copy link

@ondrejmirtes ondrejmirtes left a 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 :)

@shochdoerfer shochdoerfer force-pushed the feature/abstract_model_rules branch from 4ca967e to 6e718a5 Compare May 23, 2020 19:25
@shochdoerfer
Copy link
Member Author

@hostep @sprankhub could you both also please test this against your modules? Would love to know how much breaks :) Thanks!

@hostep
Copy link
Collaborator

hostep commented May 24, 2020

Hi @shochdoerfer

These are the error I'm getting right now on https://github.com/baldwin-agency/magento2-module-url-data-integrity-checker/tree/d71e782f7e570ba30d2ab2b2f2c460967eb429bb:

 ------ -----------------------------------------------------------------------------
  Line   Cron/ScheduleJob.php
 ------ -----------------------------------------------------------------------------
  42     Use service contracts to persist entities in favour of mixed::save() method
 ------ -----------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------
  Line   Storage/CacheStorage.php
 ------ ------------------------------------------------------------------------------------------------------------
  29     Use service contracts to persist entities in favour of Magento\Framework\App\CacheInterface::save() method
  34     Use service contracts to persist entities in favour of Magento\Framework\App\CacheInterface::load() method
 ------ ------------------------------------------------------------------------------------------------------------

I don't think there are service contracts available in Magento for these 2 things (and probably a bunch of others as well)?

@ondrejmirtes
Copy link

if ($isAbstractModelType->no()) { should be changed to if (!$isAbstractModelType->yes()) {

@hostep
Copy link
Collaborator

hostep commented May 24, 2020

That seems to solve the reported issues indeed, thanks @ondrejmirtes! (I had to run vendor/bin/phpstan clear-result-cache however before the change was picked up, which is slightly annoying.)

I'm a bit worried about that previous mixed::save() report however, in reality it's the Magento\Cron\Model\Schedule::save() and Magento\Cron\Model\Schedule does inherit from Magento\Framework\Model\AbstractModel, so in this case it should still report the error if I understand it correctly.
So it looks like the exact type isn't getting inferred correctly here. Code: https://github.com/baldwin-agency/magento2-module-url-data-integrity-checker/blob/d71e782f7e570ba30d2ab2b2f2c460967eb429bb/Cron/ScheduleJob.php#L40-L47

@shochdoerfer
Copy link
Member Author

@ondrejmirtes can you explain the difference to me? Is it because of maybe() ?

@shochdoerfer
Copy link
Member Author

@hostep thanks for your analysis. I'll check where the problem with mixed::save() is coming from.

@ondrejmirtes
Copy link

@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 mixed <> int relationship), then mixed is super type of int (yes()), but if you ask for an inverse relationship, it's maybe().

Same for objects. Exception is super type of all the Exception implementations (yes()). But InvalidArgumentException is maybe() a super type of Exception.

If two types are totally unrelated (like string and int), then isSuperType returns no().

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...

@shochdoerfer
Copy link
Member Author

@ondrejmirtes thx, your insights are very much appreciated!

@sprankhub
Copy link
Contributor

Same here: Just tested this. The new issues I get through this seem to be caused by the phpstan/phpstan update from 0.12.19 to 0.12.25. Hence, all good from my side. Thanks for your work!

However, if this PR raises errors for all usages of save, load etc., I have strong objections against that. Because as @hostep already pointed out, there are many entities, which are not covered with proper APIs yet (e.g. product custom options).

@shochdoerfer
Copy link
Member Author

@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 :(

@sprankhub
Copy link
Contributor

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 phpstan.neon, I would be fine with it.

@hostep
Copy link
Collaborator

hostep commented May 28, 2020

A configurable whitelist in the phpstan.neon file sounded like a good idea to me at first.

But then I realised we can already add exceptions by default with phpstan using something like:

parameters:
    ignoreErrors:
        - '/Use service contracts to persist entities in favour of Magento\\Framework\\App\\CacheInterface\:\:(?:save|load)\(\) method/'

So it's not really necessary since it's boiling down to the same thing I guess?

@ondrejmirtes
Copy link

But then I realised we can already add exceptions by default

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.

@shochdoerfer
Copy link
Member Author

@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 magento/framework version as a required dependency, I guess. I would have loved to avoid that to reduce maintenance, but I guess there's no way around it.

@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.

@shochdoerfer shochdoerfer added this to the 0.4.0 milestone May 29, 2020
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.
@shochdoerfer shochdoerfer force-pushed the feature/abstract_model_rules branch from 6e718a5 to 6dd02cc Compare May 30, 2020 15:12
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.
@shochdoerfer
Copy link
Member Author

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 Magento\Cron\Model\Schedule::save(). Technically, there's a resource model for the Schedule class which could be used to save the objects. So, I'd guess the error is fine.

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.

@shochdoerfer
Copy link
Member Author

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.

@shochdoerfer shochdoerfer merged commit f68cf8b into bitExpert:master May 31, 2020
parameters:
magento:
checkCollectionViaFactory: true
checkServiceContracts: true

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

Copy link
Member Author

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!

@shochdoerfer shochdoerfer deleted the feature/abstract_model_rules branch August 7, 2021 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants