bug #16546 [Serializer] ObjectNormalizer: don't serialize static methods and props (dunglas)
This PR was squashed before being merged into the 2.7 branch (closes #16546).
Discussion
----------
[Serializer] ObjectNormalizer: don't serialize static methods and props
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? |no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #16485
| License | MIT
| Doc PR | n/a
Commits
-------
1fab27b
[Serializer] ObjectNormalizer: don't serialize static methods and props
This commit is contained in:
commit
59f357d3c1
@ -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);
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user