Skip to content

[PropertyAccess] Fix Regression in PropertyAccessor::isWritable() #42672

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
Sep 10, 2021

Conversation

haase-fabian
Copy link
Contributor

Q A
Branch? 5.3
Bug fix? yes
New feature? no
Deprecations? no
License MIT
  1. is_object() check was moved to the isWritable() method.
  2. adds test to check for false as return value of isWritable method

Since commit 6f6c1a16 a regression exists where calling $propertyAccessor->isWritable() threw an TypeError when passing an array while using dot notation.

$propertyAccessor->isWritable(["name" => "Bernard"], "name");

TypeError: Argument 1 passed to Symfony\Component\PropertyAccess\PropertyAccessor::isPropertyWritable() must be an object, array given

@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 5.4 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 carsonbot added this to the 5.3 milestone Aug 21, 2021
@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

LGTM with a small comment. Thanks!

@@ -314,7 +314,7 @@ public function isWritable($objectOrArray, $propertyPath)
return false;
}
} else {
if (!$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))) {
if (!(\is_object($zval[self::VALUE]) && $this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i)))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest using

!\is_object($zval[self::VALUE]) || !$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))

that would make the extra parenthesis useless (helping readability IMHO), and prevent evaluating both conditions when the first is false (saving a method call).

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about this change? if the value is not an object, then we should return true isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed you're right, I should have read the code below. Then the whole could be simplified as:

if ($propertyPath->isIndex($i)) {
    return $zval[self::VALUE] instanceof \ArrayAccess || \is_array($zval[self::VALUE]);
 }

return !\is_object($zval[self::VALUE]) || $this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HeahDude I've implemented your first suggestion due to improved readability.
In both cases the second mthod call was ommitted when the first failed. (false && xyz()) never calls xzy().

For the second suggestion. I've tried it, but numerous tests start to fail. I think it has something to do with turning the for loop into one that doesn't actually loop.

@jderusse The behaviour prior to the referenced commit was to return false when an array got passed with a propertyPath which wasn't an index. Then isPropertyWritable would have returned false which eventually made isWritable return false.
As of the mentioned commit the type definition for the first parameter changed from a DocBlock to the method itself. Php now evaluates the type during runtime and therefore throwing an exception.

Scope of this PR is to restore the bahaviour from symfony 5.2.

@haase-fabian
Copy link
Contributor Author

@HeahDude Did I forget something to add to this PR for it to get merged? Do I need to request someone to review this?

Currently this bug blocks me from upgrading to 5.3.

@fabpot
Copy link
Member

fabpot commented Sep 10, 2021

Thank you @haase-fabian.

@fabpot fabpot merged commit 683c4f6 into symfony:5.3 Sep 10, 2021
@fabpot fabpot mentioned this pull request Sep 28, 2021
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