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

Skip to content

Commit 4cbb60c

Browse files
committed
[PropertyInfo] Support singular adder and remover
1 parent 3a165e5 commit 4cbb60c

4 files changed

Lines changed: 77 additions & 13 deletions

File tree

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\PropertyInfo\Extractor;
1313

14+
use Symfony\Component\Inflector\Inflector;
1415
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
1516
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
1617
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
@@ -55,13 +56,17 @@ public function getProperties($class, array $context = array())
5556
return;
5657
}
5758

59+
$reflectionProperties = $reflectionClass->getProperties();
60+
5861
$properties = array();
59-
foreach ($reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
60-
$properties[$reflectionProperty->name] = true;
62+
foreach ($reflectionProperties as $reflectionProperty) {
63+
if ($reflectionProperty->isPublic()) {
64+
$properties[$reflectionProperty->name] = true;
65+
}
6166
}
6267

6368
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
64-
$propertyName = $this->getPropertyName($reflectionMethod->name);
69+
$propertyName = $this->getPropertyName($reflectionMethod->name, $reflectionProperties);
6570
if (!$propertyName || isset($properties[$propertyName])) {
6671
continue;
6772
}
@@ -312,33 +317,54 @@ private function getAccessorMethod($class, $property)
312317
private function getMutatorMethod($class, $property)
313318
{
314319
$ucProperty = ucfirst($property);
320+
$ucSingulars = (array) Inflector::singularize($ucProperty);
315321

316322
foreach (self::$mutatorPrefixes as $prefix) {
317-
try {
318-
$reflectionMethod = new \ReflectionMethod($class, $prefix.$ucProperty);
323+
$names = array($ucProperty);
324+
if (in_array($prefix, self::$arrayMutatorPrefixes)) {
325+
$names = array_merge($names, $ucSingulars);
326+
}
319327

320-
// Parameter can be optional to allow things like: method(array $foo = null)
321-
if ($reflectionMethod->getNumberOfParameters() >= 1) {
322-
return array($reflectionMethod, $prefix);
328+
foreach ($names as $name) {
329+
try {
330+
$reflectionMethod = new \ReflectionMethod($class, $prefix.$name);
331+
332+
// Parameter can be optional to allow things like: method(array $foo = null)
333+
if ($reflectionMethod->getNumberOfParameters() >= 1) {
334+
return array($reflectionMethod, $prefix);
335+
}
336+
} catch (\ReflectionException $reflectionException) {
337+
// Try the next one if method does not exist
323338
}
324-
} catch (\ReflectionException $reflectionException) {
325-
// Try the next prefix if the method doesn't exist
326339
}
327340
}
328341
}
329342

330343
/**
331344
* Extracts a property name from a method name.
332345
*
333-
* @param string $methodName
346+
* @param string $methodName
347+
* @param \ReflectionProperty[] $reflectionProperties
334348
*
335349
* @return string
336350
*/
337-
private function getPropertyName($methodName)
351+
private function getPropertyName($methodName, array $reflectionProperties)
338352
{
339353
$pattern = implode('|', array_merge(self::$accessorPrefixes, self::$mutatorPrefixes));
340354

341355
if (preg_match('/^('.$pattern.')(.+)$/i', $methodName, $matches)) {
356+
if (!in_array($matches[1], self::$arrayMutatorPrefixes)) {
357+
return $matches[2];
358+
}
359+
360+
foreach ($reflectionProperties as $reflectionProperty) {
361+
foreach ((array) Inflector::singularize($reflectionProperty->name) as $name) {
362+
if (strtolower($name) === strtolower($matches[2])) {
363+
return $reflectionProperty->name;
364+
}
365+
}
366+
}
367+
342368
return $matches[2];
343369
}
344370
}

src/Symfony/Component/PropertyInfo/Tests/Extractors/ReflectionExtractorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\PropertyInfo\Tests\Extractor;
1313

1414
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
15+
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
1516
use Symfony\Component\PropertyInfo\Type;
1617

1718
/**
@@ -119,4 +120,11 @@ public function testIsWritable()
119120
$this->assertTrue($this->extractor->isWritable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', 'e', array()));
120121
$this->assertTrue($this->extractor->isWritable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', 'f', array()));
121122
}
123+
124+
public function testSingularize()
125+
{
126+
$this->assertTrue($this->extractor->isWritable(AdderRemoverDummy::class, 'analyses'));
127+
$this->assertTrue($this->extractor->isWritable(AdderRemoverDummy::class, 'feet'));
128+
$this->assertEquals(array('analyses', 'feet'), $this->extractor->getProperties(AdderRemoverDummy::class));
129+
}
122130
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
13+
14+
/**
15+
* @author Kévin Dunglas <[email protected]>
16+
*/
17+
class AdderRemoverDummy
18+
{
19+
private $analyses;
20+
private $feet;
21+
22+
public function addAnalyse(Dummy $analyse)
23+
{
24+
}
25+
26+
public function removeFoot(Dummy $foot)
27+
{
28+
}
29+
}

src/Symfony/Component/PropertyInfo/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
}
2424
],
2525
"require": {
26-
"php": ">=5.5.9"
26+
"php": ">=5.5.9",
27+
"symfony/inflector": "~3.1"
2728
},
2829
"require-dev": {
2930
"symfony/serializer": "~2.8|~3.0",

0 commit comments

Comments
 (0)