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

Skip to content

Commit 3f7fd43

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 3f7fd43

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)