Skip to content

Commit 5a5a08b

Browse files
committed
Fix Usage with anonymous classes
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
1 parent 40beab4 commit 5a5a08b

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,73 @@ public function testThrowTypeErrorWithInterface()
578578

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

0 commit comments

Comments
 (0)