[Serializer] ObjectNormalizer: don't serialize static methods and props

This commit is contained in:
Kévin Dunglas 2015-11-14 12:27:50 +01:00 committed by Fabien Potencier
parent 0bd8b58d9b
commit 1fab27b58c
2 changed files with 20 additions and 1 deletions

View File

@ -68,6 +68,7 @@ class ObjectNormalizer extends AbstractNormalizer
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
!$reflMethod->isStatic() &&
!$reflMethod->isConstructor() &&
!$reflMethod->isDestructor() &&
0 === $reflMethod->getNumberOfRequiredParameters()
@ -86,7 +87,9 @@ class ObjectNormalizer extends AbstractNormalizer
// properties
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
$attributes[$reflProperty->getName()] = true;
if (!$reflProperty->isStatic()) {
$attributes[$reflProperty->getName()] = true;
}
}
$attributes = array_keys($attributes);

View File

@ -456,6 +456,11 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
{
$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 @@ class ObjectConstructorArgsWithDefaultValueDummy
throw new \RuntimeException('Dummy::otherMethod() should not be called');
}
}
class ObjectWithStaticPropertiesAndMethods
{
public $foo = 'K';
public static $bar = 'A';
public static function getBaz()
{
return 'L';
}
}