Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae7cb05

Browse files
committed
getReflectionProperty refactor
1 parent 9786dd4 commit ae7cb05

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,15 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
163163
*/
164164
private function getReflectionProperty($classOrObject, $attribute)
165165
{
166-
$class = is_string($classOrObject) ? $classOrObject : get_class($classOrObject);
167-
168-
try {
169-
return new \ReflectionProperty($class, $attribute);
170-
} catch (\ReflectionException $e) {
171-
$class = new \ReflectionClass($class);
172-
173-
while ($parent = $class->getParentClass()) {
174-
try {
175-
return new \ReflectionProperty($parent->getName(), $attribute);
176-
} catch (\ReflectionException $e) {
177-
$class = $parent;
166+
$reflectionClass = new \ReflectionClass($classOrObject);
167+
while (true) {
168+
try {
169+
return $reflectionClass->getProperty($attribute);
170+
} catch (\ReflectionException $e) {
171+
if (!$reflectionClass = $reflectionClass->getParentClass()) {
172+
throw $e;
178173
}
179174
}
180-
181-
throw $e;
182175
}
183176
}
184177
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures;
4+
5+
class GroupDummyChild extends GroupDummy
6+
{
7+
private $baz;
8+
9+
/**
10+
* @return mixed
11+
*/
12+
public function getBaz()
13+
{
14+
return $this->baz;
15+
}
16+
17+
/**
18+
* @param mixed $baz
19+
*/
20+
public function setBaz($baz)
21+
{
22+
$this->baz = $baz;
23+
}
24+
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Serializer\Serializer;
2121
use Symfony\Component\Serializer\SerializerInterface;
2222
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
23+
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
2324
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2425
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
2526
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
@@ -67,28 +68,30 @@ public function testDenormalize()
6768

6869
public function testNormalizeWithParentClass()
6970
{
70-
$group = new GroupDummy();
71+
$group = new GroupDummyChild();
72+
$group->setBaz('baz');
7173
$group->setFoo('foo');
7274
$group->setBar('bar');
7375
$group->setKevin('Kevin');
7476
$group->setCoopTilleuls('coop');
7577
$this->assertEquals(
7678
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin',
77-
'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, ),
79+
'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz', ),
7880
$this->normalizer->normalize($group, 'any')
7981
);
8082
}
8183

8284
public function testDenormalizeWithParentClass()
8385
{
8486
$obj = $this->normalizer->denormalize(
85-
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin'),
86-
GroupDummy::class,
87+
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin', 'baz' => 'baz'),
88+
GroupDummyChild::class,
8789
'any'
8890
);
8991
$this->assertEquals('foo', $obj->getFoo());
9092
$this->assertEquals('bar', $obj->getBar());
9193
$this->assertEquals('Kevin', $obj->getKevin());
94+
$this->assertEquals('baz', $obj->getBaz());
9295
$this->assertNull($obj->getSymfony());
9396
}
9497

0 commit comments

Comments
 (0)