This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Alexander M. Turek 88cbcf99c0 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
-------

88c2b9be62 [String] Fix Notice when argument is empty string
2020-12-01 18:07:21 +01:00
.github Display debug info 2020-11-20 23:06:42 +01:00
src/Symfony bug #39244 [String] Fix Notice when argument is empty string (moldman) 2020-12-01 18:07:21 +01:00
.appveyor.yml Merge branch '4.4' into 5.1 2020-11-02 16:45:32 +01:00
.editorconfig Update .editorconfig 2018-09-06 16:22:56 +02:00
.gitignore Run the phpunit-bridge from a PR 2019-08-02 17:46:19 +02:00
.php_cs.dist Merge branch '4.4' into 5.1 2020-10-28 22:31:18 +01:00
.travis.yml Merge branch '4.4' into 5.1 2020-11-16 16:58:32 +01:00
CHANGELOG-5.0.md Merge branch '5.0' into 5.1 2020-06-15 13:50:15 +02:00
CHANGELOG-5.1.md Update CHANGELOG for 5.1.9 2020-11-29 10:27:43 +01:00
CODE_OF_CONDUCT.md Added the Code of Conduct file 2018-10-10 03:13:30 -07:00
composer.json Merge branch '4.4' into 5.1 2020-10-28 22:31:18 +01:00
CONTRIBUTING.md Mention the community review guide 2016-12-18 22:02:35 +01:00
CONTRIBUTORS.md Update CONTRIBUTORS for 3.4.47 2020-11-27 09:41:54 +01:00
LICENSE Update year in license files 2020-01-01 12:03:25 +01:00
link Merge branch '3.4' into 4.4 2020-08-26 10:30:46 +02:00
phpunit Simplify phpunit script. 2020-10-28 21:38:43 +01:00
phpunit.xml.dist Merge branch '4.4' into 5.1 2020-11-16 16:58:32 +01:00
README.md Minor improvement 2020-07-29 07:57:47 +02:00
UPGRADE-5.0.md Typo: somes styles fixed 2020-08-06 09:00:23 +02:00
UPGRADE-5.1.md Update UPGRADE-5.1.md 2020-09-07 01:58:27 +02:00
UPGRADE-6.0.md Added deprecation for RememberMe services without logout() method 2020-05-16 13:05:23 +02:00

Symfony is a PHP framework for web and console applications and a set of reusable PHP components. Symfony is used by thousands of web applications (including BlaBlaCar.com and Spotify.com) and most of the popular PHP projects (including Drupal and Magento).

Installation

Documentation

Community

Contributing

Symfony is an Open Source, community-driven project with thousands of contributors. Join them contributing code or contributing documentation.

Security Issues

If you discover a security vulnerability within Symfony, please follow our disclosure procedure.

About Us

Symfony development is sponsored by SensioLabs, led by the Symfony Core Team and supported by Symfony contributors.