Skip to content

Commit

Permalink
bug #30487 Fix Cache error while using anonymous class (Emmanuel BORGES)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

Fix Cache error while using anonymous class

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30395
| License       | MIT

Fix Cache error while using anonymous class

Commits
-------

036e722 Fix Cache error while using anonymous class
  • Loading branch information
nicolas-grekas committed Mar 15, 2019
2 parents 357fe5d + 036e722 commit 613bc42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php
Expand Up @@ -70,6 +70,11 @@ public function write(ClassMetadata $metadata)
*/
private function escapeClassName($class)
{
if (false !== strpos($class, '@')) {
// anonymous class: replace all PSR6-reserved characters
return str_replace(["\0", '\\', '/', '@', ':', '{', '}', '(', ')'], '.', $class);
}

return str_replace('\\', '.', $class);
}
}
Expand Up @@ -23,4 +23,12 @@ public function testNameCollision()
$this->cache->write($metadata);
$this->assertFalse($this->cache->has('Foo_Bar'));
}

public function testNameWithInvalidChars()
{
$metadata = new ClassMetadata('class@anonymous/path/file');

$this->cache->write($metadata);
$this->assertTrue($this->cache->has('class@anonymous/path/file'));
}
}

2 comments on commit 613bc42

@zim32
Copy link

@zim32 zim32 commented on 613bc42 Mar 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I,m just wondering if we have multiple anonymous classes inside one file will it work? Or it will overwrite data?

@nicolas-grekas
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will work

Please sign in to comment.