Skip to content

Commit 7d317ba

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

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ public function process(File $phpcsFile, $stackPtr)
6060
}
6161

6262
if (!$this->isVariableInConstructorParameters($phpcsFile, $equalsPtr, $statementEnd)) {
63-
$next = $stackPtr;
64-
$className = $this->obtainClassToGetOrCreate($phpcsFile, $next, $statementEnd);
63+
$className = $this->obtainClassToGetOrCreate($phpcsFile, $stackPtr, $statementEnd);
6564

66-
$phpcsFile->addWarning(
65+
$phpcsFile->addError(
6766
sprintf("Class %s needs to be requested in constructor, " .
6867
"otherwise compiler will not be able to find and generate these classes", $className),
6968
$stackPtr,
@@ -182,6 +181,8 @@ protected function variableName(string $parameterName): string
182181
}
183182

184183
/**
184+
* Checks if a variable is present in the constructor parameters
185+
*
185186
* @param File $phpcsFile
186187
* @param int $equalsPtr
187188
* @param int $statementEnd
@@ -205,12 +206,14 @@ private function isVariableInConstructorParameters(File $phpcsFile, int $equalsP
205206
}
206207

207208
/**
209+
* Obtain the class inside ObjectManager::getInstance()->get|create()
210+
*
208211
* @param File $phpcsFile
209-
* @param $next
212+
* @param int $next
210213
* @param int $statementEnd
211214
* @return string
212215
*/
213-
private function obtainClassToGetOrCreate(File $phpcsFile, $next, int $statementEnd): string
216+
private function obtainClassToGetOrCreate(File $phpcsFile, int $next, int $statementEnd): string
214217
{
215218
while ($next = $phpcsFile->findNext(T_DOUBLE_COLON, $next + 1, $statementEnd)) {
216219
if ($this->getNext($phpcsFile, $next, $statementEnd, T_STRING)['content'] === 'class') {

Magento2/Tests/PHP/AutogeneratedClassNotInConstructorUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AutogeneratedClassNotInConstructorUnitTest extends AbstractSniffUnitTest
1414
/**
1515
* @inheritdoc
1616
*/
17-
public function getWarningList($filename = '')
17+
public function getErrorList($filename = '')
1818
{
1919
if ($filename === 'AutogeneratedClassNotInConstructorUnitTest.1.php.inc') {
2020
return [
@@ -32,7 +32,7 @@ public function getWarningList($filename = '')
3232
/**
3333
* @inheritdoc
3434
*/
35-
public function getErrorList()
35+
public function getWarningList($filename = '')
3636
{
3737
return [];
3838
}

Magento2/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
<severity>10</severity>
116116
<type>error</type>
117117
</rule>
118+
<rule ref="Magento2.PHP.AutogeneratedClassNotInConstructor">
119+
<include-pattern>*\.php$</include-pattern>
120+
<severity>10</severity>
121+
<type>error</type>
122+
</rule>
118123

119124
<!-- Severity 9 warnings: Possible security and issues that may cause bugs. -->
120125
<rule ref="Generic.Files.ByteOrderMark">

0 commit comments

Comments
 (0)