-
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
Merged
lenaorobei
merged 8 commits into
magento:develop
from
diazwatson:121_HelperInTemplateRule
Aug 16, 2019
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ea4f74
#121 Magento2.Templates.HelperInTemplate Rule porposal
diazwatson 60c7d87
#121 HelperInTemplate Rule porposal amends
diazwatson a50eae5
Merge branch 'develop' into 121_HelperInTemplateRule
lenaorobei a6c8184
#121 HelperInTemplate Rule porposal amends
diazwatson 5ec4707
Merge branch 'develop' into 121_HelperInTemplateRule
diazwatson abdfcf5
#121 HelperInTemplate amends2
diazwatson 71235db
Merge branch 'develop' of github.com:magento/magento-coding-standard …
lenaorobei 5182dca
magento/magento-coding-standard#121: Magento2.Templates.HelperInTempl…
lenaorobei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Rule: Do not use `helpers` in templates | ||
## Background | ||
The use of helpers is in general discouraged. Consider using a ViewModel instead. | ||
|
||
## Reasoning | ||
The use of helpers is in general discouraged therefore any `$this->helper(<helper_class>)` code used in PHTML templates should be refactored. | ||
|
||
Consider using ViewModel instead. | ||
|
||
## How to fix | ||
|
||
Typical example of a helper being used in a PHTML: | ||
```html | ||
<?php $_incl = $block->helper(<helper_class>)->...; ?> | ||
``` | ||
|
||
Once the ViewModel is created, call it in the PHTML as follow: | ||
|
||
```html | ||
<?php $viewModel = $block->getViewModel(); ?> | ||
``` | ||
or | ||
```html | ||
<?php $viewModel = $block->getData('viewModel'); ?> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Sniffs\Templates; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
/** | ||
* Detects possible usage of helper in templates. | ||
*/ | ||
class HelperInTemplateSniff implements Sniff | ||
{ | ||
/** | ||
* String representation of warning. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningMessage = 'Usage of helpers in templates is discouraged.'; | ||
|
||
/** | ||
* Warning violation code. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningCode = 'FoundThis'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return [T_VARIABLE]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[$stackPtr]['content'] === 'helper(') { | ||
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.