Skip to content

Commit f7b3bcc

Browse files
committed
AC-662: Create phpcs static check for AutogeneratedClassNotInConstructorTest
1 parent a6d2530 commit f7b3bcc

3 files changed

+40
-39
lines changed

Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@
1515
*/
1616
class AutogeneratedClassNotInConstructorSniff implements Sniff
1717
{
18-
const ERROR_CODE = 'AUTOGENERATED_CLASS_NOT_IN_CONSTRUCTOR';
19-
/**
20-
* @var mixed
21-
*/
22-
private $constructorScopeOpener;
23-
/**
24-
* @var mixed
25-
*/
26-
private $constructorScopeCloser;
18+
private const ERROR_CODE = 'AUTOGENERATED_CLASS_NOT_IN_CONSTRUCTOR';
2719

2820
/**
2921
* @var array
@@ -55,9 +47,6 @@ public function process(File $phpcsFile, $stackPtr)
5547
$this->registerConstructorParameters($phpcsFile, $stackPtr);
5648
}
5749
if ($phpcsFile->getTokens()[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
58-
if (!$this->isInsideConstruct($stackPtr)) {
59-
return;
60-
}
6150
if (!$this->isObjectManagerGetInstance($phpcsFile, $stackPtr)) {
6251
return;
6352
}
@@ -73,9 +62,12 @@ public function process(File $phpcsFile, $stackPtr)
7362
$variableInParameters = false;
7463
if ($variable = $phpcsFile->findNext(T_VARIABLE, $equalsPtr, $statementEnd)) {
7564
$variableName = $phpcsFile->getTokens()[$variable]['content'];
65+
if ($variableName === '$this') {
66+
$variableName = $this->getNext($phpcsFile, $variable, $statementEnd, T_STRING)['content'];
67+
}
7668
foreach ($this->constructorParameters as $parameter) {
7769
$parameterName = $parameter['name'];
78-
if ($parameterName === $variableName) {
70+
if ($this->variableName($parameterName) === $this->variableName($variableName)) {
7971
$variableInParameters = true;
8072
}
8173
}
@@ -91,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr)
9183

9284
$className = $this->getClassNamespace($className);
9385

94-
$phpcsFile->addError(
86+
$phpcsFile->addWarning(
9587
sprintf("Class %s needs to be requested in constructor, " .
9688
"otherwise compiler will not be able to find and generate these classes", $className),
9789
$stackPtr,
@@ -121,17 +113,6 @@ private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): boo
121113
return false;
122114
}
123115

124-
/**
125-
* Checks if the code is inside __construct
126-
*
127-
* @param int $stackPtr
128-
* @return bool
129-
*/
130-
private function isInsideConstruct(int $stackPtr): bool
131-
{
132-
return $stackPtr > $this->constructorScopeOpener && $stackPtr < $this->constructorScopeCloser;
133-
}
134-
135116
/**
136117
* Get the complete class namespace from the use's
137118
*
@@ -176,36 +157,42 @@ private function registerConstructorParameters(File $phpcsFile, int $stackPtr):
176157
$functionName = $phpcsFile->getDeclarationName($stackPtr);
177158
if ($functionName == '__construct') {
178159
$this->constructorParameters = $phpcsFile->getMethodParameters($stackPtr);
179-
$this->constructorScopeOpener = $phpcsFile->getTokens()[$stackPtr]['scope_opener'];
180-
$this->constructorScopeCloser = $phpcsFile->getTokens()[$stackPtr]['scope_closer'];
181-
182160
}
183161
}
184162

185163
/**
186164
* Get next token
187165
*
188166
* @param File $phpcsFile
189-
* @param int $nextDoubleColumn
190-
* @param int $statementEnd
167+
* @param int $from
168+
* @param int $to
191169
* @param int|string|array $types
192170
* @return mixed
193171
*/
194-
private function getNext(File $phpcsFile, int $nextDoubleColumn, int $statementEnd, $types)
172+
private function getNext(File $phpcsFile, int $from, int $to, $types)
195173
{
196-
return $phpcsFile->getTokens()[$phpcsFile->findNext($types, $nextDoubleColumn + 1, $statementEnd)];
174+
return $phpcsFile->getTokens()[$phpcsFile->findNext($types, $from + 1, $to)];
197175
}
198176

199177
/**
200178
* Get previous token
201179
*
202180
* @param File $phpcsFile
203-
* @param int $nextDoubleColumn
181+
* @param int $from
204182
* @param int|string|array $types
205183
* @return mixed
206184
*/
207-
private function getPrevious(File $phpcsFile, int $nextDoubleColumn, $types)
185+
private function getPrevious(File $phpcsFile, int $from, $types)
186+
{
187+
return $phpcsFile->getTokens()[$phpcsFile->findPrevious($types, $from - 1)];
188+
}
189+
190+
/**
191+
* @param $parameterName
192+
* @return array|string|string[]
193+
*/
194+
protected function variableName($parameterName)
208195
{
209-
return $phpcsFile->getTokens()[$phpcsFile->findPrevious($types, $nextDoubleColumn - 1)];
196+
return str_replace('$', '', $parameterName);
210197
}
211198
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ class Good
2121
/**
2222
* @return Model
2323
*/
24-
public function otherMethodThatCallsGetInstance(): void
24+
public function otherMethodThatCallsGetInstanceBad(): void
2525
{
2626
$model = ObjectManager::getInstance()->get(Model::class);
2727
$model->something();
2828
}
29+
30+
/**
31+
* @return Model
32+
*/
33+
public function otherMethodThatCallsGetInstanceGood(): void
34+
{
35+
$model = $this->model ?? ObjectManager::getInstance()->get(Model::class);
36+
$model->something();
37+
}
2938
}

Magento2/Tests/PHP/AutogeneratedClassNotInConstructorUnitTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ class AutogeneratedClassNotInConstructorUnitTest extends AbstractSniffUnitTest
1414
/**
1515
* @inheritdoc
1616
*/
17-
public function getErrorList($filename = '')
17+
public function getWarningList($filename = '')
1818
{
19+
if ($filename === 'AutogeneratedClassNotInConstructorUnitTest.1.php.inc') {
20+
return [
21+
26 => 1,
22+
];
23+
}
1924
if ($filename === 'AutogeneratedClassNotInConstructorUnitTest.2.php.inc') {
2025
return [
21-
14 => 1
26+
14 => 1,
2227
];
2328
}
2429
return [];
@@ -27,7 +32,7 @@ public function getErrorList($filename = '')
2732
/**
2833
* @inheritdoc
2934
*/
30-
public function getWarningList()
35+
public function getErrorList()
3136
{
3237
return [];
3338
}

0 commit comments

Comments
 (0)