From 3fb77af6558202253dfc4abee9f05be48e95bf9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 14 Nov 2015 12:27:50 +0100 Subject: [PATCH 1/2] [Serializer] ObjectNormalizer: don't serialize static methods and props --- .../Serializer/Normalizer/ObjectNormalizer.php | 7 +++++-- .../Tests/Normalizer/ObjectNormalizerTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index ba84ac717f075..1e091c8b52ea5 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -66,8 +66,9 @@ public function normalize($object, $format = null, array $context = array()) // methods $reflClass = new \ReflectionClass($object); - foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) { + foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC ) as $reflMethod) { if ( + !$reflMethod->isStatic() && !$reflMethod->isConstructor() && !$reflMethod->isDestructor() && 0 === $reflMethod->getNumberOfRequiredParameters() @@ -86,7 +87,9 @@ public function normalize($object, $format = null, array $context = array()) // properties foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) { - $attributes[$reflProperty->getName()] = true; + if (!$reflProperty->isStatic()) { + $attributes[$reflProperty->getName()] = true; + } } $attributes = array_keys($attributes); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 1cadee52b196f..80a021d08fa25 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -456,6 +456,11 @@ public function testNoTraversableSupport() { $this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject())); } + + public function testNormalizeStatic() + { + $this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods())); + } } class ObjectDummy @@ -605,3 +610,14 @@ public function otherMethod() throw new \RuntimeException('Dummy::otherMethod() should not be called'); } } + +class ObjectWithStaticPropertiesAndMethods +{ + public $foo = 'K'; + public static $bar = 'A'; + + public static function getBaz() + { + return 'L'; + } +} From 4bf54d03864583039ccf31e0a097a436a98efa91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 14 Nov 2015 18:53:51 +0100 Subject: [PATCH 2/2] Fix CS --- .../Component/Serializer/Normalizer/ObjectNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index 1e091c8b52ea5..fe1676fbf36fb 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -66,7 +66,7 @@ public function normalize($object, $format = null, array $context = array()) // methods $reflClass = new \ReflectionClass($object); - foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC ) as $reflMethod) { + foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) { if ( !$reflMethod->isStatic() && !$reflMethod->isConstructor() &&