Skip to content

Commit 8f4052a

Browse files
Merge branch '2.7' into 2.8
* 2.7: [ci] Phpunit tests wont run if composer is installed in a wrapper [ci] Add version tag in phpunit wrapper to trigger cache-reset on demand fix race condition at mkdir (#16258) [VarDumper] Fix PHP7 type-hints compat [Bridge] [PhpUnit] fixes documentation markup. [PropertyAccess] Port of the performance optimization from 2.3 trigger deprecation warning when using empty_value [PropertyAccess] Test access to dynamic properties [PropertyAccess] Fix dynamic property accessing. [Serializer] GetSetNormalizer shouldn't set/get static methods [Serializer] PropertyNormalizer shouldn't set static properties JsonDescriptor - encode container params only once Conflicts: src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
2 parents cc9b3f8 + c84e675 commit 8f4052a

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

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
}

Normalizer/PropertyNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function normalize($object, $format = null, array $context = array())
5050
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
5151

5252
foreach ($reflectionObject->getProperties() as $property) {
53-
if (in_array($property->name, $this->ignoredAttributes)) {
53+
if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
5454
continue;
5555
}
5656

@@ -110,6 +110,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
110110
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
111111
$property = $reflectionClass->getProperty($propertyName);
112112

113+
if ($property->isStatic()) {
114+
continue;
115+
}
116+
113117
// Override visibility
114118
if (!$property->isPublic()) {
115119
$property->setAccessible(true);

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+
}

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ public function testDenormalizeNonExistingAttribute()
409409
);
410410
}
411411

412+
public function testDenormalizeShouldIgnoreStaticProperty()
413+
{
414+
$obj = $this->normalizer->denormalize(array('outOfScope' => true), __NAMESPACE__.'\PropertyDummy');
415+
416+
$this->assertEquals(new PropertyDummy(), $obj);
417+
$this->assertEquals('out_of_scope', PropertyDummy::$outOfScope);
418+
}
419+
412420
/**
413421
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
414422
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
@@ -429,10 +437,16 @@ public function testNoTraversableSupport()
429437
{
430438
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
431439
}
440+
441+
public function testNoStaticPropertySupport()
442+
{
443+
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
444+
}
432445
}
433446

434447
class PropertyDummy
435448
{
449+
public static $outOfScope = 'out_of_scope';
436450
public $foo;
437451
private $bar;
438452
protected $camelCase;
@@ -491,3 +505,9 @@ public function __construct($kevinDunglas = null)
491505
$this->kevinDunglas = $kevinDunglas;
492506
}
493507
}
508+
509+
class StaticPropertyDummy
510+
{
511+
private static $property = 'value';
512+
}
513+

0 commit comments

Comments
 (0)