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

Introduce CLI argument to read the full analysis result as JSON #11713

Closed
ruudk opened this issue Sep 18, 2024 · 5 comments
Closed

Introduce CLI argument to read the full analysis result as JSON #11713

ruudk opened this issue Sep 18, 2024 · 5 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented Sep 18, 2024

Feature request

TwigStan is a CLI wrapper around PHPStan. It invokes PHPStan 2 times via the CLI.

The first time to collect data for the PHP files it analyzes. This collected data is then used by TwigStan for the next steps.

The second time to analyze the PHP files for errors. The errors are then processed/transformed and presented to the user.

Currently, there is no easy way to retrieve the collected data via the CLI.

And the --error-format=json has some major downsides: the JSON can get corrupted when running with --debug or -vv. Especially during development this is annoying.

To solve this problem, I created an ErrorFormatter that writes the whole AnalysisResult to a JSON file:
https://github.com/twigstan/twigstan/blob/main/src/PHPStan/AnalysisResultToJson.php

This now solves the above 2 issues.

Since this is a hacky solution, I'd like to discuss how we can improve PHPStan to make this a core functionality :blush

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 18, 2024

Hey, this doesn't have to be core functionality since you already solved it with a custom formatter :) I also use a custom formatter here: https://github.com/phpstan/phpstan/blob/2.0.x/identifier-extractor/src/ErrorFormatter.php

This project used to generate https://phpstan.org/error-identifiers has:

In your case, I'd just recommend you to also only output data to JSON that you need, in order to avoid excessive data file sizes and encode times.

@ruudk
Copy link
Contributor Author

ruudk commented Sep 18, 2024

Hmm, interesting.

What about this problem:

And the --error-format=json has some major downsides: the JSON can get corrupted when running with --debug or -vv. Especially during development this is annoying.

I see that you also switch between JSON and Table in the formatter:

if ($analysisResult->hasInternalErrors()) {
return $this->tableErrorFormatter->formatErrors($analysisResult, $output);
}

This makes it hard for the consumer side to deal with, no?

Sure, you can do a json_validate or handle a JSON decode exception. But it feels a bit nasty.

@ondrejmirtes
Copy link
Member

It makes sense for this JSON formatter to always return 0. I only read the result if the exit code is 0.

As for --debug, that's tough. But you don't do that once the code works for you 😊

As for -vvv, all of those things should be printed to stderr.

@ruudk
Copy link
Contributor Author

ruudk commented Sep 18, 2024

Alright, then I'll keep it like this.

Thanks for the explanation. And great that my hack was not so hacky after all 😁

@ruudk ruudk closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants