Skip to content

Commit 504a50b

Browse files
committed
AC-662: Create phpcs static check for AutogeneratedClassNotInConstructorTest
1 parent 1d499c2 commit 504a50b

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function register()
4141
public function process(File $phpcsFile, $stackPtr)
4242
{
4343
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_USE') {
44-
$this->registerUses($phpcsFile, $stackPtr);
44+
$this->registerUse($phpcsFile, $stackPtr);
4545
}
4646
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_FUNCTION') {
4747
$this->registerConstructorParameters($phpcsFile, $stackPtr);
@@ -59,29 +59,9 @@ public function process(File $phpcsFile, $stackPtr)
5959
return;
6060
}
6161

62-
$variableInParameters = false;
63-
if ($variable = $phpcsFile->findNext(T_VARIABLE, $equalsPtr, $statementEnd)) {
64-
$variableName = $phpcsFile->getTokens()[$variable]['content'];
65-
if ($variableName === '$this') {
66-
$variableName = $this->getNext($phpcsFile, $variable, $statementEnd, T_STRING)['content'];
67-
}
68-
foreach ($this->constructorParameters as $parameter) {
69-
$parameterName = $parameter['name'];
70-
if ($this->variableName($parameterName) === $this->variableName($variableName)) {
71-
$variableInParameters = true;
72-
}
73-
}
74-
}
75-
76-
if (!$variableInParameters) {
62+
if (!$this->isVariableInConstructorParameters($phpcsFile, $equalsPtr, $statementEnd)) {
7763
$next = $stackPtr;
78-
while ($next = $phpcsFile->findNext(T_DOUBLE_COLON, $next + 1, $statementEnd)) {
79-
if ($this->getNext($phpcsFile, $next, $statementEnd, T_STRING)['content'] === 'class') {
80-
$className = $this->getPrevious($phpcsFile, $next, T_STRING)['content'];
81-
}
82-
}
83-
84-
$className = $this->getClassNamespace($className);
64+
$className = $this->obtainClassToGetOrCreate($phpcsFile, $next, $statementEnd);
8565

8666
$phpcsFile->addWarning(
8767
sprintf("Class %s needs to be requested in constructor, " .
@@ -118,9 +98,9 @@ private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): boo
11898
*/
11999
private function getClassNamespace(string $className): string
120100
{
121-
foreach ($this->uses as $use) {
122-
if (end($use) === $className) {
123-
$className = implode('/', $use);
101+
foreach ($this->uses as $key => $use) {
102+
if ($key === $className) {
103+
return $use;
124104
}
125105
}
126106
return $className;
@@ -132,15 +112,21 @@ private function getClassNamespace(string $className): string
132112
* @param File $phpcsFile
133113
* @param int $stackPtr
134114
*/
135-
private function registerUses(File $phpcsFile, int $stackPtr): void
115+
private function registerUse(File $phpcsFile, int $stackPtr): void
136116
{
137117
$useEnd = $phpcsFile->findEndOfStatement($stackPtr);
138118
$use = [];
139119
$usePosition = $stackPtr;
140120
while ($usePosition = $phpcsFile->findNext(T_STRING, $usePosition + 1, $useEnd)) {
141121
$use[] = $phpcsFile->getTokens()[$usePosition]['content'];
142122
}
143-
$this->uses [] = $use;
123+
124+
$key = end($use);
125+
if ($phpcsFile->findNext(T_AS, $stackPtr, $useEnd)) {
126+
$this->uses[$key] = implode("\\", array_slice($use, 0, count($use) - 1));
127+
} else {
128+
$this->uses[$key] = implode("\\", $use);
129+
}
144130
}
145131

146132
/**
@@ -194,4 +180,44 @@ protected function variableName(string $parameterName): string
194180
{
195181
return str_replace('$', '', $parameterName);
196182
}
183+
184+
/**
185+
* @param File $phpcsFile
186+
* @param int $equalsPtr
187+
* @param int $statementEnd
188+
* @return bool
189+
*/
190+
private function isVariableInConstructorParameters(File $phpcsFile, int $equalsPtr, int $statementEnd): bool
191+
{
192+
if ($variable = $phpcsFile->findNext(T_VARIABLE, $equalsPtr, $statementEnd)) {
193+
$variableName = $phpcsFile->getTokens()[$variable]['content'];
194+
if ($variableName === '$this') {
195+
$variableName = $this->getNext($phpcsFile, $variable, $statementEnd, T_STRING)['content'];
196+
}
197+
foreach ($this->constructorParameters as $parameter) {
198+
$parameterName = $parameter['name'];
199+
if ($this->variableName($parameterName) === $this->variableName($variableName)) {
200+
return true;
201+
}
202+
}
203+
}
204+
return false;
205+
}
206+
207+
/**
208+
* @param File $phpcsFile
209+
* @param $next
210+
* @param int $statementEnd
211+
* @return string
212+
*/
213+
private function obtainClassToGetOrCreate(File $phpcsFile, $next, int $statementEnd): string
214+
{
215+
while ($next = $phpcsFile->findNext(T_DOUBLE_COLON, $next + 1, $statementEnd)) {
216+
if ($this->getNext($phpcsFile, $next, $statementEnd, T_STRING)['content'] === 'class') {
217+
$className = $this->getPrevious($phpcsFile, $next, T_STRING)['content'];
218+
}
219+
}
220+
221+
return $this->getClassNamespace($className);
222+
}
197223
}

Magento2/Tests/PHP/AutogeneratedClassNotInConstructorUnitTest.1.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare(strict_types = 1);
77

88
namespace Magento2\Tests\PHP;
99

10-
use Magento2\Model;
10+
use Magento2\OneModel as Model;
1111

1212
class Good
1313
{

Magento2/ruleset.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<type>warning</type>
152152
</rule>
153153
<rule ref="Magento2.Html.HtmlBinding">
154+
<include-pattern>*\/.phtml$</include-pattern>
154155
<severity>9</severity>
155156
<type>warning</type>
156157
</rule>
@@ -247,7 +248,7 @@
247248
<severity>8</severity>
248249
<type>warning</type>
249250
</rule>
250-
<rule ref="Magento2.Legacy.WidgetXML.FoundObsoleteNode">
251+
<rule ref="Magento2.Legacy.WidgetXML">
251252
<include-pattern>*\/widget.xml$</include-pattern>
252253
<severity>8</severity>
253254
<type>warning</type>

0 commit comments

Comments
 (0)