Skip to content

Commit 1fab27b

Browse files
dunglasfabpot
authored andcommitted
[Serializer] ObjectNormalizer: don't serialize static methods and props
1 parent 0bd8b58 commit 1fab27b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function normalize($object, $format = null, array $context = array())
6868
$reflClass = new \ReflectionClass($object);
6969
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
7070
if (
71+
!$reflMethod->isStatic() &&
7172
!$reflMethod->isConstructor() &&
7273
!$reflMethod->isDestructor() &&
7374
0 === $reflMethod->getNumberOfRequiredParameters()
@@ -86,7 +87,9 @@ public function normalize($object, $format = null, array $context = array())
8687

8788
// properties
8889
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
89-
$attributes[$reflProperty->getName()] = true;
90+
if (!$reflProperty->isStatic()) {
91+
$attributes[$reflProperty->getName()] = true;
92+
}
9093
}
9194

9295
$attributes = array_keys($attributes);

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ public function testNoTraversableSupport()
456456
{
457457
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
458458
}
459+
460+
public function testNormalizeStatic()
461+
{
462+
$this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
463+
}
459464
}
460465

461466
class ObjectDummy
@@ -605,3 +610,14 @@ public function otherMethod()
605610
throw new \RuntimeException('Dummy::otherMethod() should not be called');
606611
}
607612
}
613+
614+
class ObjectWithStaticPropertiesAndMethods
615+
{
616+
public $foo = 'K';
617+
public static $bar = 'A';
618+
619+
public static function getBaz()
620+
{
621+
return 'L';
622+
}
623+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy