Skip to content

Commit 64fe3cd

Browse files
authored
Merge pull request #57 from magento-commerce/imported-magento-magento-coding-standard-264
[Imported] Version 9 master update
2 parents a490fea + 9039876 commit 64fe3cd

File tree

69 files changed

+1552
-76
lines changed

Some content is hidden

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

69 files changed

+1552
-76
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
.project/
77
.settings/
88
.vscode/
9+
10+
.phpunit.result.cache

Magento2/Helpers/Tokenizer/AbstractTokenizer.php

-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
abstract class AbstractTokenizer
1212
{
1313
/**
14-
* Current index in string
15-
*
1614
* @var int
1715
*/
1816
protected $_currentIndex;
1917

2018
/**
21-
* String for tokenize
22-
*
2319
* @var string
2420
*/
2521
protected $_string;

Magento2/Helpers/Tokenizer/Variable.php

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
class Variable extends AbstractTokenizer
1313
{
1414
/**
15-
* Internal counter used to keep track of how deep in array parsing we are
16-
*
1715
* @var int
1816
*/
1917
protected $arrayDepth = 0;

Magento2/Sniffs/Annotation/MethodArgumentsSniff.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,6 @@ public function process(File $phpcsFile, $stackPtr)
609609
* @param File $phpcsFile
610610
* @param array $paramPointers
611611
*
612-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
613-
*
614612
* @see https://devdocs.magento.com/guides/v2.4/coding-standards/docblock-standard-general.html#format-consistency
615613
*/
616614
private function validateFormattingConsistency(
@@ -626,7 +624,9 @@ private function validateFormattingConsistency(
626624
if (isset($paramPointers[$ptr])) {
627625
$paramContent = $tokens[$paramPointers[$ptr] + 2]['content'];
628626
$paramDefinition = $paramDefinitions[$ptr];
629-
$argumentPositions[] = strpos($paramContent, $paramDefinition['paramName']);
627+
if (isset($paramDefinition['paramName'])) {
628+
$argumentPositions[] = strpos($paramContent, $paramDefinition['paramName']);
629+
}
630630
$commentPositions[] = $paramDefinition['comment']
631631
? strrpos($paramContent, $paramDefinition['comment']) : null;
632632
}

Magento2/Sniffs/Classes/DiscouragedDependenciesSniff.php

-6
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,16 @@ class DiscouragedDependenciesSniff implements Sniff
3232
protected $warningCode = 'ConstructorProxyInterceptor';
3333

3434
/**
35-
* Aliases of proxies or plugins from use statements
36-
*
3735
* @var string[]
3836
*/
3937
private $aliases = [];
4038

4139
/**
42-
* The current file - used for clearing USE aliases when file changes
43-
*
4440
* @var null|string
4541
*/
4642
private $currentFile = null;
4743

4844
/**
49-
* Terms to search for in variables and namespaces
50-
*
5145
* @var string[]
5246
*/
5347
public $incorrectClassNames = ['proxy','interceptor'];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
namespace Magento2\Sniffs\Commenting;
3+
4+
use PHP_CodeSniffer\Files\File;
5+
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
6+
use PHP_CodeSniffer\Util\Tokens;
7+
use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
8+
9+
/**
10+
* Class ClassPropertyPHPDocFormattingSniff
11+
*/
12+
class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff
13+
{
14+
15+
/**
16+
* @var array
17+
*/
18+
private $ignoreTokens = [
19+
T_PUBLIC,
20+
T_PRIVATE,
21+
T_PROTECTED,
22+
T_VAR,
23+
T_STATIC,
24+
T_WHITESPACE,
25+
];
26+
27+
/**
28+
* @var PHPDocFormattingValidator
29+
*/
30+
private $PHPDocFormattingValidator;
31+
32+
/**
33+
* Constructs an ClassPropertyPHPDocFormattingSniff.
34+
*/
35+
public function __construct()
36+
{
37+
$scopes = Tokens::$ooScopeTokens;
38+
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
39+
$listen = [
40+
T_VARIABLE,
41+
T_DOUBLE_QUOTED_STRING,
42+
T_HEREDOC,
43+
];
44+
45+
parent::__construct($scopes, $listen, true);
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function processMemberVar(File $phpcsFile, $stackPtr)
52+
{
53+
$tokens = $phpcsFile->getTokens();
54+
55+
$commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($stackPtr - 1), null, true);
56+
if ($commentEnd === false
57+
|| ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
58+
&& $tokens[$commentEnd]['code'] !== T_COMMENT)
59+
) {
60+
$phpcsFile->addWarning('Missing PHP DocBlock for class property.', $stackPtr, 'Missing');
61+
return;
62+
}
63+
$commentStart = $tokens[$commentEnd]['comment_opener'];
64+
$foundVar = null;
65+
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
66+
if ($tokens[$tag]['content'] === '@var') {
67+
if ($foundVar !== null) {
68+
$error = 'Only one @var tag is allowed for class property declaration.';
69+
$phpcsFile->addWarning($error, $tag, 'DuplicateVar');
70+
} else {
71+
$foundVar = $tag;
72+
}
73+
}
74+
}
75+
76+
if ($foundVar === null) {
77+
$error = 'Class properties must have type declaration using @var tag.';
78+
$phpcsFile->addWarning($error, $stackPtr, 'MissingVar');
79+
return;
80+
}
81+
82+
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
83+
if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
84+
$error = 'Content missing for @var tag in class property declaration.';
85+
$phpcsFile->addWarning($error, $foundVar, 'EmptyVar');
86+
return;
87+
}
88+
89+
// Check if class has already have meaningful description after @var tag
90+
$isShortDescriptionAfterVar = $phpcsFile->findNext(
91+
T_DOC_COMMENT_STRING,
92+
$foundVar + 4,
93+
$commentEnd,
94+
false,
95+
null,
96+
false
97+
);
98+
if ($this->PHPDocFormattingValidator->providesMeaning(
99+
$isShortDescriptionAfterVar,
100+
$commentStart,
101+
$tokens
102+
) !== true) {
103+
preg_match(
104+
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i',
105+
$tokens[($foundVar + 2)]['content'],
106+
$varParts
107+
);
108+
if ($varParts[1]) {
109+
return;
110+
}
111+
$error = 'Short description duplicates class property name.';
112+
$phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar');
113+
return;
114+
}
115+
// Check if class has already have meaningful description before @var tag
116+
$isShortDescriptionPreviousVar = $phpcsFile->findPrevious(
117+
T_DOC_COMMENT_STRING,
118+
$foundVar,
119+
$commentStart,
120+
false,
121+
null,
122+
false
123+
);
124+
if ($this->PHPDocFormattingValidator->providesMeaning(
125+
$isShortDescriptionPreviousVar,
126+
$commentStart,
127+
$tokens
128+
) !== true) {
129+
preg_match(
130+
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i',
131+
$tokens[($foundVar + 2)]['content'],
132+
$varParts
133+
);
134+
if ($varParts[1]) {
135+
return;
136+
}
137+
$error = 'Short description duplicates class property name.';
138+
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar');
139+
return;
140+
}
141+
}
142+
143+
/**
144+
* @inheritDoc
145+
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
146+
*/
147+
protected function processVariable(File $phpcsFile, $stackPtr)
148+
{
149+
}
150+
151+
/**
152+
* @inheritDoc
153+
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
154+
*/
155+
protected function processVariableInString(File $phpcsFile, $stackPtr)
156+
{
157+
}
158+
}

Magento2/Sniffs/Exceptions/DirectThrowSniff.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DirectThrowSniff implements Sniff
1616
/**
1717
* String representation of warning.
1818
* phpcs:disable Generic.Files.LineLength.TooLong
19+
* @var string
1920
*/
2021
protected $warningMessage = 'Direct throw of generic Exception is discouraged. Use context specific instead.';
2122
//phpcs:enable
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento2\Sniffs\Legacy;
8+
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
12+
class DiConfigSniff implements Sniff
13+
{
14+
private const WARNING_CODE = 'FoundObsoleteAttribute';
15+
16+
/**
17+
* @var string[] Associative array containing the obsolete nodes and the message to display when they are found.
18+
*/
19+
private $obsoleteDiNodes = [
20+
'<param' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">',
21+
'<instance' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">',
22+
'<array' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">',
23+
'<item key=' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">',
24+
'<value' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal.'
25+
];
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function register(): array
31+
{
32+
return [
33+
T_INLINE_HTML
34+
];
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function process(File $phpcsFile, $stackPtr)
41+
{
42+
$lineContent = $phpcsFile->getTokensAsString($stackPtr, 1);
43+
44+
foreach ($this->obsoleteDiNodes as $element => $message) {
45+
if (strpos($lineContent, $element) !== false) {
46+
$phpcsFile->addWarning(
47+
$message,
48+
$stackPtr,
49+
self::WARNING_CODE
50+
);
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)