Skip to content

Commit

Permalink
[Serializer] ObjectNormalizer: don't serialize static methods and props
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored and fabpot committed Nov 17, 2015
1 parent 0bd8b58 commit 1fab27b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -68,6 +68,7 @@ public function normalize($object, $format = null, array $context = array())
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
!$reflMethod->isStatic() &&
!$reflMethod->isConstructor() &&
!$reflMethod->isDestructor() &&
0 === $reflMethod->getNumberOfRequiredParameters()
Expand All @@ -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);
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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';
}
}

0 comments on commit 1fab27b

Please sign in to comment.