Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60c7d87

Browse filesBrowse files
committed
#121 HelperInTemplate Rule porposal amends
1 parent 9ea4f74 commit 60c7d87
Copy full SHA for 60c7d87

File tree

Expand file treeCollapse file tree

4 files changed

+26
-77
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+26
-77
lines changed

‎Magento2/Sniffs/Templates/HelperInTemplateSniff.md

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Templates/HelperInTemplateSniff.md
-25Lines changed: 0 additions & 25 deletions
This file was deleted.

‎Magento2/Sniffs/Templates/HelperInTemplateSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Templates/HelperInTemplateSniff.php
-48Lines changed: 0 additions & 48 deletions
This file was deleted.

‎Magento2/Sniffs/Templates/ThisInTemplateSniff.md

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Templates/ThisInTemplateSniff.md
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Replace `$this` with `$block`. If you use private or protected methods, make the
1414

1515
---
1616

17+
# Rule: Do not use `helpers` in templates
18+
## Background
19+
The use of helpers is in general discouraged. For template files, consider using a ViewModel instead.
20+
1721
## Reasoning
1822
The use of helpers is in general discouraged therefore any `$this->helper(<helper_class>)` code used in PHTML templates should be refactored.
1923

@@ -23,7 +27,7 @@ Consider using ViewModel instead.
2327

2428
Typical example of a helper being used in a PHTML:
2529
```html
26-
<?php $_incl = $block->helper(<helper_class>)->...; ?>
30+
<?php $_incl = $this->helper(<helper_class>)->...; ?>
2731
```
2832

2933
Once the ViewModel is created, call it in the PHTML as follow:

‎Magento2/Sniffs/Templates/ThisInTemplateSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Templates/ThisInTemplateSniff.php
+21-3Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento2\Sniffs\Templates;
78

89
use PHP_CodeSniffer\Sniffs\Sniff;
@@ -18,14 +19,28 @@ class ThisInTemplateSniff implements Sniff
1819
*
1920
* @var string
2021
*/
21-
protected $warningMessage = 'Usage of $this in template files is deprecated.';
22+
protected $warningMessageFoundHelper = 'Usage of helpers in templates is discouraged.';
23+
24+
/**
25+
* Warning violation code.
26+
*
27+
* @var string
28+
*/
29+
protected $warningCodeFoundHelper = 'FoundHelper';
30+
31+
/**
32+
* String representation of warning.
33+
*
34+
* @var string
35+
*/
36+
protected $warningMessageFoundThis = 'Usage of $this in template files is deprecated.';
2237

2338
/**
2439
* Warning violation code.
2540
*
2641
* @var string
2742
*/
28-
protected $warningCode = 'FoundThis';
43+
protected $warningCodeFoundThis = 'FoundThis';
2944

3045
/**
3146
* @inheritdoc
@@ -42,7 +57,10 @@ public function process(File $phpcsFile, $stackPtr)
4257
{
4358
$tokens = $phpcsFile->getTokens();
4459
if ($tokens[$stackPtr]['content'] === '$this') {
45-
$phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode);
60+
$phpcsFile->addWarning($this->warningMessageFoundThis, $stackPtr, $this->warningCodeFoundThis);
61+
}
62+
if ($tokens[$stackPtr]['content'] === 'helper(') {
63+
$phpcsFile->addWarning($this->warningMessageFoundHelper, $stackPtr, $this->warningCodeFoundHelper);
4664
}
4765
}
4866
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.