[Serializer] GetSetNormalizer shouldn't set/get static methods
This commit is contained in:
parent
e53b3b3a78
commit
d8d4405e50
@ -114,7 +114,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
||||
if ($allowed && !$ignored) {
|
||||
$setter = 'set'.ucfirst($attribute);
|
||||
|
||||
if (in_array($setter, $classMethods)) {
|
||||
if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
|
||||
$object->$setter($value);
|
||||
}
|
||||
}
|
||||
@ -170,10 +170,13 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
||||
{
|
||||
$methodLength = strlen($method->name);
|
||||
|
||||
return (
|
||||
return
|
||||
!$method->isStatic() &&
|
||||
(
|
||||
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
|
||||
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
|
||||
0 === $method->getNumberOfRequiredParameters()
|
||||
);
|
||||
)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
|
||||
public function testNoStaticGetSetSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
|
||||
}
|
||||
|
||||
public function testPrivateSetter()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
|
||||
@ -568,6 +581,7 @@ class GetSetDummy
|
||||
private $baz;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
private static $staticObject;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
@ -628,6 +642,16 @@ class GetSetDummy
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
|
||||
public static function getStaticObject()
|
||||
{
|
||||
return self::$staticObject;
|
||||
}
|
||||
|
||||
public static function setStaticObject($object)
|
||||
{
|
||||
self::$staticObject = $object;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user