Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyAccess] Add nullsafe operator support #34483

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
ogizanagi opened this issue Nov 21, 2019 · 5 comments · Fixed by #47511
Closed

[PropertyAccess] Add nullsafe operator support #34483

ogizanagi opened this issue Nov 21, 2019 · 5 comments · Fixed by #47511

Comments

@ogizanagi
Copy link
Contributor

ogizanagi commented Nov 21, 2019

Description

Given an object graph foo.bar.baz, where bar is optional (can be null),
trying to call the property accessor with foo.bar.baz will throw a:

UnexpectedTypeException: PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "NULL" while trying to traverse path "foo.bar.baz" at property "baz".

In some use-cases, it'll be very handy to get null instead of this exception (e.g: in custom validation constraints using a property path set by the developper in annotations).

The flag introduced in #30545 allows to ignore exception on invalid property paths, but it works differently and has some drawbacks:

  • paths like foo.bak (non-existing property) will be considered as readable and return null. But foo.bar.baz would still throw the same exception.
  • it'll indeed hide potential mistakes in the property path. Which I'd like to avoid for this feature (bar must exist, but can be null).

Then I was about to suggest yet another flag for the PropertyAccessor ($throwOnNullValueInGraph?), but instead, I'm wondering if we shouldn't allow something mimicking the PHP nullsafe_calls RFC 👇

Example

Given an object graph foo.bar.baz, where bar is optional (can be null):

$propertyAccessor = PropertyAccess::createPropertyAccessor();

// Where class Foo has a `bar` property, and class Bar has a `baz` property, for which values are set by the constructors:
$full = new \stdClass();
$full->foo = new Foo(new Bar(new Baz()));

$propertyAccessor->getValue($full, 'foo.bar?.baz') // Would return the Baz instance

$noBar = new \stdClass();
$noBar->foo = new Foo(null);

$propertyAccessor->getValue($noBar, 'foo.bar?.baz') // Would return null

$noBak = new \stdClass();
$noBak->foo = new Foo(new Bar(new Baz()));

$propertyAccessor->getValue($noBak, 'foo.bak?.baz') // Would still throw a NoSuchPropertyException, as property `bak` does not exist in class `Foo`

This wouldn't change anything for writing, only for reading.

@ogizanagi ogizanagi changed the title [PropertyAccessor] Allow to return null for optional paths in the graph [PropertyAccess] Allow to return null for optional paths in the graph Nov 21, 2019
@oliverde8
Copy link

I like the idea, started to work on something, probably won't have time to spend more time on it before Sunday.

@vodkhard
Copy link

non news on this feature ?

@stof
Copy link
Member

stof commented Nov 23, 2021

Well, nobody finished the work of implementing it

@ogizanagi ogizanagi reopened this Nov 23, 2021
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@ogizanagi
Copy link
Contributor Author

ogizanagi commented May 24, 2022

I'd still like to see this happen, but someone needs to take over #34497 to introduce the nullsafe operator support in the PropertyAccess component.

@carsonbot carsonbot removed the Stalled label May 24, 2022
@ogizanagi ogizanagi changed the title [PropertyAccess] Allow to return null for optional paths in the graph [PropertyAccess] Add nullsafe operator support May 24, 2022
fsoedjede added a commit to fsoedjede/symfony that referenced this issue Sep 7, 2022
fsoedjede added a commit to fsoedjede/symfony that referenced this issue Sep 7, 2022
fsoedjede added a commit to fsoedjede/symfony that referenced this issue Sep 7, 2022
fsoedjede added a commit to fsoedjede/symfony that referenced this issue Sep 7, 2022
fsoedjede added a commit to fsoedjede/symfony that referenced this issue Sep 22, 2022
@fabpot fabpot closed this as completed Oct 1, 2022
fabpot added a commit that referenced this issue Oct 1, 2022
…s (fsoedjede)

This PR was merged into the 6.2 branch.

Discussion
----------

[Form][PropertyAccess] Allow optional property accesses

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #34483 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#17288 <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

This PR is basically a copy of #34497 with some minor changes.

I needed it and I have some time now to help advance in implementing it.

**TODO**

- [x] Add nullsafe operator usage
- [x] Update unit test
- [x] Update documentation

Regards

Commits
-------

6455feb feature #34483 - Allow optional property accesses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants