Skip to content

Commit 1e62380

Browse files
authored
Merge pull request #160 from magento-commerce/develop
Develop to Master version 18
2 parents d7d69e4 + 9091848 commit 1e62380

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+334
-2886
lines changed

.github/workflows/php.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ jobs:
2020
dependencies:
2121
- "lowest"
2222
- "highest"
23-
experimental:
24-
- false
23+
exclude:
24+
- php-version: "8.1"
25+
dependencies: "lowest"
2526
name: Tests with PHP ${{ matrix.php-version }} and ${{ matrix.dependencies }} dependencies
2627

2728
steps:
@@ -36,6 +37,7 @@ jobs:
3637
uses: shivammathur/setup-php@v2
3738
with:
3839
php-version: ${{ matrix.php-version }}
40+
ini-file: development
3941
env:
4042
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Html;
9+
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Sniff for void closing tags.
15+
*/
16+
class HtmlClosingVoidTagsSniff implements Sniff
17+
{
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
private const WARNING_MESSAGE =
24+
'Avoid using closing slash with void tags, this can lead to unexpected behavior - "%s"';
25+
26+
/**
27+
* Warning violation code.
28+
*
29+
* @var string
30+
*/
31+
private const WARNING_CODE = 'HtmlClosingVoidElements';
32+
33+
/**
34+
* List of void elements.
35+
*
36+
* https://html.spec.whatwg.org/multipage/syntax.html#void-elements
37+
*
38+
* @var string[]
39+
*/
40+
private const HTML_VOID_ELEMENTS = [
41+
'area',
42+
'base',
43+
'br',
44+
'col',
45+
'embed',
46+
'hr',
47+
'input',
48+
'keygen',
49+
'link',
50+
'menuitem',
51+
'meta',
52+
'param',
53+
'source',
54+
'track',
55+
'wbr'
56+
];
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function register(): array
62+
{
63+
return [T_INLINE_HTML];
64+
}
65+
66+
/**
67+
* Detect use of self-closing tag with void html element.
68+
*
69+
* @param File $phpcsFile
70+
* @param int $stackPtr
71+
* @return void
72+
*/
73+
public function process(File $phpcsFile, $stackPtr): void
74+
{
75+
if ($stackPtr !== 0) {
76+
return;
77+
}
78+
$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
79+
80+
if (empty($html)) {
81+
return;
82+
}
83+
84+
if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
85+
foreach ($matches as $match) {
86+
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
87+
$phpcsFile->addWarning(
88+
sprintf(self::WARNING_MESSAGE, $match[0]),
89+
null,
90+
self::WARNING_CODE
91+
);
92+
}
93+
}
94+
}
95+
}
96+
}

Magento2/Sniffs/Legacy/LayoutSniff.php

+1-32
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class LayoutSniff implements Sniff
1919
{
2020
private const ERROR_CODE_XML = 'WrongXML';
21-
private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed';
2221
private const ERROR_CODE_OBSOLETE_BLOCK = 'ObsoleteBlock';
2322
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass';
2423
private const ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE = 'ObsoleteToHtmlAttribute';
@@ -230,7 +229,6 @@ public function process(File $phpcsFile, $stackPtr)
230229
}
231230

232231
$this->testObsoleteReferences($layout, $phpcsFile);
233-
$this->testHeadBlocks($layout, $phpcsFile);
234232
$this->testOutputAttribute($layout, $phpcsFile);
235233
$this->testHelperAttribute($layout, $phpcsFile);
236234
$this->testListText($layout, $phpcsFile);
@@ -276,35 +274,6 @@ private function getFormattedXML(File $phpcsFile)
276274
return $doc->saveXML();
277275
}
278276

279-
/**
280-
* Check that CSS, Link and Script blocks are inside a head block
281-
*
282-
* @param SimpleXMLElement $layout
283-
* @param File $phpcsFile
284-
*/
285-
private function testHeadBlocks(SimpleXMLElement $layout, File $phpcsFile): void
286-
{
287-
$selectorHeadBlock = '(name()="block" or name()="referenceBlock") and ' .
288-
'(@name="head" or @name="convert_root_head" or @name="vde_head")';
289-
$elements = $layout->xpath(
290-
'//block[@class="Magento\Theme\Block\Html\Head\Css" ' .
291-
'or @class="Magento\Theme\Block\Html\Head\Link" ' .
292-
'or @class="Magento\Theme\Block\Html\Head\Script"]' .
293-
'/parent::*[not(' .
294-
$selectorHeadBlock .
295-
')]'
296-
);
297-
if (!empty($elements)) {
298-
$phpcsFile->addError(
299-
'Blocks \Magento\Theme\Block\Html\Head\{Css,Link,Script} ' .
300-
'are allowed within the "head" block only. ' .
301-
'Verify integrity of the nodes nesting.',
302-
dom_import_simplexml($elements[0])->getLineNo(),
303-
self::ERROR_CODE_NOT_ALLOWED
304-
);
305-
};
306-
}
307-
308277
/**
309278
* Check that the output attribute has the right value
310279
*
@@ -378,7 +347,7 @@ private function testListText(SimpleXMLElement $layout, File $phpcsFile): void
378347
dom_import_simplexml($elements[0])->getLineNo()-1,
379348
self::ERROR_CODE_OBSOLETE_CLASS
380349
);
381-
};
350+
}
382351
}
383352

384353
/**

Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php

-70
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@
1313
class PhtmlTemplateSniff implements Sniff
1414
{
1515
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound';
16-
private const WARNING_CODE_THIS_USAGE = 'ThisUsageObsolete';
1716
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess';
18-
19-
private const OBSOLETE_REGEX = [
20-
'FoundJQueryUI' => [
21-
'pattern' => '/(["\'])jquery\/ui\1/',
22-
'message' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ui widget instead'
23-
],
24-
'FoundDataMageInit' => [
25-
'pattern' => '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/',
26-
'message' => 'Please do not initialize JS component in php. Do it in template'
27-
],
28-
'FoundXMagentoInit' => [
29-
'pattern' => '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i',
30-
'message' => 'Please do not initialize JS component in php. Do it in template'
31-
],
32-
];
3317

3418
/**
3519
* @inheritdoc
@@ -51,18 +35,9 @@ public function process(File $phpcsFile, $stackPtr)
5135
$tokens = $phpcsFile->getTokens();
5236
if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) {
5337
$this->checkBlockVariable($phpcsFile, $stackPtr, $tokens);
54-
$this->checkThisVariable($phpcsFile, $stackPtr, $tokens);
5538
}
5639
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML || $tokens[$stackPtr]['code'] === T_HEREDOC) {
5740
$this->checkHtml($phpcsFile, $stackPtr);
58-
59-
$file = $phpcsFile->getFilename();
60-
61-
if (strpos($file, '/view/frontend/templates/') !== false
62-
|| strpos($file, '/view/base/templates/') !== false
63-
) {
64-
$this->checkHtmlSpecificFiles($phpcsFile, $stackPtr);
65-
}
6641
}
6742
}
6843

@@ -90,30 +65,6 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
9065
}
9166
}
9267

93-
/**
94-
* Check access to members and methods of Block class through $this
95-
*
96-
* @param File $phpcsFile
97-
* @param int $stackPtr
98-
* @param array $tokens
99-
*/
100-
private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens): void
101-
{
102-
$varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1);
103-
if ($tokens[$varPos]['content'] !== '$this') {
104-
return;
105-
}
106-
$stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
107-
if (strpos($tokens[$stringPos]['content'], 'helper') === false) {
108-
$phpcsFile->addWarning(
109-
'Access to members and methods of Block class through $this is ' .
110-
'obsolete in phtml templates. Use only $block instead of $this.',
111-
$stringPos,
112-
self::WARNING_CODE_THIS_USAGE
113-
);
114-
}
115-
}
116-
11768
/**
11869
* Check use of "text/javascript" type
11970
*
@@ -132,25 +83,4 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void
13283
);
13384
}
13485
}
135-
136-
/**
137-
* Check of some obsoletes uses in specific files
138-
*
139-
* @param File $phpcsFile
140-
* @param int $stackPtr
141-
*/
142-
private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void
143-
{
144-
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
145-
146-
foreach (self::OBSOLETE_REGEX as $code => $data) {
147-
if (preg_match($data['pattern'], $content)) {
148-
$phpcsFile->addWarning(
149-
$data['message'],
150-
$stackPtr,
151-
$code
152-
);
153-
}
154-
}
155-
}
15686
}

Magento2/Sniffs/PHPCompatibility/AbstractPrivateMethodsSniff.php

-104
This file was deleted.

0 commit comments

Comments
 (0)