Skip to content
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

Error on references of PHPStan internal class names #2932

Merged
merged 3 commits into from Feb 23, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 22, 2024

As discussed in phpstan/phpstan#10587 (comment) this PR adds a new ClassForbiddenNameCheck which reports errors when code references a PHPStan prefixed class from a phpstan.phar file

@staabm staabm changed the title Error on refernces of PHPStan internal class names Error on references of PHPStan internal class names Feb 22, 2024
@staabm staabm marked this pull request as ready for review February 22, 2024 16:31
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Feb 22, 2024

Similar problems can happen on other popular tools, e.g. Rector.
Should we also hardcode a rector prefix?

@ondrejmirtes
Copy link
Member

Thank you, I love this!

I wonder if we could detect it automatically but then we're in the https://github.com/shipmonk-rnd/composer-dependency-analyser area so probably not worth it.

I'd hardcode:

$errors = [];

if ($checkClassCaseSensitivity) {
$errors += $this->classCaseSensitivityCheck->checkClassNames($pairs);
Copy link
Member

Choose a reason for hiding this comment

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

I don't really like doing + here. I'd rather iterate over the array and add the error one-by-one with $errors[] = ....

'Internal %s Class cannot be referenced: %s.',
$projectName,
$className,
))->line($pair->getNode()->getLine())->build();
Copy link
Member

Choose a reason for hiding this comment

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

Should be non-ignorable.

}

$errors[] = RuleErrorBuilder::message(sprintf(
'Internal %s Class cannot be referenced: %s.',
Copy link
Member

Choose a reason for hiding this comment

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

Please rewrite this as: "Referencing prefixed PHPStan class: _PHPStan_d5242a5\Symfony/Finder"

And add a tip without the prefix: "This is most likely unintentional. Did you mean to type Symfony\Finder?"

@ondrejmirtes ondrejmirtes merged commit ca55346 into phpstan:1.10.x Feb 23, 2024
440 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants