Skip to content

Commit 43b69ef

Browse files
Merge pull request #93 from magento-commerce/imported-eliseacornejo-magento-coding-standard-306
[Imported] AC-681: Create phpcs static check for PhtmlTemplateTest
2 parents aa0c657 + 70a747c commit 43b69ef

File tree

6 files changed

+355
-0
lines changed

6 files changed

+355
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class PhtmlTemplateSniff implements Sniff
14+
{
15+
private const WARNING_CODE = 'PhtmlTemplateObsolete';
16+
17+
private const OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES = [
18+
'/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' .
19+
'ui widget instead.',
20+
'/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' .
21+
'it in template.',
22+
'@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' .
23+
'in php. Do it in template.',
24+
];
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function register(): array
30+
{
31+
return [
32+
T_OBJECT_OPERATOR,
33+
T_INLINE_HTML,
34+
T_HEREDOC
35+
];
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) {
45+
$this->checkBlockVariable($phpcsFile, $stackPtr, $tokens);
46+
$this->checkThisVariable($phpcsFile, $stackPtr, $tokens);
47+
}
48+
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML || $tokens[$stackPtr]['code'] === T_HEREDOC) {
49+
$this->checkHtml($phpcsFile, $stackPtr);
50+
51+
$file = $phpcsFile->getFilename();
52+
53+
if (strpos($file, '/view/frontend/templates/') !== false
54+
|| strpos($file, '/view/base/templates/') !== false
55+
) {
56+
$this->checkHtmlSpecificFiles($phpcsFile, $stackPtr);
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Check access to protected and private members of Block
63+
*
64+
* @param File $phpcsFile
65+
* @param int $stackPtr
66+
* @param array $tokens
67+
*/
68+
private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $tokens): void
69+
{
70+
$varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1);
71+
if ($tokens[$varPos]['content'] !== '$block') {
72+
return;
73+
}
74+
$stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
75+
if (strpos($tokens[$stringPos]['content'], '_') === 0) {
76+
$phpcsFile->addWarning(
77+
'Access to protected and private members of Block class is ' .
78+
'obsolete in phtml templates. Use only public members.',
79+
$stringPos,
80+
self::WARNING_CODE
81+
);
82+
}
83+
}
84+
85+
/**
86+
* Check access to members and methods of Block class through $this
87+
*
88+
* @param File $phpcsFile
89+
* @param int $stackPtr
90+
* @param array $tokens
91+
*/
92+
private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens): void
93+
{
94+
$varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1);
95+
if ($tokens[$varPos]['content'] !== '$this') {
96+
return;
97+
}
98+
$stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
99+
if (strpos($tokens[$stringPos]['content'], 'helper') === false) {
100+
$phpcsFile->addWarning(
101+
'Access to members and methods of Block class through $this is ' .
102+
'obsolete in phtml templates. Use only $block instead of $this.',
103+
$stringPos,
104+
self::WARNING_CODE
105+
);
106+
}
107+
}
108+
109+
/**
110+
* Check use of "text/javascript" type
111+
*
112+
* @param File $phpcsFile
113+
* @param int $stackPtr
114+
*/
115+
private function checkHtml(File $phpcsFile, int $stackPtr): void
116+
{
117+
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
118+
119+
if (preg_match('/type="text\/javascript"/', $content)) {
120+
$phpcsFile->addWarning(
121+
'Please do not use "text/javascript" type attribute.',
122+
$stackPtr,
123+
self::WARNING_CODE
124+
);
125+
}
126+
}
127+
128+
/**
129+
* Check of some obsoletes uses in specific files
130+
*
131+
* @param File $phpcsFile
132+
* @param int $stackPtr
133+
*/
134+
private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void
135+
{
136+
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
137+
138+
foreach (self::OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES as $obsoleteRegex => $errorMessage) {
139+
if (preg_match($obsoleteRegex, $content)) {
140+
$phpcsFile->addWarning(
141+
$errorMessage,
142+
$stackPtr,
143+
self::WARNING_CODE
144+
);
145+
}
146+
}
147+
}
148+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use DirectoryIterator;
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
use RecursiveDirectoryIterator;
11+
use RecursiveIteratorIterator;
12+
13+
class PhtmlTemplateUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
protected function getTestFiles($testFileBase): array
19+
{
20+
$testFiles = [];
21+
22+
$dir = __DIR__.'/_files/PhtmlTemplateUnitTest';
23+
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
24+
25+
/**
26+
* @var DirectoryIterator $file
27+
*/
28+
foreach ($di as $file) {
29+
if ($file->isDir()) {
30+
continue;
31+
}
32+
$path = $file->getPathname();
33+
if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') {
34+
$testFiles[] = $path;
35+
}
36+
}
37+
38+
// Put them in order.
39+
sort($testFiles);
40+
41+
return $testFiles;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function getErrorList()
48+
{
49+
return [];
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getWarningList($testFile = '')
56+
{
57+
if ($testFile === 'PhtmlTemplateUnitTest.1.phtml' || $testFile === 'PhtmlTemplateUnitTest.2.phtml') {
58+
return [
59+
7 => 1,
60+
9 => 1,
61+
13 => 1,
62+
20 => 1,
63+
22 => 1,
64+
23 => 1,
65+
27 => 1,
66+
33 => 1,
67+
39 => 1
68+
];
69+
}
70+
if ($testFile === 'PhtmlTemplateUnitTest.3.phtml')
71+
{
72+
return [
73+
9 => 1,
74+
20 => 1,
75+
22 => 1,
76+
23 => 1,
77+
27 => 1,
78+
];
79+
}
80+
return [];
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script type="text/javascript">
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script type="text/javascript">
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script type="text/javascript">
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>

Magento2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@
308308
<severity>8</severity>
309309
<type>warning</type>
310310
</rule>
311+
<rule ref="Magento2.Legacy.PhtmlTemplate">
312+
<include-pattern>*\.phtml$</include-pattern>
313+
<severity>8</severity>
314+
<type>warning</type>
315+
</rule>
311316

312317
<!-- Severity 7 warnings: General code issues. -->
313318
<rule ref="Generic.Arrays.DisallowLongArraySyntax">

0 commit comments

Comments
 (0)