Skip to content

Error on references of PHPStan internal class names#2932

Merged
ondrejmirtes merged 3 commits intophpstan:1.10.xfrom
staabm:class-check
Feb 23, 2024
Merged

Error on references of PHPStan internal class names#2932
ondrejmirtes merged 3 commits intophpstan:1.10.xfrom
staabm:class-check

Conversation

@staabm
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Copy Markdown
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
Copy Markdown
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:

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Feb 22, 2024

$errors = [];

if ($checkClassCaseSensitivity) {
$errors += $this->classCaseSensitivityCheck->checkClassNames($pairs);
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@ondrejmirtes
Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

3 participants