Open lbajsarowicz opened 6 years ago
Wowser! Yes, this definitely seems like a cool rule.
Not sure how feasible it would be to extend the check to use repositories where they exist, ie $quoteRepository->save($quote)
.
@fooman I think it's enough to document repositories as suggested alternative if they exist and provide the needed method. Resource models will then be listed as fallback.
Question: how likely are false positives? Can getResource()
refer to something else than the deprecated method from the abstract Magento model class?
@schmengler I think the narrower we can make the rule the better to avoid false positives (also easier to document). I believe this rule covers descendants of Magento\Framework\Model\AbstractModel
only and in that context getResource()
would always be a correct positive (unless you somehow overwrite parent behaviour but that would be odd as well).
There is a range of deprecation annotations here https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Model/AbstractModel.php so we might need multiple rules.
So it boils down to "If class extends Magento\Framework\Model\AbstractModel
, then $this->getResource()
, $this->_getResource()
and $this->getResourceCollection()
should not be used" :+1:
Most of the Mageto 2 tutorials and stackoverflow answers:
provide
getResource()->save($object)
as recommended way to persist data changes on model - from model itself. As use ofgetResource()
is deprecated, and ResourceModel should be initialized using Dependency Injection, we should introduce inspection for PHP CS to avoid usinggetResource()
in models and setup scripts.