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

Enabling constructor check for class-string variables #3661

Merged
merged 6 commits into from
Jan 16, 2025

Conversation

sayuprc
Copy link
Contributor

@sayuprc sayuprc commented Nov 24, 2024

Changed to check constructor arguments when creating an instance from a variable containing a class-string.
Skip checking if T in class-string is abstract or interface.

resolve phpstan/phpstan#12010

@sayuprc sayuprc force-pushed the resolve-12010 branch 2 times, most recently from 7663d06 to f6c5c24 Compare November 24, 2024 05:17
@sayuprc sayuprc marked this pull request as draft November 24, 2024 05:46
@sayuprc sayuprc marked this pull request as ready for review November 24, 2024 14:56
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -245,6 +248,18 @@ private function getClassNames(Node $node, Scope $scope): array

$type = $scope->getType($node->class);

if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

could this use Type->isClassStringType()?

Comment on lines 252 to 260
$classStringObjectType = $type->getClassStringObjectType();

return array_map(
static fn (string $name): array => [$name, true],
array_filter($classStringObjectType->getObjectClassNames(), function (string $name): bool {
$reflectionClass = $this->reflectionProvider->getClass($name);
return !$reflectionClass->isAbstract() && !$reflectionClass->isInterface();
}),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

could this be shortened with Type->getObjectClassReflections()?

@sayuprc
Copy link
Contributor Author

sayuprc commented Dec 2, 2024

@staabm
Thank you for your reviewing!
I tried fixing with 298e55e

@sayuprc sayuprc requested a review from staabm December 9, 2024 11:19
Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

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

lgtm thanks.

please give ondej some time to work thru the queue of open PRs

@@ -245,6 +248,18 @@ private function getClassNames(Node $node, Scope $scope): array

$type = $scope->getType($node->class);

if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry, rule logic can't rely on string type descriptions.

src/Rules/Classes/InstantiationRule.php Outdated Show resolved Hide resolved
@ondrejmirtes ondrejmirtes changed the base branch from 2.0.x to 2.1.x January 16, 2025 14:43
@ondrejmirtes ondrejmirtes merged commit 8a3f8c4 into phpstan:2.1.x Jan 16, 2025
255 of 257 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
Development

Successfully merging this pull request may close these issues.

Constructor args are not checked in new $class(...) when the $class is class-string
4 participants