Skip to content

[PropertyAccess] Fix handling of uninitialized property of anonymous class #45002

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
merged 1 commit into from
Jan 12, 2022

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #44983
License MIT
Doc PR -

@nicolas-grekas
Copy link
Member Author

Thank you @filiplikavcan.

@nicolas-grekas nicolas-grekas merged commit 727842f into symfony:4.4 Jan 12, 2022
@filiplikavcan
Copy link
Contributor

This also affects 5.* and 6.* version. Will this PR be merged into those branches too?

@nicolas-grekas nicolas-grekas deleted the fix44983 branch January 14, 2022 09:38
@nicolas-grekas
Copy link
Member Author

It is already, isn't it?

@filiplikavcan
Copy link
Contributor

Ah, ok it is.

It is not merged in 6.1 because there are conflict changes I suppose. Should I create a new PR for that or it is up to maintainers to resolve conflicts and merge it to 6.1?

@nicolas-grekas
Copy link
Member Author

No need to, I'll handle the merge later on.

@@ -436,11 +436,11 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
}
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty($matches[1], $matches[2]);
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ('.preg_quote(get_debug_type($object), '/').')::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
Copy link
Contributor

@filiplikavcan filiplikavcan Jan 31, 2022

Choose a reason for hiding this comment

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

@nicolas-grekas

This change introduced a new bug:

class X { public int $a; }
class Y extends X {}
(new Y)->a;

This throws:

Typed property X::$a must not be accessed before initialization

but since get_debug_type(new Y) returns Y the error message does not match

Typed property Y::$a must not be accessed before initialization"

Here is a PR which fixes this: #45255

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

Successfully merging this pull request may close these issues.

3 participants