Skip to content

Commit aa94dd6

Browse files
committed
bug #23156 [PropertyAccess] Fix Usage with anonymous classes (mablae)
This PR was merged into the 3.2 branch. Discussion ---------- [PropertyAccess] Fix Usage with anonymous classes | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23136 | License | MIT Replace forbidden characters in the the class names of Anonymous Classes in form of "class@anonymous /symfony/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php0x7f3f5f267ad5" Wrapped in eval to avoid PHP parsing errors < 7 and using `rawurlenceode` for perf reasons Thanks @nicolas-grekas for the help and patience. Let me know if anything is missing. Commits ------- 3f7fd43 Fix Usage with anonymous classes
2 parents c0486f6 + 3f7fd43 commit aa94dd6

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ private function readProperty($zval, $property)
523523
*/
524524
private function getReadAccessInfo($class, $property)
525525
{
526-
$key = $class.'..'.$property;
526+
$key = (false !== strpos($class, '@') ? rawurlencode($class) : $class).'..'.$property;
527527

528528
if (isset($this->readPropertyCache[$key])) {
529529
return $this->readPropertyCache[$key];
@@ -702,7 +702,7 @@ private function writeCollection($zval, $property, $collection, $addMethod, $rem
702702
*/
703703
private function getWriteAccessInfo($class, $property, $value)
704704
{
705-
$key = $class.'..'.$property;
705+
$key = (false !== strpos($class, '@') ? rawurlencode($class) : $class).'..'.$property;
706706

707707
if (isset($this->writePropertyCache[$key])) {
708708
return $this->writePropertyCache[$key];

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,64 @@ public function testThrowTypeErrorWithInterface()
578578

579579
$this->propertyAccessor->setValue($object, 'countable', 'This is a string, \Countable expected.');
580580
}
581+
582+
/**
583+
* @requires PHP 7.0
584+
*/
585+
public function testAnonymousClassRead()
586+
{
587+
$value = 'bar';
588+
589+
$obj = $this->generateAnonymousClass($value);
590+
591+
$propertyAccessor = new PropertyAccessor(false, false, new ArrayAdapter());
592+
593+
$this->assertEquals($value, $propertyAccessor->getValue($obj, 'foo'));
594+
}
595+
596+
/**
597+
* @requires PHP 7.0
598+
*/
599+
public function testAnonymousClassWrite()
600+
{
601+
$value = 'bar';
602+
603+
$obj = $this->generateAnonymousClass('');
604+
605+
$propertyAccessor = new PropertyAccessor(false, false, new ArrayAdapter());
606+
$propertyAccessor->setValue($obj, 'foo', $value);
607+
608+
$this->assertEquals($value, $propertyAccessor->getValue($obj, 'foo'));
609+
}
610+
611+
private function generateAnonymousClass($value)
612+
{
613+
$obj = eval('return new class($value)
614+
{
615+
private $foo;
616+
617+
public function __construct($foo)
618+
{
619+
$this->foo = $foo;
620+
}
621+
622+
/**
623+
* @return mixed
624+
*/
625+
public function getFoo()
626+
{
627+
return $this->foo;
628+
}
629+
630+
/**
631+
* @param mixed $foo
632+
*/
633+
public function setFoo($foo)
634+
{
635+
$this->foo = $foo;
636+
}
637+
};');
638+
639+
return $obj;
640+
}
581641
}

0 commit comments

Comments
 (0)