bug #16911 [PropertyInfo] Update List Information from ReflectionExtractor (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
This commit is contained in:
Kévin Dunglas 2015-12-14 22:30:57 +01:00
commit f50f92ab08
3 changed files with 27 additions and 8 deletions

View File

@ -62,9 +62,13 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$propertyName = $this->getPropertyName($reflectionMethod->name);
if ($propertyName) {
$properties[$propertyName] = true;
if (!$propertyName || isset($properties[$propertyName])) {
continue;
}
if (!preg_match('/^[A-Z]{2,}/', $propertyName)) {
$propertyName = lcfirst($propertyName);
}
$properties[$propertyName] = true;
}
return array_keys($properties);

View File

@ -36,18 +36,19 @@ class ReflectionExtractorTest extends \PHPUnit_Framework_TestCase
'bal',
'parent',
'collection',
'B',
'foo',
'foo2',
'foo3',
'foo4',
'foo5',
'files',
'A',
'B',
'C',
'D',
'E',
'F',
'a',
'DOB',
'c',
'd',
'e',
'f',
),
$this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
);

View File

@ -46,6 +46,11 @@ class Dummy extends ParentDummy
*/
public $collection;
/**
* @var ParentDummy
*/
public $B;
/**
* A.
*
@ -63,4 +68,13 @@ class Dummy extends ParentDummy
public function setB(ParentDummy $parent = null)
{
}
/**
* Date of Birth.
*
* @return \DateTime
*/
public function getDOB()
{
}
}