[Serializer] GetSetNormalizer shouldn't set/get static methods

This commit is contained in:
Warnar Boekkooi 2015-11-04 15:08:11 +08:00
parent e53b3b3a78
commit d8d4405e50
2 changed files with 48 additions and 6 deletions

View File

@ -114,7 +114,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
if ($allowed && !$ignored) { if ($allowed && !$ignored) {
$setter = 'set'.ucfirst($attribute); $setter = 'set'.ucfirst($attribute);
if (in_array($setter, $classMethods)) { if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
$object->$setter($value); $object->$setter($value);
} }
} }
@ -170,10 +170,13 @@ class GetSetMethodNormalizer extends AbstractNormalizer
{ {
$methodLength = strlen($method->name); $methodLength = strlen($method->name);
return ( return
((0 === strpos($method->name, 'get') && 3 < $methodLength) || !$method->isStatic() &&
(0 === strpos($method->name, 'is') && 2 < $methodLength)) && (
0 === $method->getNumberOfRequiredParameters() ((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
); (0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
0 === $method->getNumberOfRequiredParameters()
)
;
} }
} }

View File

@ -549,11 +549,24 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testDenormalizeShouldNotSetStaticAttribute()
{
$obj = $this->normalizer->denormalize(array('staticObject' => true), __NAMESPACE__.'\GetSetDummy');
$this->assertEquals(new GetSetDummy(), $obj);
$this->assertNull(GetSetDummy::getStaticObject());
}
public function testNoTraversableSupport() public function testNoTraversableSupport()
{ {
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject())); $this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
} }
public function testNoStaticGetSetSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
}
public function testPrivateSetter() public function testPrivateSetter()
{ {
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy'); $obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
@ -568,6 +581,7 @@ class GetSetDummy
private $baz; private $baz;
protected $camelCase; protected $camelCase;
protected $object; protected $object;
private static $staticObject;
public function getFoo() public function getFoo()
{ {
@ -628,6 +642,16 @@ class GetSetDummy
{ {
return $this->object; return $this->object;
} }
public static function getStaticObject()
{
return self::$staticObject;
}
public static function setStaticObject($object)
{
self::$staticObject = $object;
}
} }
class GetConstructorDummy class GetConstructorDummy
@ -799,3 +823,18 @@ class ObjectWithPrivateSetterDummy
{ {
} }
} }
class ObjectWithJustStaticSetterDummy
{
private static $foo = 'bar';
public static function getFoo()
{
return self::$foo;
}
public static function setFoo($foo)
{
self::$foo = $foo;
}
}