-
Notifications
You must be signed in to change notification settings - Fork 160
[New Rule] Do not use setTemplate in Block classes #31
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
Comments
Need to investigate and find Magento core use cases where this rule is not applicable. |
Hi @lenaorobei I investigated a bit, there might be 3 scenarios in core where the rule is not applicable.
All the other occurrences are rule violation and should be refactored. Let me know what you think. |
Hi @ldusan84 I can raise this question during weekly architecture discussion magento/architecture#153 |
Hi @lenaorobei Sorry for the late reply. Yeah let's do it if it's still possible. Thanks. |
@ldusan84, sure. I can be discussed during any upcoming meeting https://github.com/magento/architecture/issues?q=is%3Aissue+is%3Aopen+label%3A%22meeting+notes%22 |
MTS-2096: PHP Code Sniffer: additional functions to block
Background
When creating a Block class, a Block class could set its PHTML template in multiple ways: Through XML layout, through a call to
$this->setTemplate()
and through a variable $_template. The new design of Block classes is to configure them at constructor time, meaning that configuration options (like the template) are added using constructor arguments. This allows for the XML layout to change the template. The template in the Block class is then only defined as a default value, if the XML layout is not overriding the template: This default value is best defined via a protected variable$_template
.Reason
If
$this->setTemplate()
is used instead, this could lead to potential issues: First of all, setters are deprecated in Block classes (because constructor arguments should be preferred instead). Second, if$this->setTemplate()
is added to the constructor after calling upon the parent constructor, it would undo the configuration via XML layout. Simply put: It is outdated and leads to issues quickly.Implementation
ExtDn SetTemplateInBlockSniff.
The text was updated successfully, but these errors were encountered: