Skip to content

Commit ae1ecdc

Browse files
Merge branch '2.8'
* 2.8: [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/Form/Extension/Core/Type/ChoiceType.php src/Symfony/Component/Form/Extension/Core/Type/DateType.php src/Symfony/Component/Form/Extension/Core/Type/TimeType.php src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
2 parents 50416e6 + 8f4052a commit ae1ecdc

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
@@ -472,11 +472,24 @@ public function testDenormalizeNonExistingAttribute()
472472
);
473473
}
474474

475+
public function testDenormalizeShouldNotSetStaticAttribute()
476+
{
477+
$obj = $this->normalizer->denormalize(array('staticObject' => true), __NAMESPACE__.'\GetSetDummy');
478+
479+
$this->assertEquals(new GetSetDummy(), $obj);
480+
$this->assertNull(GetSetDummy::getStaticObject());
481+
}
482+
475483
public function testNoTraversableSupport()
476484
{
477485
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
478486
}
479487

488+
public function testNoStaticGetSetSupport()
489+
{
490+
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
491+
}
492+
480493
public function testPrivateSetter()
481494
{
482495
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
@@ -491,6 +504,7 @@ class GetSetDummy
491504
private $baz;
492505
protected $camelCase;
493506
protected $object;
507+
private static $staticObject;
494508

495509
public function getFoo()
496510
{
@@ -551,6 +565,16 @@ public function getObject()
551565
{
552566
return $this->object;
553567
}
568+
569+
public static function getStaticObject()
570+
{
571+
return self::$staticObject;
572+
}
573+
574+
public static function setStaticObject($object)
575+
{
576+
self::$staticObject = $object;
577+
}
554578
}
555579

556580
class GetConstructorDummy
@@ -722,3 +746,18 @@ private function setFoo($foo)
722746
{
723747
}
724748
}
749+
750+
class ObjectWithJustStaticSetterDummy
751+
{
752+
private static $foo = 'bar';
753+
754+
public static function getFoo()
755+
{
756+
return self::$foo;
757+
}
758+
759+
public static function setFoo($foo)
760+
{
761+
self::$foo = $foo;
762+
}
763+
}

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ public function testDenormalizeNonExistingAttribute()
338338
);
339339
}
340340

341+
public function testDenormalizeShouldIgnoreStaticProperty()
342+
{
343+
$obj = $this->normalizer->denormalize(array('outOfScope' => true), __NAMESPACE__.'\PropertyDummy');
344+
345+
$this->assertEquals(new PropertyDummy(), $obj);
346+
$this->assertEquals('out_of_scope', PropertyDummy::$outOfScope);
347+
}
348+
341349
/**
342350
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
343351
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
@@ -358,10 +366,16 @@ public function testNoTraversableSupport()
358366
{
359367
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
360368
}
369+
370+
public function testNoStaticPropertySupport()
371+
{
372+
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
373+
}
361374
}
362375

363376
class PropertyDummy
364377
{
378+
public static $outOfScope = 'out_of_scope';
365379
public $foo;
366380
private $bar;
367381
protected $camelCase;
@@ -420,3 +434,9 @@ public function __construct($kevinDunglas = null)
420434
$this->kevinDunglas = $kevinDunglas;
421435
}
422436
}
437+
438+
class StaticPropertyDummy
439+
{
440+
private static $property = 'value';
441+
}
442+

0 commit comments

Comments
 (0)