Skip to content

[PropertyAccess] Add missing arguments to PropertyAccess::createPropertyAccessor() #18977

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

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Jun 6, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR symfony/symfony-docs#6640

Actually, the recommended way to use the PropertyAccessor is to use PropertyAccess::createPropertyAccessor.

The problem is that using this way, we can't specify that invalid indexes should throw an exception, and so when calling PropertyAccessor::isReadable([], '[foo]') it returns always true.

It should be possible to make the exception thrown, plus the PropertyAccessor::$throwExceptionOnInvalidIndex already exists but is not used in PropertyAccess::createPropertyAccessor.

@chalasr chalasr changed the title Add argument to PropertyAccess::createPropertyAccessor [PropertyAccess] Add throwExceptionsOnInvalidIndex argument to PropertyAccess::createPropertyAccessor Jun 6, 2016
Remove extra arg on PropertyAccessBuilder
@chalasr chalasr force-pushed the propertyaccess_throwInvalidIndex_arg branch from 428b35a to e69c6bd Compare June 6, 2016 17:17
@GuilhemN
Copy link
Contributor

GuilhemN commented Jun 6, 2016

This can respect bc if you use func_num_args and func_get_arg. (as in this example)

@chalasr
Copy link
Member Author

chalasr commented Jun 6, 2016

Many thanks @Ener-Getick, I was not sure about if it could be envisaged.
If this is merged, should I reference somewhere that the argument should be really added to the method signature in 4.0?

EDIT
I just saw that you also modified the interface by adding a commented param.

@chalasr chalasr force-pushed the propertyaccess_throwInvalidIndex_arg branch from fb219d8 to c46124b Compare June 6, 2016 19:44
Document param (commented)

Fabbot fixes
@OskarStark
Copy link
Contributor

LGTM 👍

@chalasr chalasr force-pushed the propertyaccess_throwInvalidIndex_arg branch from 69cfb23 to 8eb11fc Compare June 6, 2016 21:58
@chalasr
Copy link
Member Author

chalasr commented Jun 6, 2016

Added doc PR and new tests.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jun 7, 2016

Why do we care about adding an optional argument to the PropertyAccess::createPropertyAccessor method ? The class is final and thus cannot be extended, nor is part of an interface, right ? It appears there is no BC break to me.

@GuilhemN
Copy link
Contributor

GuilhemN commented Jun 7, 2016

Indeed @ogizanagi i didn't realize that this class is final. So we don't need any bc layer.

@chalasr
Copy link
Member Author

chalasr commented Jun 7, 2016

Me too, will remove this hack. Thank's @ogizanagi

@fabpot
Copy link
Member

fabpot commented Jun 9, 2016

👍

@ogizanagi
Copy link
Contributor

What about adding the $magicCall argument, too ?

@chalasr chalasr changed the title [PropertyAccess] Add throwExceptionsOnInvalidIndex argument to PropertyAccess::createPropertyAccessor [PropertyAccess] Add missing arguments to PropertyAccess::createPropertyAccessor() Jun 9, 2016
@chalasr
Copy link
Member Author

chalasr commented Jun 9, 2016

Done @ogizanagi

@fabpot
Copy link
Member

fabpot commented Jun 9, 2016

Thank you @chalasr.

@fabpot fabpot closed this in 86eb7a3 Jun 9, 2016
@chalasr
Copy link
Member Author

chalasr commented Jun 9, 2016

@fabpot Out of curiosity, why did you create a merge commit that closed this rather than doing "Squash and merge" from here directly?

@chalasr chalasr deleted the propertyaccess_throwInvalidIndex_arg branch June 9, 2016 11:59
@OskarStark
Copy link
Contributor

OskarStark commented Jun 9, 2016

He is not doing it via the web interface. The symfony guys are using a proprietary CLI tool 😄

And we hope it will be open sourced sometime 👍

@chalasr
Copy link
Member Author

chalasr commented Jun 9, 2016

Unfortunately, best tools are (too) often hidden... ^^ Thank's for the explanation @OskarStark

@OskarStark
Copy link
Contributor

Unfortunately, best tools are (too) often hidden... ^^

you are right 😃

* @return PropertyAccessorBuilder The new property accessor builder
*/
public static function createPropertyAccessorBuilder()
public static function createPropertyAccessorBuilder($enableExceptionOnInvalidIndex = false, $enableMagicCall = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't agree with this change. A builder is exactly there to dynamically set the behavior and not have a huge list of arguments. If there are new flags for the property accessor, you don't want to add every new field as argument for the builder. Then there is no point of a builder.

@Tobion
Copy link
Contributor

Tobion commented Jun 12, 2016

I'm 👎 on this PR. It doesn't make sense and the use case it covers is already possible by using the builder. Also it does't expose all arguments as the cache argument is not exposed which makes it even inconsistent.

@chalasr
Copy link
Member Author

chalasr commented Jun 12, 2016

@Tobion the PropertyAccessBuilder::cacheItemPool is not available in 2.7.

IMHO the problem is that having two ways of getting a PropertyAccessor instance is a bit confusing.
We should be able to create a PropertyAccessor instance with all its arguments without need of switching between PropertyAccess::createPropertyAccessor() and PropertyAccessorBuilder::getPropertyAccessor().

What about making these two classes only one?

@Tobion
Copy link
Contributor

Tobion commented Jun 12, 2016

One is a builder and the other the actual accessor. They are meant to be used differently. This is exactly why this PR makes no sense.

the PropertyAccessBuilder::cacheItemPool is not available in 2.7.

What does this have to do with this PR?

@chalasr
Copy link
Member Author

chalasr commented Jun 12, 2016

One is a builder and the other the actual accessor. They are meant to be used differently.

One is the builder, but the other is not the accessor (PropertyAccess class), it just creates an instance of the builder that returns the accessor. This is the reason of this PR, the documented and most used way is to use PropertyAccess::createPropertyAccessor.
I see the PropertyAccess class as the right way to get a PropertyAccessor instance, as said in the PropertyAccessor itself:

/**
 * Should not be used by application code. Use
 * {@link PropertyAccess::createPropertyAccessor()} instead.
 *
 * @param bool $magicCall
 * @param bool $throwExceptionOnInvalidIndex
 */
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)

What is the point of having a PropertyAccess::createPropertyAccessor() if it can't use the PropertyAccessor arguments? We have two ways to get the same result, except that the first doesn't take benefit of the accessor constructor arguments and the second does.
I don't see the value of having the first before this PR, otherwise let's just use the builder directly and document it as the way to go.

@fabpot
Copy link
Member

fabpot commented Jun 21, 2016

I agree with @Tobion.

Reading the other PR adding the cache pool feels wrong.

My understanding is that the createPropertyAccessor() method creates a property accessor with good defaults. And if you need to tweak the config, you need to use the builder. Also, using the builder is better as instead of passing "random" booleans as argument, you call explicit method names. And when adding some more configuration, supporting it is just a matter of adding a method to the builder, no need to change the arguments of the PropertyAccess class.

So, I'm going to revert this PR instead of merging #19040.

fabpot added a commit that referenced this pull request Jun 21, 2016
…ertyAccess::createPropertyAccessor() (chalasr)"

This reverts commit 86eb7a3, reversing
changes made to 856c9f6.
@fabpot
Copy link
Member

fabpot commented Jun 21, 2016

reverted

@chalasr
Copy link
Member Author

chalasr commented Jun 21, 2016

In fact, this was firstly motivated by the default configuration provided in the PropertyAccess::createPropertyAccessor() method, disabling exceptions on invalid indexes and so making PropertyAccessor::isReadable() always returning true, even for undefined indexes.

IMHO the throwExceptionOnInvalidIndex argument should only make the getValue() method returning null in case of invalid indices if set to false and throw an exception if set to true, but it should never change the return of the isReadable() method because it makes it wrong.
See #18969 for an example.

Having faced this issue made me thinking too restrictively, I totally understand the reverting of this.

@fabpot fabpot mentioned this pull request Oct 27, 2016
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.

7 participants