8000 bug #16454 [Serializer] GetSetNormalizer shouldn't set/get static met… · symfony/symfony@1bed177 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bed177

Browse files
committed
bug #16454 [Serializer] GetSetNormalizer shouldn't set/get static methods (boekkooi)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] GetSetNormalizer shouldn't set/get static methods | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | maybe | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR fixes setting static getters and setters when serializing. This is strictly speaking a BC break but in my opinion a needed one because the current behavior is unexpected. Commits ------- d8d4405 [Serializer] GetSetNormalizer shouldn't set/get static methods
2 parents e21bf56 + d8d4405 commit 1bed177

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
114114
if ($allowed && !$ignored) {
115115
$setter = 'set'.ucfirst($attribute);
116116

117-
if (in_array($setter, $classMethods)) {
117+
if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
118118
$object->$setter($value);
119119
}
120120
}
@@ -170,10 +170,13 @@ private function isGetMethod(\ReflectionMethod $method)
170170
{
171171
$methodLength = strlen($method->name);
172172

173-
return (
174-
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
175-
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
176-
0 === $method->getNumberOfRequiredParameters()
177-
);
173+
return
174+
!$method->isStatic() &&
175+
(
176+
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
177+
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
178+
0 === $method->getNumberOfRequiredParameters()
179+
)
180+
;
178181
}
179182
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,24 @@ public function testDenormalizeNonExistingAttribute()
549549
);
550550
}
551551

552+
public function testDenormalizeShouldNotSetStaticAttribute()
553+
{
554+
$obj = $this->normalizer->denormalize(array('staticObject' => true), __NAMESPACE__.'\GetSetDummy');
555+
556+
$this->assertEquals(new GetSetDummy(), $obj);
557+
$this->assertNull(GetSetDummy::getStaticObject());
558+
}
559+
552560
public function testNoTraversableSupport()
553561
{
554562
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
555563
}
556564

565+
public function testNoStaticGetSetSupport()
566+
{
567+
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
568+
}
569+
557570
public function testPrivateSetter()
558571
{
559572
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
@@ -568,6 +581,7 @@ class GetSetDummy
568581
private $baz;
569582
protected $camelCase;
570583
protected $object;
584+
private static $staticObject;
571585

572586
public function getFoo()
573587
{
@@ -628,6 +642,16 @@ public function getObject()
628642
{
629643
return $this->object;
630644
}
645+
646+
public static function getStaticObject()
647+
{
648+
return self::$staticObject;
649+
}
650+
651+
public static function setStaticObject($object)
652+
{
653+
self::$staticObject = $object;
654+
}
631655
}
632656

633657
class GetConstructorDummy
@@ -799,3 +823,18 @@ private function setFoo($foo)
799823
{
800824
}
801825
}
826+
827+
class ObjectWithJustStaticSetterDummy
828+
{
829+
private static $foo = 'bar';
830+
831+
public static function getFoo()
832+
{
833+
return self::$foo;
834+
}
835+
836+
public static function setFoo($foo)
837+
{
838+
self::$foo = $foo;
839+
}
840+
}

0 commit comments

Comments
 (0)
0