Skip to content

Commit 01ff1bd

Browse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into AC-670_phpcs-layouttest
2 parents 4bb6fcb + b8d2c96 commit 01ff1bd

20 files changed

+6461
-575
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ jobs:
4242
- name: Install dependencies
4343
run: npm install
4444
- name: Run ESLint
45-
run: npm run eslint -- eslint/rules Magento2/ --ignore-pattern 'Magento2/Tests/Eslint/*Test.js'
45+
run: npm run eslint -- eslint/rules Magento2 --ignore-pattern 'Magento2/Tests/Eslint/*Test.js'
46+
- name: Run JSCS
47+
run: npm run jscs eslint/rules Magento2
4648

4749
- name: Validate composer
4850
run: composer validate

Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,14 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
229229
'^strval$' => '(string) construction',
230230
'^htmlspecialchars$' => '\Magento\Framework\Escaper->escapeHtml',
231231
'^getimagesize$' => 'getimagesizefromstring',
232+
'^file_exists$' => 'Magento\Framework\Filesystem\DriverInterface::isExists()',
233+
'^file_get_contents$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetContents()',
234+
'^file_put_contents$' => 'Magento\Framework\Filesystem\DriverInterface::filePutContents()',
235+
'^fgetcsv$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetCsv()',
236+
'^fputcsv$' => 'Magento\Framework\Filesystem\DriverInterface::filePutCsv()',
237+
'^ftell$' => 'Magento\Framework\Filesystem\DriverInterface::fileTell()',
238+
'^fseek$' => 'Magento\Framework\Filesystem\DriverInterface::fileSeek()',
239+
'^feof$' => 'Magento\Framework\Filesystem\DriverInterface::endOfFile()',
240+
'^flock$' => 'Magento\Framework\Filesystem\DriverInterface::fileLock()',
232241
];
233242
}
Lines changed: 148 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)