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

Added prefix for the output file #163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

szymekjanaczek
Copy link

Hi!

I've recently spend quite a long time trying to understand, why I'm getting error like this one below:

PHP Fatal error:  Uncaught RuntimeException: Expecting array, got string in /var/www/html/vendor/staabm/phpstan-baseline-analysis/lib/AnalyzerResultReader.php:23

after executing php ./vendor/bin/phpstan-baseline-graph '*.json' > result.html in my project.

The reason was really simple: *.json was catching all json files - including e. g. composer.json.

Fixed potential problem caused by confusing phpstan-baseline-analyze output files and other - like "composer.json"
@staabm
Copy link
Owner

staabm commented Jun 15, 2024

The reason was really simple: *.json was catching all json files - including e. g. composer.json.

I think we should improve the json file loading so a more useful error is reported in unexpected file content

@szymekjanaczek
Copy link
Author

@staabm sure! What about adding file name to the exceptions messages?

We can wrap all this code in try...catch and throw one Exception with original error message and the source file name.
Or just add file name to each exception one by one.

foreach ($json as $data) {

    if (!is_array($data)) {
        throw new \RuntimeException('Expecting array, got ' . gettype($data));
    }

    foreach ($data as $baselinePath => $resultArray) {

        if (!is_string($baselinePath)) {
            throw new \RuntimeException('Expecting string, got ' . gettype($baselinePath));
        }
        if (!is_array($resultArray)) {
            throw new \RuntimeException('Expecting array, got ' . gettype($resultArray));
        }

        $decoded[$baselinePath] = $this->buildAnalyzerResult($resultArray);
    }
}

@staabm
Copy link
Owner

staabm commented Jun 18, 2024

the reported exception should be able to tell the user with which file a problem occured

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.

2 participants