Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 2 commits into from
Closed

[PropertyInfo] Update List Information from ReflectionExtractor #16911

wants to merge 2 commits into from

Conversation

zanbaldwin
Copy link
Member

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:

class X
{
    public $a;
    public $b;
    public function getA() {}
}
// Result: array('a', 'b');
class Y
{
    public $A;
    public $b;
    public function setA() {}
}
// Result: array('A', 'b');
class Y
{
    public $username;
    protected $emailAddress;
    public $password;
    public function getEmailAddress() {}
    public function isActive() {}
}
// Result: array('username', 'emailAddress', 'password', 'active');

@zanbaldwin zanbaldwin changed the title Feature/property info reflection list [PropertyInfo] Update List Information from ReflectionExtractor Dec 8, 2015
@@ -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);
Copy link
Member

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().

Copy link
Member Author

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()?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

@dunglas
Copy link
Member

dunglas commented Dec 9, 2015

Status: need work

Zander Baldwin added 2 commits December 9, 2015 10:50
Unless a property with the same casing exists, lowercase the first letter of a name extracted from a method. Fixes #16889.
@dunglas
Copy link
Member

dunglas commented Dec 10, 2015

Status: Reviewed

@dunglas
Copy link
Member

dunglas commented Dec 10, 2015

👍

@dunglas
Copy link
Member

dunglas commented Dec 14, 2015

Thanks for fixing this bug @zanderbaldwin.

dunglas added a commit that referenced this pull request Dec 14, 2015
…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
@dunglas dunglas closed this Dec 14, 2015
This was referenced Dec 26, 2015
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.

4 participants