Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Nov 9, 2022

@staabm
Copy link
Contributor Author

staabm commented Nov 9, 2022

just realized that this rule also missed a separate error case, which theAccessPropertiesRule contained in

if ($parentClassReflection->hasProperty($name)) {
if ($scope->canAccessProperty($parentClassReflection->getProperty($name, $scope))) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Access to private property $%s of parent class %s.',
$name,
$parentClassReflection->getDisplayName(),
))->build(),
];
}

added the missing check and a test

@staabm staabm marked this pull request as ready for review November 9, 2022 22:59
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -184,6 +185,26 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
return $messages;
}

if ($typeForDescribe instanceof TypeWithClassName && $typeForDescribe->getClassReflection() !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

  1. typeForDescribe is just for error message description
  2. instanceof *Type is always wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored it to make it more like

if (count($classNames) === 1) {
$referencedClass = $typeResult->getReferencedClasses()[0];
$propertyClassReflection = $this->reflectionProvider->getClass($referencedClass);
$parentClassReflection = $propertyClassReflection->getParentClass();
while ($parentClassReflection !== null) {
if ($parentClassReflection->hasProperty($name)) {
if ($scope->canAccessProperty($parentClassReflection->getProperty($name, $scope))) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Access to private property $%s of parent class %s.',
$name,
$parentClassReflection->getDisplayName(),
))->build(),
];
}
$parentClassReflection = $parentClassReflection->getParentClass();
}
}

foreach ($typeForDescribe->getClassReflection()->getParents() as $parentClassReflection) {
if (!$parentClassReflection->hasProperty($name)) {
continue;
$classNames = $classType->getReferencedClasses();
Copy link
Member

Choose a reason for hiding this comment

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

This is also wrong. getReferencedClasses will return nested classes too. Which means that if you get Foo<Bar>, getReferencedClasses will contain both Foo and Bar.

I think that in context of this PR, you need TypeUtils::getDirectClassNames. Feel free to fix the other PR too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

at this point I just did what you said. I don't know what I am doing here right now.

Feel free to fix the other PR too.

not sure which one you mean.

I will be in a meeting for the rest of the day. would be great in case you already know what needs to be done, if you could finish it.

thank you

Copy link
Member

Choose a reason for hiding this comment

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

You said: refactored it to make it more like

if (count($classNames) === 1) {
$referencedClass = $typeResult->getReferencedClasses()[0];
$propertyClassReflection = $this->reflectionProvider->getClass($referencedClass);
$parentClassReflection = $propertyClassReflection->getParentClass();
while ($parentClassReflection !== null) {
if ($parentClassReflection->hasProperty($name)) {
if ($scope->canAccessProperty($parentClassReflection->getProperty($name, $scope))) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Access to private property $%s of parent class %s.',
$name,
$parentClassReflection->getDisplayName(),
))->build(),
];
}
$parentClassReflection = $parentClassReflection->getParentClass();
}
}

But that other rule is wrong too, not sure for how long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok. I pushed a fix. hopefully thats what you had in mind.

thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that other rule is wrong too, not sure for how long.

btw: I found the commit staabm@455fb7b

@ondrejmirtes ondrejmirtes merged commit 173091d into phpstan:1.9.x Nov 18, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the static branch June 21, 2023 15:09
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.

False positive: Access to private property $root of parent class Foo.
3 participants