Skip to content

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

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

Closed
wants to merge 2 commits into from

Conversation

filiplikavcan
Copy link
Contributor

@filiplikavcan filiplikavcan commented Jan 11, 2022

Q A
Branch? 4.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
License MIT
$object = new class() {
    public string $uninitialized;
};

$this->propertyAccessor->getValue($object, 'uninitialized');

Expected

Throws UninitializedPropertyException with message "The property "class@anonymous::$uninitialized" is not readable because it is typed "string". You should initialize it or declare a default value instead.".

Actual

Throws Error with message "Typed property class@anonymous::$publication must not be accessed before initialization".

Side efect

Form with data of an anonymous class instance cannot be created. This code throws "Typed property class@anonymous::$name must not be accessed before initialization":

// in controller

$data = new class() {
    public string $name;
};

$form = $this->createForm(SomeFormType::class, $data);

This works as expected:

// in controller

class SomeClass {
    public string $name;
};

$data = new SomeClass;

$form = $this->createForm(SomeFormType::class, $data);

No exception is thrown because PropertyPathAccessor catches UninitializedPropertyException and returns null instead.

@carsonbot
Copy link

It looks like you unchecked the "Allow edits from maintainer" box. That is fine, but please note that if you have multiple commits, you'll need to squash your commits into one before this can be merged. Or, you can check the "Allow edits from maintainers" box and the maintainer can squash for you.

Cheers!

Carsonbot

@filiplikavcan filiplikavcan changed the title [PropertyAccess] Fix handling of uninitialized property of anonymous … [PropertyAccess] Fix handling of uninitialized property of anonymous class Jan 11, 2022
@filiplikavcan
Copy link
Contributor Author

filiplikavcan commented Jan 11, 2022

Hm, the failing integration test does not seem to be related to the changes in this PR. Can somebody help me with that if it is related?

@carsonbot
Copy link

Hey!

I think @HeahDude has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas
Copy link
Member

Thanks for the PR. Continued in #44983.
Please allow edits from maintainers next time!

nicolas-grekas added a commit that referenced this pull request Jan 12, 2022
… anonymous class (filiplikavcan)

This PR was merged into the 4.4 branch.

Discussion
----------

[PropertyAccess] Fix handling of uninitialized property of anonymous class

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

Commits
-------

27d5edf [PropertyAccess] Fix handling of uninitialized property of anonymous class
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