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 75cd1c6

Browse filesBrowse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into develop
2 parents 7ac918c + 7521587 commit 75cd1c6
Copy full SHA for 75cd1c6
Expand file treeCollapse file tree

25 files changed

+1060
-293
lines changed

‎.github/workflows/php.yml

Copy file name to clipboardExpand all lines: .github/workflows/php.yml
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
php-version:
1616
- "8.1"
1717
- "8.2"
18+
- "8.3"
1819
dependencies:
1920
- "lowest"
2021
- "highest"
@@ -89,4 +90,4 @@ jobs:
8990
run: composer install
9091

9192
- name: Run rector
92-
run: vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php --autoload-file vendor/phpcompatibility/php-compatibility/PHPCSAliases.php
93+
run: vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php --autoload-file vendor/magento/php-compatibility-fork/PHPCSAliases.php

‎Magento2/Sniffs/Annotation/MethodAnnotationStructureSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Annotation/MethodAnnotationStructureSniff.php
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,33 @@ public function register()
5151
public function process(File $phpcsFile, $stackPtr)
5252
{
5353
$tokens = $phpcsFile->getTokens();
54-
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($stackPtr), 0);
55-
$commentEndPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr), 0);
56-
if (!$commentStartPtr) {
57-
$phpcsFile->addError('Comment block is missing', $stackPtr, 'MethodArguments');
54+
$commentEndPtr = $stackPtr;
55+
$tokensToFind = [
56+
\T_SEMICOLON,
57+
\T_OPEN_CURLY_BRACKET,
58+
\T_CLOSE_CURLY_BRACKET,
59+
\T_ATTRIBUTE_END,
60+
\T_DOC_COMMENT_CLOSE_TAG
61+
];
62+
63+
do {
64+
$commentEndPtr = $phpcsFile->findPrevious($tokensToFind, $commentEndPtr - 1);
65+
if ($commentEndPtr !== false
66+
&& $tokens[$commentEndPtr]['code'] === \T_ATTRIBUTE_END
67+
&& isset($tokens[$commentEndPtr]['attribute_opener'])
68+
) {
69+
$commentEndPtr = $tokens[$commentEndPtr]['attribute_opener'];
70+
}
71+
} while ($commentEndPtr !== false && !in_array($tokens[$commentEndPtr]['code'], $tokensToFind, true));
72+
73+
if ($commentEndPtr === false || $tokens[$commentEndPtr]['code'] !== \T_DOC_COMMENT_CLOSE_TAG) {
74+
$phpcsFile->addError('Comment block is missing', $stackPtr, 'NoCommentBlock');
5875
return;
5976
}
6077

78+
$commentStartPtr = $tokens[$commentEndPtr]['comment_opener']
79+
?? $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $commentEndPtr - 1);
80+
6181
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
6282
$phpcsFile->addWarning(
6383
'Motivation behind the added @deprecated tag MUST be explained. '

‎Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php
+40-2Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,29 @@ public function process(File $phpcsFile, $stackPtr)
6363
return;
6464
}
6565

66+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
67+
6668
if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
67-
$phpcsFile->addWarning(
69+
$fix = $phpcsFile->addFixableWarning(
6870
sprintf(
6971
'%s description must contain meaningful information beyond what its name provides or be removed.',
7072
ucfirst($tokens[$stackPtr]['content'])
7173
),
7274
$stackPtr,
7375
'InvalidDescription'
7476
);
77+
78+
if ($fix) {
79+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
80+
$phpcsFile->fixer->replaceToken($i, '');
81+
}
82+
83+
if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
84+
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
85+
) {
86+
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
87+
}
88+
}
7589
}
7690

7791
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
@@ -105,11 +119,35 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
105119
}
106120

107121
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
108-
$phpcsFile->addWarning(
122+
$fix = $phpcsFile->addFixableWarning(
109123
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
110124
$i,
111125
'ForbiddenTags'
112126
);
127+
128+
if ($fix) {
129+
for ($j = $i - 1; $j > $commentStartPtr; $j--) {
130+
if (!in_array($tokens[$j]['code'], [T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], true)) {
131+
break;
132+
}
133+
134+
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
135+
break;
136+
}
137+
138+
$phpcsFile->fixer->replaceToken($j, '');
139+
}
140+
141+
$phpcsFile->fixer->replaceToken($i, '');
142+
143+
for ($j = $i + 1; $j < $commentCloserPtr; $j++) {
144+
$phpcsFile->fixer->replaceToken($j, '');
145+
146+
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
147+
break;
148+
}
149+
}
150+
}
113151
}
114152
}
115153

‎Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff
3232
T_NULLABLE,
3333
T_BITWISE_AND,
3434
T_TYPE_UNION,
35+
T_READONLY,
3536
];
3637

3738
/**

‎Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
8787
'^parse_ini_file$' => null,
8888
'^parsekit_compile_string$' => null,
8989
'^pcntl_.*$' => null,
90+
'^posix_isatty$' => 'stream_isatty',
9091
'^posix_.*$' => null,
9192
'^pfpro_.*$' => null,
9293
'^pfsockopen$' => null,
+106Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento2\Sniffs\Legacy;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
15+
16+
class EscapeMethodsOnBlockClassSniff implements Sniff
17+
{
18+
private const ESCAPER_METHODS = [
19+
'escapeCss' => true,
20+
'escapeHtml' => true,
21+
'escapeHtmlAttr' => true,
22+
'escapeJs' => true,
23+
'escapeJsQuote' => true,
24+
'escapeQuote' => true,
25+
'escapeUrl' => true,
26+
'escapeXssInUrl' => true,
27+
];
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function register()
33+
{
34+
return [
35+
T_OBJECT_OPERATOR,
36+
];
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function process(File $phpcsFile, $stackPtr)
43+
{
44+
$tokens = $phpcsFile->getTokens();
45+
46+
if ($stackPtr <= 1 || !isset($tokens[$stackPtr + 2])) {
47+
return;
48+
}
49+
50+
$objectPtr = $stackPtr - 1;
51+
if ($tokens[$objectPtr]['code'] !== T_VARIABLE) {
52+
$objectPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $objectPtr, null, true);
53+
54+
if (!$objectPtr) {
55+
return;
56+
}
57+
}
58+
59+
if ($tokens[$objectPtr]['code'] !== T_VARIABLE
60+
|| $tokens[$objectPtr]['content'] !== '$block'
61+
) {
62+
return;
63+
}
64+
65+
$methodPtr = $stackPtr + 1;
66+
if ($tokens[$methodPtr]['code'] !== T_STRING) {
67+
$methodPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $methodPtr, null, true);
68+
69+
if (!$methodPtr) {
70+
return;
71+
}
72+
}
73+
74+
if ($tokens[$methodPtr]['code'] !== T_STRING
75+
|| !isset(self::ESCAPER_METHODS[$tokens[$methodPtr]['content']])
76+
) {
77+
return;
78+
}
79+
80+
$openParenPtr = $methodPtr + 1;
81+
if ($tokens[$openParenPtr]['code'] !== T_OPEN_PARENTHESIS) {
82+
$openParenPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $openParenPtr, null, true);
83+
84+
if (!$openParenPtr) {
85+
return;
86+
}
87+
}
88+
89+
if ($tokens[$openParenPtr]['code'] !== T_OPEN_PARENTHESIS) {
90+
return;
91+
}
92+
93+
$fix = $phpcsFile->addFixableWarning(
94+
'Using %s on $block is deprecated. Please use equivalent method on $escaper',
95+
$methodPtr,
96+
'Found',
97+
[
98+
$tokens[$methodPtr]['content'], // method name
99+
]
100+
);
101+
102+
if ($fix) {
103+
$phpcsFile->fixer->replaceToken($objectPtr, '$escaper');
104+
}
105+
}
106+
}

‎Magento2/Sniffs/Security/XssTemplateSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Security/XssTemplateSniff.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ public function process(File $phpcsFile, $stackPtr)
147147
private function findSpecialAnnotation($stackPtr)
148148
{
149149
if ($this->tokens[$stackPtr]['code'] === T_ECHO) {
150-
$startOfStatement = $this->file->findPrevious(T_OPEN_TAG, $stackPtr);
150+
$startOfStatement = $this->file->findPrevious([T_OPEN_TAG, T_SEMICOLON], $stackPtr);
151151
return $this->file->findPrevious(T_COMMENT, $stackPtr, $startOfStatement);
152152
}
153153
if ($this->tokens[$stackPtr]['code'] === T_OPEN_TAG_WITH_ECHO) {
154-
$endOfStatement = $this->file->findNext(T_CLOSE_TAG, $stackPtr);
154+
$endOfStatement = $this->file->findNext([T_CLOSE_TAG, T_SEMICOLON], $stackPtr);
155155
return $this->file->findNext(T_COMMENT, $stackPtr, $endOfStatement);
156156
}
157157
return false;

‎Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc

Copy file name to clipboardExpand all lines: Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc
+61-1Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
function functionWithNoPrecedingDocBlock() {}
33
/**
44
* Class for method structure for annotations test cases
55
*/
@@ -389,4 +389,64 @@ class MethodAnnotationFixture
389389
{
390390
return false;
391391
}
392+
393+
/** @var OutputInterface */
394+
private $output;
395+
396+
private function thisMethodHasNoDocBlock(): bool
397+
{
398+
return false;
399+
}
400+
}
401+
402+
/**
403+
* Class with comment
404+
*/
405+
class ClassWithDocBlock
406+
{
407+
private function methodWithNoDocBlock(): bool
408+
{
409+
return false;
410+
}
411+
412+
#[
413+
/**
414+
* This docBloc is not for the method but for the attribute
415+
*/
416+
\ReturnTypeWillChange
417+
]
418+
public function methodWithDocBlockInsideAttributesDelimiters(string $text): string
419+
{
420+
return $text;
421+
}
422+
423+
#[\ReturnTypeWillChange]
424+
public function methodWithAttributeAndNoDocBlock(string $text): string
425+
{
426+
return $text;
427+
}
428+
429+
/**
430+
* Short description.
431+
*
432+
* @param string $text
433+
* @return string
434+
*/
435+
#[\ReturnTypeWillChange]
436+
public function methodWithAttributeAndValidDocBlock(string $text): string
437+
{
438+
return $text;
439+
}
440+
441+
#[\ReturnTypeWillChange]
442+
/**
443+
* Short description.
444+
*
445+
* @param string $text
446+
* @return string
447+
*/
448+
public function methodWithAttributeAndValidDocBlock2(string $text): string
449+
{
450+
return $text;
451+
}
392452
}

‎Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php

Copy file name to clipboardExpand all lines: Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class MethodAnnotationStructureUnitTest extends AbstractSniffUnitTest
1515
public function getErrorList()
1616
{
1717
return [
18+
2 => 1,
1819
10 => 1,
1920
18 => 1,
2021
30 => 1,
@@ -31,13 +32,18 @@ public function getErrorList()
3132
185 => 1,
3233
227 => 1,
3334
235 => 1,
35+
261 => 1,
3436
268 => 2,
3537
269 => 1,
3638
277 => 1,
3739
278 => 1,
3840
288 => 1,
3941
289 => 1,
40-
298 => 1
42+
298 => 1,
43+
396 => 1,
44+
407 => 1,
45+
418 => 1,
46+
424 => 1,
4147
];
4248
}
4349

‎Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.1.inc

Copy file name to clipboardExpand all lines: Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.1.inc
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,10 @@ class AlsoDeprecatedButHandler
179179

180180
}
181181

182+
/**
183+
* @package this tag should not be used
184+
*/
185+
class OnlyUselessCommentContent
186+
{
187+
188+
}

0 commit comments

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