Skip to content

[PropertyInfo] Support singular adder and remover #18337

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 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\PropertyInfo\Extractor;

use Symfony\Component\Inflector\Inflector;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
Expand Down Expand Up @@ -55,13 +56,17 @@ public function getProperties($class, array $context = array())
return;
}

$reflectionProperties = $reflectionClass->getProperties();

$properties = array();
foreach ($reflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
$properties[$reflectionProperty->name] = true;
foreach ($reflectionProperties as $reflectionProperty) {
if ($reflectionProperty->isPublic()) {
$properties[$reflectionProperty->name] = true;
}
}

foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$propertyName = $this->getPropertyName($reflectionMethod->name);
$propertyName = $this->getPropertyName($reflectionMethod->name, $reflectionProperties);
if (!$propertyName || isset($properties[$propertyName])) {
continue;
}
Expand Down Expand Up @@ -312,33 +317,54 @@ private function getAccessorMethod($class, $property)
private function getMutatorMethod($class, $property)
{
$ucProperty = ucfirst($property);
$ucSingulars = (array) Inflector::singularize($ucProperty);

foreach (self::$mutatorPrefixes as $prefix) {
try {
$reflectionMethod = new \ReflectionMethod($class, $prefix.$ucProperty);
$names = array($ucProperty);
if (in_array($prefix, self::$arrayMutatorPrefixes)) {
$names = array_merge($names, $ucSingulars);
}

// Parameter can be optional to allow things like: method(array $foo = null)
if ($reflectionMethod->getNumberOfParameters() >= 1) {
return array($reflectionMethod, $prefix);
foreach ($names as $name) {
try {
$reflectionMethod = new \ReflectionMethod($class, $prefix.$name);

// Parameter can be optional to allow things like: method(array $foo = null)
if ($reflectionMethod->getNumberOfParameters() >= 1) {
return array($reflectionMethod, $prefix);
}
} catch (\ReflectionException $reflectionException) {
// Try the next one if method does not exist
}
} catch (\ReflectionException $reflectionException) {
// Try the next prefix if the method doesn't exist
}
}
}

/**
* Extracts a property name from a method name.
*
* @param string $methodName
* @param string $methodName
* @param \ReflectionProperty[] $reflectionProperties
*
* @return string
*/
private function getPropertyName($methodName)
private function getPropertyName($methodName, array $reflectionProperties)
{
$pattern = implode('|', array_merge(self::$accessorPrefixes, self::$mutatorPrefixes));

if (preg_match('/^('.$pattern.')(.+)$/i', $methodName, $matches)) {
if (!in_array($matches[1], self::$arrayMutatorPrefixes)) {
return $matches[2];
}

foreach ($reflectionProperties as $reflectionProperty) {
foreach ((array) Inflector::singularize($reflectionProperty->name) as $name) {
if (strtolower($name) === strtolower($matches[2])) {
return $reflectionProperty->name;
}
}
}

return $matches[2];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\PropertyInfo\Tests\Extractor;

use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
use Symfony\Component\PropertyInfo\Type;

/**
Expand Down Expand Up @@ -119,4 +120,11 @@ public function testIsWritable()
$this->assertTrue($this->extractor->isWritable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', 'e', array()));
$this->assertTrue($this->extractor->isWritable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', 'f', array()));
}

public function testSingularize()
{
$this->assertTrue($this->extractor->isWritable(AdderRemoverDummy::class, 'analyses'));
$this->assertTrue($this->extractor->isWritable(AdderRemoverDummy::class, 'feet'));
$this->assertEquals(array('analyses', 'feet'), $this->extractor->getProperties(AdderRemoverDummy::class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class AdderRemoverDummy
{
private $analyses;
private $feet;

public function addAnalyse(Dummy $analyse)
{
}

public function removeFoot(Dummy $foot)
{
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/PropertyInfo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=5.5.9",
"symfony/inflector": "~3.1"
},
"require-dev": {
"symfony/serializer": "~2.8|~3.0",
Expand Down