-
Notifications
You must be signed in to change notification settings - Fork 160
#121 Magento2.Templates.HelperInTemplate Rule porposal #122
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
#121 Magento2.Templates.HelperInTemplate Rule porposal #122
Conversation
@diazwatson, I got approval magento/architecture#222 that it is a valid recommendation and we can move forward with this rule. However, I have few comments:
$this->helper();
|
|
||
Typical example of a helper being used in a PHTML: | ||
```html | ||
<?php $_incl = $block->helper(<helper_class>)->...; ?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In most cases it is $this->helper(<helper_class>)
, because $this
refers to the template engine, not to the block.
Cool, I'll update this PR with those changes |
I checked the file with following content: <?php
echo $block->escapeHtml($block->getGroupCode());
echo $this->escapeHtml($this->getGroupCode());
$this->foo();
$this->helper(); Actual result:
Expected result:
|
Hi @lenaorobei could you share the steps or commands you are using for test? |
I just created <?php
echo $block->escapeHtml($block->getGroupCode());
echo $this->escapeHtml($this->getGroupCode());
$this->foo();
$this->helper(); Then I run command
|
Hi @lenaorobei I have updated this PR with the changes requested. Some thoughts about these changes:
besides, some other sniffers have several rules, Some examples are
The rule is called What do you think about renaming the rule to something more generic? Something like
|
@diazwatson thanks for sharing your suggestions, I really appreciate it. However, I did some sync up with internal teams regarding this rule and we agreed following:
|
Hi @lenaorobei could you explain how this should be then refactor to generate only one warning? |
It should be something like this: public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] === '$this') {
$helperPosition = $phpcsFile->findNext(T_STRING, $stackPtr, null, false, 'helper', true);
if ($helperPosition !== false) {
$phpcsFile->addWarning($this->warningMessageFoundHelper, $helperPosition, $this->warningCodeFoundHelper);
} else {
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
}
}
} |
@lenaorobei Thanks for the feedback, please have a look at the latest changes when you get a chance. |
return [T_VARIABLE]; | ||
return [ | ||
T_VARIABLE, | ||
T_STRING, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are looking for $this
, so we need to subscribe to T_VARIABLE
token only. T_STRING
is redundant here.
#121