Skip to content

[PropertyInfo] Singularize array mutator in ReflectionExtractor #18166

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
teohhanhui opened this issue Mar 14, 2016 · 12 comments · Fixed by #18260
Closed

[PropertyInfo] Singularize array mutator in ReflectionExtractor #18166

teohhanhui opened this issue Mar 14, 2016 · 12 comments · Fixed by #18260

Comments

@teohhanhui
Copy link
Contributor

ReflectionExtractor currently supports array mutators (add, remove) but it's not very useful as the singular method name is not being checked.

If we can use doctrine/inflector to singularize the property name, we'll be able to pick up a lot more useful information in many cases. I'll open a PR if this is desirable (looking at the composer.json of Symfony components, I understand that it has to be an optional dependency).

@teohhanhui
Copy link
Contributor Author

@dunglas

@dunglas
Copy link
Member

dunglas commented Mar 15, 2016

Looks like a bug to me. Maybe can we use our own inflector: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/PropertyAccess/StringUtil.php#L147

@teohhanhui
Copy link
Contributor Author

symfony/property-info does not require symfony/property-access. So you mean we should suggest symfony/property-access just to use its StringUtil?

I suspect doctrine/inflection would be superior (and more specific to the job)...

@teohhanhui
Copy link
Contributor Author

Of course, the alternative would be just to copy the StringUtil::singularify from PropertyAccess component, and add a note to keep them in sync (do we do that?).

@dunglas
Copy link
Member

dunglas commented Mar 15, 2016

PropertyInfo and PropertyAccess components are often used together. They must have exactly the same behavior regarding singulars, so they must rely on the same inflection backend.

I'm not a fond of copy/pasting the class. Maybe can we extract the StringUtil class to its own tiny component? /cc @fabpot @webmozart

@teohhanhui
Copy link
Contributor Author

I've just taken another look at this. Maybe we could just use PropertyAccessorInterface::isReadable / isWritable?

A property access extractor (Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface) that uses the PropertyAccess component seems only logical.

@teohhanhui
Copy link
Contributor Author

@dunglas I noticed that the return value signature for Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface::isReadable / isWritable is bool|null. When should a null value be returned?

@teohhanhui
Copy link
Contributor Author

To answer my own question:

Maybe we could just use PropertyAccessorInterface::isReadable / isWritable

No. Because that operates at the instance level.

@webmozart
Copy link
Contributor

PropertyInfo and PropertyAccess components are often used together.

@dunglas Does that indiciate that they should probably be in the same package? What about moving the new code from PropertyInfo to the older PropertyAccess component?

@dunglas
Copy link
Member

dunglas commented Mar 30, 2016

@webmozart I've no strong opinion about that. Both components are often used together but they are also commonly used separately. They don't solve the exact same problem.

For instance https://github.com/dunglas/php-to-json-schema uses only PropertyInfo and symfony/form (and many other components) only uses PropertyAccess.

I usually prefer a set of small decoupled components able to work together than a big monolith.

What would be the benefit to merge both components?

For this specific issue, having a new component (like proposed in #18260) or relying on a third party library (like Doctrine Inflector) looks like a better solution: pluralizing and singularizing words is something very useful and common (e.g. API Platform use Doctrine Inflector for a lot of things, including routing) and isn't related at all with properties.

fabpot added a commit that referenced this issue Mar 31, 2016
…cess) (teohhanhui)

This PR was merged into the 3.1-dev branch.

Discussion
----------

Add Inflector component (from StringUtil of PropertyAccess)

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

As proposed by @dunglas here: #18166 (comment)

This will help us fix #18166

Commits
-------

8abebf6 Add Inflector component (from StringUtil of PropertyAccess)
@teohhanhui
Copy link
Contributor Author

@fabpot This was accidentally closed from the commit message.

@fabpot fabpot reopened this Mar 31, 2016
@teohhanhui
Copy link
Contributor Author

@webmozart @dunglas

I think it'd be preferrable to use the PropertyAccess component for checking property access (duh...), but currently it only works with an instance. The same functionality is duplicated in PropertyInfo's PropertyAccessExtractorInterface.

But I do not agree that PropertyInfo be merged into PropertyAccess, because its purpose is to provide other types of metadata about the property (e.g. Doctrine, PHPDoc). These features would not be a good fit in PropertyAccess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants