Skip to content

Commit

Permalink
bug #10604 [PropertyAccessor] Wrong number of parameters (coma)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.5-dev branch.

Discussion
----------

[PropertyAccessor] Wrong number of parameters

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Since [6d2af21#diff-3e45cf556c0330623c0167841d303dff](6d2af21#diff-3e45cf556c0330623c0167841d303dff) the PropertyAccessor is failing on setters with default values because  [ReflectionFunctionAbstract::getNumberOfRequiredParameters](http://www.php.net/manual/es/reflectionfunctionabstract.getnumberofparameters.php) only count the parameters without default values.

Commits
-------

6157be0 Wrong number of parameters
  • Loading branch information
fabpot committed Apr 1, 2014
2 parents 8ced461 + 6157be0 commit 01f21e3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Expand Up @@ -570,7 +570,7 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
}

/**
* Returns whether a method is public and has a specific number of required parameters.
* Returns whether a method is public and has the number of required parameters.
*
* @param \ReflectionClass $class The class of the method
* @param string $methodName The method name
Expand All @@ -584,7 +584,9 @@ private function isMethodAccessible(\ReflectionClass $class, $methodName, $param
if ($class->hasMethod($methodName)) {
$method = $class->getMethod($methodName);

if ($method->isPublic() && $method->getNumberOfRequiredParameters() === $parameters) {
if ($method->isPublic()
&& $method->getNumberOfRequiredParameters() <= $parameters
&& $method->getNumberOfParameters() >= $parameters) {
return true;
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php
Expand Up @@ -18,13 +18,19 @@ class TestClass
private $privateProperty;

private $publicAccessor;
private $publicAccessorWithDefaultValue;
private $publicAccessorWithRequiredAndDefaultValue;
private $publicAccessorWithMoreRequiredParameters;
private $publicIsAccessor;
private $publicHasAccessor;

public function __construct($value)
{
$this->publicProperty = $value;
$this->publicAccessor = $value;
$this->publicAccessorWithDefaultValue = $value;
$this->publicAccessorWithRequiredAndDefaultValue = $value;
$this->publicAccessorWithMoreRequiredParameters = $value;
$this->publicIsAccessor = $value;
$this->publicHasAccessor = $value;
}
Expand All @@ -34,11 +40,41 @@ public function setPublicAccessor($value)
$this->publicAccessor = $value;
}

public function setPublicAccessorWithDefaultValue($value = null)
{
$this->publicAccessorWithDefaultValue = $value;
}

public function setPublicAccessorWithRequiredAndDefaultValue($value, $optional = null)
{
$this->publicAccessorWithRequiredAndDefaultValue = $value;
}

public function setPublicAccessorWithMoreRequiredParameters($value, $needed)
{
$this->publicAccessorWithMoreRequiredParameters = $value;
}

public function getPublicAccessor()
{
return $this->publicAccessor;
}

public function getPublicAccessorWithDefaultValue()
{
return $this->publicAccessorWithDefaultValue;
}

public function getPublicAccessorWithRequiredAndDefaultValue()
{
return $this->publicAccessorWithRequiredAndDefaultValue;
}

public function getPublicAccessorWithMoreRequiredParameters()
{
return $this->publicAccessorWithMoreRequiredParameters;
}

public function setPublicIsAccessor($value)
{
$this->publicIsAccessor = $value;
Expand Down
Expand Up @@ -43,6 +43,8 @@ public function getValidPropertyPaths()
// Accessor methods
array(new TestClass('Bernhard'), 'publicProperty', 'Bernhard'),
array(new TestClass('Bernhard'), 'publicAccessor', 'Bernhard'),
array(new TestClass('Bernhard'), 'publicAccessorWithDefaultValue', 'Bernhard'),
array(new TestClass('Bernhard'), 'publicAccessorWithRequiredAndDefaultValue', 'Bernhard'),
array(new TestClass('Bernhard'), 'publicIsAccessor', 'Bernhard'),
array(new TestClass('Bernhard'), 'publicHasAccessor', 'Bernhard'),

Expand Down Expand Up @@ -250,6 +252,14 @@ public function testSetValueUpdatesMagicSet()
$this->assertEquals('Updated', $author->__get('magicProperty'));
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
*/
public function testSetValueThrowsExceptionIfThereAreMissingParameters()
{
$this->propertyAccessor->setValue(new TestClass('Bernhard'), 'publicAccessorWithMoreRequiredParameters', 'Updated');
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
*/
Expand Down

0 comments on commit 01f21e3

Please sign in to comment.