Replacing `$this` with `$block` in templates is not universally safe / correct
fredden opened this issue · 5 comments
Preconditions
- In a template (
.phtmlfile), make use of$this->helper(SomeClass:class);. For example, https://github.com/magento/magento2/blob/2.4.4/app/code/Magento/Catalog/view/frontend/templates/product/listing.phtml#L20 - Magento Coding Standard version 25
Steps to reproduce
- Run
phpcbf --standard=Magento2on template
Expected result
- Template works the same as before auto-fixes are applied.
Actual result
- Exception is throw:
Invalid method Vendor\Module\Block\SomeBlockClass\Interceptor::helper
Hi @fredden. Thank you for your report.
To speed up processing of this issue, make sure that you provided sufficient information.
Add a comment to assign the issue: @magento I am working on this
- Join Magento Community Engineering Slack and ask your questions in #github channel.
@Nuranto thanks for your feedback.
I have no problem with the warning / message that says that helpers are deprecated / should not be used. I have no problem with the warning / message that says that one should use $block->getThing() not $this->getThing() in templates. I have no problem with the auto-fix of $this->getThing() to $block->getThing().
This bug is for when $this->helper() gets replaced automatically with $block->helper(), which results in a broken template. (The automatic replacement of $this->getThing() with $block->getThing() is fine and the template works just fine with this change.)
That's what we've had to do as well (remove/exclude this rule from the fixer).