diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 44a71cf1b4..183417c254 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -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 ( - ((0 === strpos($method->name, 'get') && 3 < $methodLength) || - (0 === strpos($method->name, 'is') && 2 < $methodLength)) && - 0 === $method->getNumberOfRequiredParameters() - ); + return + !$method->isStatic() && + ( + ((0 === strpos($method->name, 'get') && 3 < $methodLength) || + (0 === strpos($method->name, 'is') && 2 < $methodLength)) && + 0 === $method->getNumberOfRequiredParameters() + ) + ; } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 3baf58703f..1d487c2fdd 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -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; + } +}