Skip to content
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

Replacing $this with $block in templates is not universally safe / correct #407

Open
fredden opened this issue Jun 28, 2022 · 5 comments · May be fixed by #413
Open

Replacing $this with $block in templates is not universally safe / correct #407

fredden opened this issue Jun 28, 2022 · 5 comments · May be fixed by #413
Assignees
Labels
bug Something isn't working Progress: PR created

Comments

@fredden
Copy link
Member

fredden commented Jun 28, 2022

Preconditions

  1. In a template (.phtml file), 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
  2. Magento Coding Standard version 25

Steps to reproduce

  1. Run phpcbf --standard=Magento2 on template

Expected result

  1. Template works the same as before auto-fixes are applied.

Actual result

  1. Exception is throw: Invalid method Vendor\Module\Block\SomeBlockClass\Interceptor::helper
@fredden fredden added the bug Something isn't working label Jun 28, 2022
@m2-assistant
Copy link

m2-assistant bot commented Jun 28, 2022

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


@Nuranto
Copy link

Nuranto commented Jun 29, 2022

@fredden have a look at #156

@fredden
Copy link
Member Author

fredden commented Jun 29, 2022

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

@Nuranto
Copy link

Nuranto commented Jun 29, 2022 via email

@fredden
Copy link
Member Author

fredden commented Jun 29, 2022

That's what we've had to do as well (remove/exclude this rule from the fixer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Progress: PR created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants