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

Skip to content

Commit 88cbcf9

Browse files
committed
bug #39244 [String] Fix Notice when argument is empty string (moldman)
This PR was merged into the 5.1 branch. Discussion ---------- [String] Fix Notice when argument is empty string | Q | A | ------------- | --- | Branch? | 5.1 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT PHP Notice is generated when we pass empty string to `singularize` or `pluralize` method. ``` $inflector = new \Symfony\Component\String\Inflector\EnglishInflector(); $inflector->singularize(''); ``` ``` Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 PHP Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 ``` ``` $inflector = new \Symfony\Component\String\Inflector\EnglishInflector(); $inflector->pluralize(''); ``` ``` Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 424 PHP Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 424 ``` **Background**: When `\Symfony\Component\PropertyAccess\PropertyAccessorInterface::setValue` is used with `_` property, then it calls \Symfony\Component\String\Inflector\EnglishInflector::singularize with empty string. ``` class Check { public $_; } $check = new Check(); $pr = PropertyAccess::createPropertyAccessorBuilder() ->getPropertyAccessor(); if($pr->isWritable($check, '_')){ $pr->setValue($check, '_', 'test'); } var_dump($check); ``` ``` Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 PHP Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 ... Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 PHP Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 Notice: Uninitialized string offset: 0 in vendor/symfony/string/Inflector/EnglishInflector.php on line 344 object(Check)#6 (1) { ["_"]=> string(4) "test" } ``` P.S. Another solution is to include empty string in \Symfony\Component\String\Inflector\EnglishInflector::$uninflected ``` private static $uninflected = [ '', 'atad', 'reed', 'kcabdeef', 'hsif', 'ofni', 'esoom', 'seires', 'peehs', 'seiceps', ]; ``` If this PR is not relevant please close and sorry for inconvenience. Commits ------- 88c2b9b [String] Fix Notice when argument is empty string
2 parents f7c372e + 88c2b9b commit 88cbcf9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Symfony/Component/String/Inflector/EnglishInflector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ final class EnglishInflector implements InflectorInterface
305305
* A list of words which should not be inflected, reversed.
306306
*/
307307
private static $uninflected = [
308+
'',
308309
'atad',
309310
'reed',
310311
'kcabdeef',

src/Symfony/Component/String/Tests/EnglishInflectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,16 @@ public function testPluralize(string $singular, $plural)
306306
{
307307
$this->assertSame(\is_array($plural) ? $plural : [$plural], (new EnglishInflector())->pluralize($singular));
308308
}
309+
310+
public function testPluralizeEmptyString()
311+
{
312+
$plural = (new EnglishInflector())->pluralize('');
313+
$this->assertSame([''], $plural);
314+
}
315+
316+
public function testSingularizeEmptyString()
317+
{
318+
$singular = (new EnglishInflector())->singularize('');
319+
$this->assertSame([''], $singular);
320+
}
309321
}

0 commit comments

Comments
 (0)