-
Notifications
You must be signed in to change notification settings - Fork 159
AC-662: Create phpcs static check for AutogeneratedClassNotInConstructorTest #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there are changes missing also in ruleset, as we should ignore factory files
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Detects possible usage of 'var' language construction. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment accurate?
{ | ||
$prev = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1); | ||
$next = $phpcsFile->findNext(T_STRING, $stackPtr + 1); | ||
if ($prev && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to
return $prev &&
$next &&
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' &&
$phpcsFile->getTokens()[$next]['content'] === 'getInstance';
{ | ||
$model = $this->model ?? ObjectManager::getInstance()->get(Model::class); | ||
$model->something(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another case missing here, what about assigning an object manager instance to a variable?
} | |
} | |
/** | |
* @return Model | |
*/ | |
public function objectManagerAssignedToAVariableBad(): void | |
{ | |
$objectManager = ObjectManager::getInstance(); | |
$model = $objectManager->get(Model::class); | |
$model->something(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not on the ticket. And I can see in the original sniff is not covered I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @jcuerdo ! Please see my review comments
$prev = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1); | ||
$next = $phpcsFile->findNext(T_STRING, $stackPtr + 1); | ||
if ($prev && | ||
$next && | ||
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' && | ||
$phpcsFile->getTokens()[$next]['content'] === 'getInstance') { | ||
return true; | ||
} | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$prev = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1); | |
$next = $phpcsFile->findNext(T_STRING, $stackPtr + 1); | |
if ($prev && | |
$next && | |
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' && | |
$phpcsFile->getTokens()[$next]['content'] === 'getInstance') { | |
return true; | |
} | |
return false; | |
return $phpcsFile->findPrevious(T_STRING, $stackPtr - 1) && | |
$phpcsFile->findNext(T_STRING, $stackPtr + 1) && | |
$phpcsFile->getTokens()[$prev]['content'] === 'ObjectManager' && | |
$phpcsFile->getTokens()[$next]['content'] === 'getInstance'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code does not work, you are using variables $prev and $next that are not defined.
* @param string $className | ||
* @return string | ||
*/ | ||
private function getClassNamespace(string $className): string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function handling alias like use Magento\Module\Class as MagentoModuleClass;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not included.
while ($usePosition = $phpcsFile->findNext(T_STRING, $usePosition + 1, $useEnd)) { | ||
$use[] = $phpcsFile->getTokens()[$usePosition]['content']; | ||
} | ||
$this->uses [] = $use; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be easier to process use in getClassNamespace
if uses will be stored this way
$this->uses [] = $use; | |
$this->uses[end($use)] = implode('\', $use); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option is easier to compare on line 122.
} | ||
|
||
$variableInParameters = false; | ||
if ($variable = $phpcsFile->findNext(T_VARIABLE, $equalsPtr, $statementEnd)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is too long, please consider decomposition into several private functions
@magento import pr to magento-commerce/magento-coding-standard |
@jcuerdo the Pull Request is successfully imported. |
Update the new version
Correct:
Incorrect:
This check is not applicable to factories. If the class name (file name) ends with Factory - this check should be skipped.