Skip to content

Detect accessing static property as non static #3857

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

Conversation

jnoordsij
Copy link
Contributor

Closes phpstan/phpstan#12692

Thanks to the pointers provided on the issue, I've managed to copy over some bits from the 'reverse' check and adjust them accordingly:

if (!$property->isStatic()) {
$hasPropertyTypes = TypeUtils::getHasPropertyTypes($classType);
foreach ($hasPropertyTypes as $hasPropertyType) {
if ($hasPropertyType->getPropertyName() === $name) {
return [];
}
}
return array_merge($messages, [
RuleErrorBuilder::message(sprintf(
'Static access to instance property %s::$%s.',
$property->getDeclaringClass()->getDisplayName(),
$name,
))->identifier('property.staticAccess')->build(),
]);
}

This seems to work fine, but maybe there's still some configuration to be accounted for, given that while incorrect static access will result in a fatal error, incorrect non static access will only emit an info + warning message. As a consequence maybe this check should be considered less critical/important.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Please do not re-number test lines like that. Put the new test at the bottom of the file. Or even better - add a completely new test method with a completely new test file. It can be testBug12692 / bug-12692.php. Thanks

@jnoordsij jnoordsij force-pushed the detect-incorrect-non-static-access branch from a2a8633 to e4acdfa Compare March 9, 2025 17:18
@ondrejmirtes ondrejmirtes merged commit deb0911 into phpstan:2.1.x Mar 9, 2025
410 of 417 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@jnoordsij jnoordsij deleted the detect-incorrect-non-static-access branch March 9, 2025 20:31
@herndlm herndlm mentioned this pull request Mar 10, 2025
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.

Detect accessing static property as non static
2 participants