Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PropertyAccess] Reset object property to either null or uninitialized state #44880

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

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
Loading
from

Conversation

filiplikavcan
Copy link
Contributor

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets #44686
License MIT
Doc PR

Description

I propose extending Property Accessor with an optional feature that could enable checking whether object setter has a non-nullable parameter and would call unset($object->parameter) instead of $object->setParameter($value) if $value is null and $object implements __unset() method.

Motivation

Form component could use this feature and reset non-nullable properties to uninitialized state when null value is submitted for a non-nullable property. Currently when a NotNull constraint is set on a property it is only evaluated for a new object (with uninitialized properties). Once the value for the property is set (for example through Doctrine hydration) and a user submits an empty/null value the constraint is never evaluated because execution fails on Property Accessor trying to call setter with non-nullable parameter.

Example

class User
{
    private string $name;

    public function setName(string $name)
    {
        $this->name = $name;
    }

    public function __unset($property)
    {
        unset($this->$property);
    }
}

$user = new User;

$propertyAccessor->setValue($user, 'name', 'John'); // $user->setName('John') is called
$propertyAccessor->setValue($user, 'name', null); // Fatal error: User::setName(): Argument #1 ($name) must be of type string, null given

$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
    ->enableMagicUnset()
    ->getPropertyAccessor();

$propertyAccessor->setValue($user, 'name', null); // unset($user->name) is called

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@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

@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

@carsonbot carsonbot changed the title Reset object property to either null or uninitialized state [PropertyAccess] Reset object property to either null or uninitialized state Jan 4, 2022
@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
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.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.