-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] Update List Information from ReflectionExtractor #16911
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] Update List Information from ReflectionExtractor #16911
Conversation
@@ -63,6 +63,9 @@ public function getProperties($class, array $context = array()) | |||
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { | |||
$propertyName = $this->getPropertyName($reflectionMethod->name); | |||
if ($propertyName) { | |||
if (!isset($properties[$propertyName])) { | |||
$propertyName = lcfirst($propertyName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use this regex instead:
// if more than two uppercase characters don't lcfirst
return preg_match('/[A-Z]{2,}$/', $name) ? $name : lcfirst($name);
(First proposed by @soyuka in https://github.com/dunglas/DunglasApiBundle/pull/260/files)
It allows to handle properly methods like getIBAN()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that be /^[A-Z]{2,}/
to still lowercase the first letter of methods like getCarsIOwn()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really:
> /[A-Z]{2,}$/.test('SIRET')
true //don't lowercase first char
> /[A-Z]{2,}$/.test('Siret')
false //do lowercase
> /[A-Z]{2,}$/.test('SIret')
false //same
> /[A-Z]{2,}/.test('SIret')
true //with your regex it'd become SIret, but it should be sIret
Regex tests that the full property name is uppercased. If it's not, I think we should lower the first character.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So are we lowercasing the first letter unless it doesn't contain any lowercase letters at all? Example...
Input | Result |
---|---|
A |
a |
Aa |
aa |
AAa |
aAa |
AAA |
AAA |
I don't think it's a good solution, unless I misunderstood what you meant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I thought that it was:
Lowercase the first letter unless the string is an uppercased word (eg not only the first letter is uppercased).
For the above proposal, I was refering to getters/setters, for example:
Input | Result | Wrong |
---|---|---|
getSomeVar | someVar | ∅ |
getSIRET | SIRET | sIRET |
getIBAN | IBAN | iBAN |
getAnotherVariableWithUppercase | anotherVariableWithUppercase | ∅ |
That said, I might have misunderstood the goal of your PR.
Status: need work |
Unless a property with the same casing exists, lowercase the first letter of a name extracted from a method. Fixes #16889.
Status: Reviewed |
👍 |
Thanks for fixing this bug @zanderbaldwin. |
…actor (zanderbaldwin) This PR was squashed before being merged into the 2.8 branch (closes #16911). Discussion ---------- [PropertyInfo] Update List Information from ReflectionExtractor | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16889 | License | MIT | Doc PR | symfony/symfony-docs#5974 Unless a property with the same casing exists, lowercase the first letter of a property name extracted from a method. From what I understand, we don't actually need to support `snake_case` at all in the PropertyInfo component. I cannot think of any use-case where PropertyAccess would be used to get/set a property value *before* using PropertyInfo to find out information about the property and the values it supports. Since PropertyInfo supports the discovery of property names, which can then be used as identifiers in PropertyAccess, there should be no need to support a naming strategy to map property identifiers from one naming convention to another. --- Running `$reflectionExtractor->getProperties($class)` with the following classes: ```php class X { public $a; public $b; public function getA() {} } // Result: array('a', 'b'); ``` ```php class Y { public $A; public $b; public function setA() {} } // Result: array('A', 'b'); ``` ```php class Y { public $username; protected $emailAddress; public $password; public function getEmailAddress() {} public function isActive() {} } // Result: array('username', 'emailAddress', 'password', 'active'); ``` Commits ------- b2da76c [PropertyInfo] Update List Information from ReflectionExtractor
Unless a property with the same casing exists, lowercase the first letter of a property name extracted from a method. From what I understand, we don't actually need to support
snake_case
at all in the PropertyInfo component.I cannot think of any use-case where PropertyAccess would be used to get/set a property value before using PropertyInfo to find out information about the property and the values it supports.
Since PropertyInfo supports the discovery of property names, which can then be used as identifiers in PropertyAccess, there should be no need to support a naming strategy to map property identifiers from one naming convention to another.
Running
$reflectionExtractor->getProperties($class)
with the following classes: