Skip to content

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

Merged
merged 8 commits into from
Sep 23, 2021

Conversation

jcuerdo
Copy link
Contributor

@jcuerdo jcuerdo commented Sep 14, 2021

Correct:

public function __construct(
    ...
    Model $model = null
) {
    $this->model = $model ?? ObjectManager::getInstance()->get(Model::class);
}

Incorrect:

public function __construct(
    ...
) {
    $this->model = ObjectManager::getInstance()->get(Model::class);
}

 
This check is not applicable to factories. If the class name (file name) ends with Factory - this check should be skipped.

@jcuerdo jcuerdo marked this pull request as draft September 14, 2021 14:21
Copy link
Contributor

@svera svera left a 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.
Copy link
Contributor

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 &&
Copy link
Contributor

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();
}
Copy link
Contributor

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?

Suggested change
}
}
/**
* @return Model
*/
public function objectManagerAssignedToAVariableBad(): void
{
$objectManager = ObjectManager::getInstance();
$model = $objectManager->get(Model::class);
$model->something();
}

Copy link
Contributor Author

@jcuerdo jcuerdo Sep 16, 2021

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.

@jcuerdo jcuerdo marked this pull request as ready for review September 17, 2021 11:01
@jcuerdo jcuerdo requested a review from svera September 17, 2021 11:01
Copy link
Member

@sivaschenko sivaschenko left a 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

Comment on lines 105 to 113
$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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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';

Copy link
Contributor Author

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
Copy link
Member

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;?

Copy link
Contributor Author

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;
Copy link
Member

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

Suggested change
$this->uses [] = $use;
$this->uses[end($use)] = implode('\', $use);

Copy link
Contributor Author

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)) {
Copy link
Member

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

@jcuerdo
Copy link
Contributor Author

jcuerdo commented Sep 23, 2021

@magento import pr to magento-commerce/magento-coding-standard

@m2-github-services
Copy link
Contributor

@jcuerdo the Pull Request is successfully imported.

@magento-devops-reposync-svc magento-devops-reposync-svc merged commit 8e4a639 into magento:develop Sep 23, 2021
@jcuerdo jcuerdo deleted the AC-662 branch September 23, 2021 13:56
magento-devops-reposync-svc pushed a commit that referenced this pull request Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants