Skip to content
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

[PropertyInfo] Support singular adder and remover #18337

Closed
wants to merge 7 commits into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Mar 28, 2016

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

Fix #18166 with only a soft dependency to the PropertyAccess component. If #18260 is accepted, this PR can be reworked to use this new component, in the meantime it fixes the problem with a soft dependency.

I don't now if it should be considered as a bug fix (and then merged in 2.8) or as a new feature. It's technically a bug fix but I'm not sure that introducing a new soft dependency is acceptable if older branches. What do you think @symfony/deciders?

@dunglas
Copy link
Member Author

dunglas commented Mar 28, 2016

AppVeyor errors not related.

return array($reflectionMethod, $prefix);
if (null !== $singulars && in_array($prefix, self::$arrayMutatorPrefixes)) {
$names = $singulars;
$names[] = $ucProperty;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the plural version should have the highest precedence.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer to keep it as is: if someone rely on the old behavior (say it has both methods, currently the singular is used), it should not change or it will be a BC break.

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 see the plural version of the adder/remover being checked at all in PropertyAccessor. Did I miss anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not supported by the PropertyAccess component but it is supported by the PropertyInfo component. Changing this behavior would be a BC break.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I get what you mean. The PropertyInfo component currently only supports the plural version. How would it be a BC break if we give the highest precedence to the plural version?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry I just got it...

Copy link
Contributor

Choose a reason for hiding this comment

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

As in:

$names = $singulars;
array_unshift($names, $ucProperty);

or its equivalent.

@xabbuh
Copy link
Member

xabbuh commented Apr 5, 2016

@dunglas The Inflector component was merged.

@stof
Copy link
Member

stof commented Apr 7, 2016

@dunglas please update your PR.

@dunglas
Copy link
Member Author

dunglas commented Apr 18, 2016

Inflector component integrated and @teohhanhui comments fixed.

Status: needs review

try {
$reflectionMethod = new \ReflectionMethod($class, $prefix.$ucProperty);
if (in_array($prefix, self::$arrayMutatorPrefixes)) {
array_unshift($names, $ucProperty);
Copy link
Contributor

Choose a reason for hiding this comment

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

This would keep on prepending...

Copy link
Contributor

Choose a reason for hiding this comment

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

What about:

$ucProperty = ucfirst($property);
$ucSingulars = (array) Inflector::singularize($ucProperty);

foreach (self::$mutatorPrefixes as $prefix) {
    $names = array($ucProperty);
    if (in_array($prefix, self::$arrayMutatorPrefixes)) {
        $names = array_merge($names, $ucSingulars);
    }

    ...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Perfect.

@dunglas
Copy link
Member Author

dunglas commented May 2, 2016

Changes from @teohhanhui are merged. This is ready to be merged.


foreach ($reflectionProperties as $reflectionProperty) {
foreach ((array) Inflector::singularize($reflectionProperty->name) as $name) {
if ($name === lcfirst($matches[2])) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we use strtolower() when comparing as method names are case-insensitive (like is the regular expression used at the beginning of this method)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@dunglas
Copy link
Member Author

dunglas commented May 4, 2016

Travis errors not related.

return array($reflectionMethod, $prefix);
}
} catch (\ReflectionException $reflectionException) {
// Try the next prefix if the method doesn't exist
Copy link
Member

@xabbuh xabbuh May 5, 2016

Choose a reason for hiding this comment

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

This is now not true anymore. We try the next name (or next prefix) instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

// Try the next one if method does not exist

@xabbuh
Copy link
Member

xabbuh commented May 5, 2016

👍

@weaverryan
Copy link
Member

👍

@dunglas
Copy link
Member Author

dunglas commented May 24, 2016

@fabpot ok to merge this one for the rc1?

@dunglas
Copy link
Member Author

dunglas commented Jun 3, 2016

@symfony/deciders, unfortunately this PR hasn't been merged in 3.1.
It's a blocker for some projects (including API Platform v2). What do you think of backporting it in Symfony 2.8 as a bug fix?

@teohhanhui
Copy link
Contributor

teohhanhui commented Jun 3, 2016

@dunglas But the Inflector component was only introduced in 3.1, so perhaps this can be a bugfix in 3.1?

@dunglas
Copy link
Member Author

dunglas commented Jun 3, 2016

We can add PropertyAccess as a soft dependency for the >3.1.

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.

[PropertyInfo] Singularize array mutator in ReflectionExtractor
8 participants