8000 [Console] ProgressBar::getMessage throws TypeError when message not defined · Issue #54386 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] ProgressBar::getMessage throws TypeError when message not defined #54386

New issue

Have a question about this proje 8000 ct? 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

Closed
kadet1090 opened this issue Mar 24, 2024 · 1 comment
Closed

Comments

@kadet1090
Copy link
kadet1090 commented Mar 24, 2024

Symfony version(s) affected

6.3.0-beta+

Description

When trying to obtain message from ProgressBar that was not set previously using setMessage, the getMessage method throws TypeError.

In ProgressBar.php line 188:
                                                                                                                  
  [TypeError]                                                                                                     
  Symfony\Component\Console\Helper\ProgressBar::getMessage(): Return value must be of type string, null 
  

The workaround is to catch the TypeError but it is not elegant solution.

How to reproduce

$progressBar = new ProgressBar($output);
$progressBar->getMessage(); // throws TypeError

Possible Solution

  • Change type hint of ProgressBar->getMessage() to ?string instead of string, add $default argument + ?? $default to the code.
  • Add method like hasMessage(string $name = 'message') to check if message is available

Additional Context

I have custom service that updates the progress bar only if necessary and thus I have to check if message that was supplied differs from the one that is already set:
https://github.com/cojedzie/cojedzie/blob/master/api/src/DataImport/ConsoleProgressReporter.php#L52-L55

@chalasr
Copy link
Member
chalasr commented Mar 24, 2024

Thanks for the report.

Change type hint of ProgressBar->getMessage() to ?string instead of string, add $default argument + ?? $default to the code.

Pull request welcome on branch 6.4

xabbuh added a commit that referenced this issue Mar 28, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

Fix TypeError on ProgressBar

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54386
| License       | MIT

`ProgressBar->getMessage()` returns null if the message is not defined

Commits
-------

adfe8df Fix TypeError on ProgressBar
@xabbuh xabbuh closed this as completed Mar 28, 2024
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

4 participants
0