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

Could not read file: xyz.php #3066

Closed
twisted1919 opened this issue Mar 6, 2020 · 14 comments
Closed

Could not read file: xyz.php #3066

twisted1919 opened this issue Mar 6, 2020 · 14 comments

Comments

@twisted1919
Copy link

Bug report

We're using PHPStan 0.12.14 and this issue appeared recently, basically PHPStan errors out with hundred of errors like:

Could not read file: callout.php 

And we cannot find out what exactly is causing it, if you take a look bellow, 'callout' is just one of the array keys. Why PHPStan picks that one in particular, i have no idea.

Code snippet that reproduces the problem

public function run()
    {
        // update old layouts
        $models = ListPageType::model()->findAll();
        $searchReplace = [
            'panel panel-default'   => 'box box-primary borderless',
            'panel-heading'         => 'box-header',
            'panel-title'           => 'box-title',
            'panel-body'            => 'box-body',
            'callout'               => 'callout callout-info',
            'panel-footer'          => 'box-footer',
            'panel-'                => 'box-',
            '@import url(\'http://fonts.googleapis.com/css?family=Open+Sans\');'            => '',
            '@import url(\'http://fonts.googleapis.com/css?family=Noto+Sans:700italic\');'  => '',
            '#b94a48'               => '#367fa9',
            '\'Open Sans\','        => '',
            '\'Noto Sans\','        => '',
        ];
        foreach ($models as $model) {
            $model->content = str_replace(array_keys($searchReplace), array_values($searchReplace), $model->content);
            $model->save(false);
        }

        /** @var OptionEmailTemplate $optionEmailTemplate */
        $optionEmailTemplate = container()->get(OptionEmailTemplate::class);

        $common = str_replace('#b94a48', '#367fa9', $optionEmailTemplate->common);
        $optionEmailTemplate->saveAttributes(['common' => $common]);
        // end update
    }

Expected output

We're expecting PHPStan to show no error, at least not an error related to loading a file based on a defined array key.

@mergeable
Copy link

mergeable bot commented Mar 6, 2020

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

You use a broken autoloader, PHPStan tries to load class name callout. Autoloader should always silently skip unknown classes, but instead, yours prints this error: Could not read file: callout.php

@twisted1919
Copy link
Author

@ondrejmirtes - i don't know about that, i searched the whole project for that string and the only occurrence is in phpstan:

declare (strict_types=1);
namespace PHPStan\File;

class CouldNotReadFileException extends \PHPStan\AnalysedCodeException
{
    public function __construct(string $fileName)
    {
        parent::__construct(\sprintf('Could not read file: %s', $fileName));
    }
}

Again, this used to work just fine a while back.

Any pointers?

@ondrejmirtes
Copy link
Member

Show me your phpstan.neon and how you run PHPStan on the command line.

@twisted1919
Copy link
Author

My phpstan.neon :

parameters:
    checkMissingIterableValueType: false
    excludes_analyse:
        - /var/www/web/apps/backend/config/*
        # - /var/www/web/apps/backend/views/*

We're running phpstan in Docker and we have a bash script which calls it, here's the meaningful part:


run () {
    cd "$ROOT/apps/$1/" || exit
    echo -e "\n+++++++++++++++++++++++++++++"
    echo "Checking the '$1' app..."
  
    # ...
    # ...

    # currently --level max is an alias for --level 8
    ~/.composer/vendor/bin/phpstan analyse "/var/www/web/apps/$folder_name/" -c phpstan.neon --level max --memory-limit=-1
}

@twisted1919
Copy link
Author

Please also open this issue until we're sure what exactly is causing the problem, otherwise this will get lost in the list of closed issues.

@ondrejmirtes ondrejmirtes reopened this Mar 6, 2020
@ondrejmirtes
Copy link
Member

Can you try to delete files from your codebase until this problem is no longer happening? Of course you need to keep the file where ''callout'' occurs, but try deleting files until this bug is gone.

@twisted1919
Copy link
Author

Unfortunately i cannot do that, we're talking here about a huge project.

However, i tried to downgrade PHPStan, and version 0.12.9 works flawless, so whatever broke this has been introduced in version 0.12.10. Maybe this helps you pin-point what is going on.

@ondrejmirtes
Copy link
Member

Please try the current dev-master, I tried something: phpstan/phpstan-src@d0a9aa1

@twisted1919
Copy link
Author

There you go:

root@6c78d3a6d932:/var/www/web# ~/.composer/vendor/bin/phpstan --version
PHPStan - PHP Static Analysis Tool 0.12.x-dev@d0a9aa1

And the result:

PHP Fatal error:  Uncaught TypeError: Argument 12 passed to PHPStan\Analyser\Error::__construct() must be of the type array, null given, called in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php on line 148 and defined in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php:48
Stack trace:
#0 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php(148): PHPStan\Analyser\Error->__construct('Could not read ...', '/var/www/web/ap...', NULL, false, NULL, NULL, NULL, false, NULL, NULL, NULL, NULL)
#1 /tmp/phpstan/resultCache.php(97): PHPStan\Analyser\Error::__set_state(Array)
#2 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/ResultCache/ResultCacheManager.php(57): require('/tmp/phpstan/re...')
#3 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseApplication.php(64): PHPStan\Analyser\ResultCache\ResultCacheManager->restore(Array, false)
#4 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseComman in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php on line 48
Fatal error: Uncaught TypeError: Argument 12 passed to PHPStan\Analyser\Error::__construct() must be of the type array, null given, called in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php on line 148 and defined in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php:48
Stack trace:
#0 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php(148): PHPStan\Analyser\Error->__construct('Could not read ...', '/var/www/web/ap...', NULL, false, NULL, NULL, NULL, false, NULL, NULL, NULL, NULL)
#1 /tmp/phpstan/resultCache.php(97): PHPStan\Analyser\Error::__set_state(Array)
#2 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/ResultCache/ResultCacheManager.php(57): require('/tmp/phpstan/re...')
#3 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseApplication.php(64): PHPStan\Analyser\ResultCache\ResultCacheManager->restore(Array, false)
#4 phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Command/AnalyseComman in phar:///root/.composer/vendor/phpstan/phpstan/phpstan/src/Analyser/Error.php on line 48

@ondrejmirtes
Copy link
Member

Yeah, sorry about that. Please try the new dev-master (b7c8e61) again.

@twisted1919
Copy link
Author

I also removed the /tmp/phpstan folder just to be sure.
I think that also has something to do with all this. You know better though.
After i deleted it and required latest dev, i ran the tool and everything is good!

Thank you for fixing this, i really appreciate it.
I'll be waiting for the next release that ships the fix.

Thank you once again.

@ondrejmirtes
Copy link
Member

OK, cool :)

@lock
Copy link

lock bot commented Apr 15, 2020

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.
BTW: Did you know that PHPStan now has a brand new website with a user guide, guide to writing analyser-friendly PHP code, and a guide to developing PHPStan extensions? Visit phpstan.org today!

@lock lock bot locked as resolved and limited conversation to collaborators Apr 15, 2020
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