Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] PHP 8.1 PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated #45179

Closed
BrokenSourceCode opened this issue Jan 25, 2022 · 3 comments

Comments

@BrokenSourceCode
Copy link
Contributor

BrokenSourceCode commented Jan 25, 2022

Symfony version(s) affected

6.0.2

Description

Hi,

With Symfony/Console 6.0.2 and PHP 8.1, there is a depreciation on the method Symfony\Component\Console\Question\ChoiceQuestion::getDefaultValidator() (click on the link to see the line of the issue) :

PHP Deprecated : trim() : Passing null to parameter #1 ($string) of type string is deprecated.

This happens when the default value for the question is null.

How to reproduce

// Method definition : https://github.com/symfony/console/blob/v6.0.2/Style/SymfonyStyle.php#L277
$io->choice('Select the queue to analyze', ['queue1', 'queue2', 'queue3']); // The 3rd argument must be `null`.

Possible Solution

We can simply cast the variable to string to fix the issue. Just like this at this line:

-$selectedChoices[$k] = trim($v);
+$selectedChoices[$k] = trim((string) $v);

Additional Context

No response

@chalasr
Copy link
Member

chalasr commented Jan 25, 2022

Hi, can you send a PR on 6.0 4.4 with test case?

@BrokenSourceCode
Copy link
Contributor Author

BrokenSourceCode commented Jan 25, 2022

Hi, can you send a PR on 6.0 with test case?

@chalasr This my first contribution for Symfony, did I forget something?
Link to the PR

Regards

nicolas-grekas added a commit that referenced this issue Jan 26, 2022
…nSourceCode)

This PR was submitted for the 6.0 branch but it was squashed and merged into the 4.4 branch instead.

Discussion
----------

[Console] Fix PHP 8.1 deprecation in ChoiceQuestion

| Q             | A
| ------------- | ---
| Branch?       | ~6.0~ 4.4 for bug fixes <!-- see below -->
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #45179
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Commits
-------

aa89814 [Console] Fix PHP 8.1 deprecation in ChoiceQuestion
Nerahikada added a commit to Nerahikada/DiscordPHP that referenced this issue May 16, 2022
SQKo pushed a commit to discord-php/DiscordPHP that referenced this issue May 23, 2022
* Move an operator in the wrong position

* Fixed deprecations in php8.1

see: symfony/symfony#45179
@mathmul
Copy link

mathmul commented Jun 7, 2022

Possible Solution

We can simply cast the variable to string to fix the issue. Just like this at this line:

-$selectedChoices[$k] = trim($v);
+$selectedChoices[$k] = trim((string) $v);

Or trim($v ?? '')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants