Skip to content

Commit e33beda

Browse files
committed
minor symfony#23595 [PropertyInfo] Use rawurlencode to escape PSR-6 keys (dunglas)
This PR was merged into the 3.2 branch. Discussion ---------- [PropertyInfo] Use rawurlencode to escape PSR-6 keys | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | no | New feature? |no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Use `rawurlenode` instead of my custom escaper as suggested by @nicolas-grekas: https://twitter.com/nicolasgrekas/status/887728384469590016 Commits ------- ab91659 [PropertyInfo] Use rawurlencode to escape PSR-6 keys
2 parents 4b4f831 + ab91659 commit e33beda

File tree

2 files changed

+2
-51
lines changed

2 files changed

+2
-51
lines changed

src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ private function extract($method, array $arguments)
106106
return call_user_func_array(array($this->propertyInfoExtractor, $method), $arguments);
107107
}
108108

109-
$key = $this->escape($method.'.'.$serializedArguments);
109+
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
110+
$key = rawurlencode($method.'.'.$serializedArguments);
110111

111112
if (array_key_exists($key, $this->arrayCache)) {
112113
return $this->arrayCache[$key];
@@ -124,29 +125,4 @@ private function extract($method, array $arguments)
124125

125126
return $this->arrayCache[$key] = $value;
126127
}
127-
128-
/**
129-
* Escapes a key according to PSR-6.
130-
*
131-
* Replaces characters forbidden by PSR-6 and the _ char by the _ char followed by the ASCII
132-
* code of the escaped char.
133-
*
134-
* @param string $key
135-
*
136-
* @return string
137-
*/
138-
private function escape($key)
139-
{
140-
return strtr($key, array(
141-
'{' => '_123',
142-
'}' => '_125',
143-
'(' => '_40',
144-
')' => '_41',
145-
'/' => '_47',
146-
'\\' => '_92',
147-
'@' => '_64',
148-
':' => '_58',
149-
'_' => '_95',
150-
));
151-
}
152128
}

src/Symfony/Component/PropertyInfo/Tests/PropertyInfoCacheExtractorTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,4 @@ public function testGetProperties()
6161
parent::testGetProperties();
6262
parent::testGetProperties();
6363
}
64-
65-
/**
66-
* @dataProvider escapeDataProvider
67-
*/
68-
public function testEscape($toEscape, $expected)
69-
{
70-
$reflectionMethod = new \ReflectionMethod($this->propertyInfo, 'escape');
71-
$reflectionMethod->setAccessible(true);
72-
73-
$this->assertSame($expected, $reflectionMethod->invoke($this->propertyInfo, $toEscape));
74-
}
75-
76-
public function escapeDataProvider()
77-
{
78-
return array(
79-
array('foo_bar', 'foo_95bar'),
80-
array('foo_95bar', 'foo_9595bar'),
81-
array('foo{bar}', 'foo_123bar_125'),
82-
array('foo(bar)', 'foo_40bar_41'),
83-
array('foo/bar', 'foo_47bar'),
84-
array('foo\bar', 'foo_92bar'),
85-
array('foo@bar', 'foo_64bar'),
86-
array('foo:bar', 'foo_58bar'),
87-
);
88-
}
8964
}

0 commit comments

Comments
 (0)