Skip to content

fix: prevent error when detail message has console style #447

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

Merged
merged 8 commits into from
Feb 14, 2021
Merged

fix: prevent error when detail message has console style #447

merged 8 commits into from
Feb 14, 2021

Conversation

misaert
Copy link
Contributor

@misaert misaert commented Dec 9, 2020

Q A
Bug fix? yes
New feature? no

Before when we have a variable in console command with color (example: $io->text("Status: <fg=$statusColor>$status</>");, PHP Insights throws an exception:

$ vendor/bin/phpinsights analyse -vvv
...
In Color.php line 123:
                                                                                                                   
  [Symfony\Component\Console\Exception\InvalidArgumentException]                                                   
  Invalid "$statuscolor" color; expected one of (black, red, green, yellow, blue, magenta, cyan, white, default).  
                                                                                                                   

Exception trace:
  at <PROJECT_PATH>/vendor/symfony/console/Color.php:123
 Symfony\Component\Console\Color->parseColor() at <PROJECT_PATH>/vendor/symfony/console/Color.php:47
 Symfony\Component\Console\Color->__construct() at <PROJECT_PATH>/vendor/symfony/console/Formatter/OutputFormatterStyle.php:46
 Symfony\Component\Console\Formatter\OutputFormatterStyle->setForeground() at <PROJECT_PATH>/vendor/symfony/console/Formatter/OutputFormatter.php:215
 Symfony\Component\Console\Formatter\OutputFormatter->createStyleFromString() at <PROJECT_PATH>/vendor/symfony/console/Formatter/OutputFormatter.php:170
 Symfony\Component\Console\Formatter\OutputFormatter->formatAndWrap() at <PROJECT_PATH>/vendor/symfony/console/Formatter/OutputFormatter.php:135
 Symfony\Component\Console\Formatter\OutputFormatter->format() at <PROJECT_PATH>/vendor/symfony/console/Output/Output.php:157
 Symfony\Component\Console\Output\Output->write() at <PROJECT_PATH>/vendor/symfony/console/Output/Output.php:132
 Symfony\Component\Console\Output\Output->writeln() at <PROJECT_PATH>/vendor/symfony/console/Style/OutputStyle.php:62
 Symfony\Component\Console\Style\OutputStyle->writeln() at <PROJECT_PATH>/vendor/symfony/console/Style/SymfonyStyle.php:385
 Symfony\Component\Console\Style\SymfonyStyle->writeln() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Formatters/Console.php:351
 NunoMaduro\PhpInsights\Application\Console\Formatters\Console->issues() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Formatters/Console.php:99
 NunoMaduro\PhpInsights\Application\Console\Formatters\Console->format() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Formatters/Multiple.php:27
 NunoMaduro\PhpInsights\Application\Console\Formatters\Multiple->format() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Analyser.php:60
 NunoMaduro\PhpInsights\Application\Console\Analyser->analyse() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Commands/AnalyseCommand.php:70
 NunoMaduro\PhpInsights\Application\Console\Commands\AnalyseCommand->__invoke() at n/a:n/a
 call_user_func() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/src/Application/Console/Commands/InvokableCommand.php:37
 NunoMaduro\PhpInsights\Application\Console\Commands\InvokableCommand->execute() at <PROJECT_PATH>/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at <PROJECT_PATH>/vendor/symfony/console/Application.php:971
 Symfony\Component\Console\Application->doRunCommand() at <PROJECT_PATH>/vendor/symfony/console/Application.php:290
 Symfony\Component\Console\Application->doRun() at <PROJECT_PATH>/vendor/symfony/console/Application.php:166
 Symfony\Component\Console\Application->run() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/bin/phpinsights:35
 {closure}() at <PROJECT_PATH>/vendor/nunomaduro/phpinsights/bin/phpinsights:36

It is because the method writeln (https://github.com/nunomaduro/phpinsights/blob/master/src/Application/Console/Formatters/Console.php#L416) interprets colors in the code.

The fix escapes lines of detail message to prevent this.

I don't know if I have to test it and how to do it.

@misaert
Copy link
Contributor Author

misaert commented Dec 10, 2020

I have an error on CI:
image
(https://travis-ci.org/github/nunomaduro/phpinsights/jobs/748575277)

It is PHPStan but with different files:

 ------ --------------------------------------------------------------------- 
  Line   src/Domain/ComposerLoader.php                                        
 ------ --------------------------------------------------------------------- 
  57     Parameter #3 $installationManager of class Composer\Package\Locker   
         constructor expects Composer\Installer\InstallationManager,          
         Composer\Repository\RepositoryManager given.                         
  57     Parameter #4 $composerFileContents of class Composer\Package\Locker  
         constructor expects string, Composer\Installer\InstallationManager   
         given.                                                               
  57     Parameter #5 $process of class Composer\Package\Locker constructor   
         expects Composer\Util\ProcessExecutor|null, string given.            
 ------ --------------------------------------------------------------------- 
                                                                                
 [ERROR] Found 3 errors                                                         
                                                                                
Script phpstan analyse --ansi handling the phpstan:test event returned with error code 1

Why do I have these errors?

Thanks.

@Jibbarth
Copy link
Collaborator

Hi @misaert ! This fix seems legit 👍

But I don't see in which case the error is raised.
Did you find which issue is raised with the code $io->text("Status: <fg=$statusColor>$status</>"); ?

I wonder what is the output in the formatter.
Then, to test it, we can create a sample php that use this code, and launch analysis on it 😉

For the PHPStan error, I have to check that. I come back to you when I have found 🙃

@50bhan
Copy link
Contributor

50bhan commented Dec 10, 2020

@Jibbarth Maybe it's because of the new rules in PHPstan? Because it's OK on old versions. I think PHPstan changes in new versions is cause of this error.

@misaert
Copy link
Contributor Author

misaert commented Dec 10, 2020

Hi @misaert ! This fix seems legit

But I don't see in which case the error is raised.
Did you find which issue is raised with the code $io->text("Status: <fg=$statusColor>$status</>"); ?

I wonder what is the output in the formatter.
Then, to test it, we can create a sample php that use this code, and launch analysis on it

For the PHPStan error, I have to check that. I come back to you when I have found

Hi @Jibbarth,

The issue is PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer.

With the fix, the output is:

• [Code] Explicit string variable: (PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer)
  src/Command/StatusCommand.php:  
@@ -67 +67 @@
-        $io->text("Status: <fg=$statusColor>ERROR</>");
+        $io->text("Status: <fg=${statusColor}>ERROR</>");

I manually test it, yes. Bu I wonder if I have to test with PHPUnit and how if needed 😉

Thanks 🙂

@Jibbarth
Copy link
Collaborator

Hi @misaert,

I just merged master in your branch to get the new Github Action workflows, and I added some phpunit tests on this fix.

Thanks for contributing on this, really appreciated 👍

@Jibbarth Jibbarth merged commit 81bdfca into nunomaduro:master Feb 14, 2021
@misaert
Copy link
Contributor Author

misaert commented Feb 14, 2021

Hi @misaert,

I just merged master in your branch to get the new Github Action workflows, and I added some phpunit tests on this fix.

Thanks for contributing on this, really appreciated 👍

Thank you 👍

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

Successfully merging this pull request may close these issues.

3 participants