Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jun 14, 2017
2 parents c0486f6 + 3f7fd43 commit aa94dd6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Expand Up @@ -523,7 +523,7 @@ private function readProperty($zval, $property)
*/
private function getReadAccessInfo($class, $property)
{
$key = $class.'..'.$property;
$key = (false !== strpos($class, '@') ? rawurlencode($class) : $class).'..'.$property;

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

if (isset($this->writePropertyCache[$key])) {
return $this->writePropertyCache[$key];
Expand Down
Expand Up @@ -578,4 +578,64 @@ public function testThrowTypeErrorWithInterface()

$this->propertyAccessor->setValue($object, 'countable', 'This is a string, \Countable expected.');
}

/**
* @requires PHP 7.0
*/
public function testAnonymousClassRead()
{
$value = 'bar';

$obj = $this->generateAnonymousClass($value);

$propertyAccessor = new PropertyAccessor(false, false, new ArrayAdapter());

$this->assertEquals($value, $propertyAccessor->getValue($obj, 'foo'));
}

/**
* @requires PHP 7.0
*/
public function testAnonymousClassWrite()
{
$value = 'bar';

$obj = $this->generateAnonymousClass('');

$propertyAccessor = new PropertyAccessor(false, false, new ArrayAdapter());
$propertyAccessor->setValue($obj, 'foo', $value);

$this->assertEquals($value, $propertyAccessor->getValue($obj, 'foo'));
}

private function generateAnonymousClass($value)
{
$obj = eval('return new class($value)
{
private $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
/**
* @return mixed
*/
public function getFoo()
{
return $this->foo;
}
/**
* @param mixed $foo
*/
public function setFoo($foo)
{
$this->foo = $foo;
}
};');

return $obj;
}
}

0 comments on commit aa94dd6

Please sign in to comment.