Visual Studio Code: Unable to locate phpcs

后端 未结 15 1905
梦毁少年i
梦毁少年i 2021-02-01 01:56

I am facing this issue in Visual Studio Code. I have already tried reinstalling the phpcs extension but still facing this issue.

Unable to locat

15条回答
  •  有刺的猬
    2021-02-01 02:25

    I had the same issue on my Mac in Visual Studio Code (I used PEAR to install PHPCS). I do not know why but PHPCS couldn't find autoload.php of CodeSniffer. You can try to type on terminal command line:

    $ phpcs
    

    I got an error:

    Warning: include_once(PHP/CodeSniffer/autoload.php): failed to open stream: No such file or directory in /Users/david/pear/bin/phpcs on line 14

    Warning: include_once(): Failed opening 'PHP/CodeSniffer/autoload.php' for inclusion (include_path='.:') in /Users/david/pear/bin/phpcs on line 14

    Solution:

    Open Pear Bin folder in your user folder, e.g. in my case: /Users/david/pear/bin/ in Finder,

    Find phpcs file inside and edit it (e.g. in Visual Studio Code :),

    You will find these lines there:

    if (is_file(__DIR__.'/../autoload.php') === true) {
        include_once __DIR__.'/../autoload.php';
    } else {
        include_once 'PHP/CodeSniffer/autoload.php';
    }
    

    Add two new lines there with the proper location of CodeSniffer/autoload.php file (place it before } else { line and modify the path according to your needs - replace "david" with your user name...):

    } else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
        include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';
    

    So the final code of the condition is:

    if (is_file(__DIR__.'/../autoload.php') === true) {
        include_once __DIR__.'/../autoload.php';
    } else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
        include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';
    } else {
        include_once 'PHP/CodeSniffer/autoload.php';
    }
    

    Then save it and restart Visual Studio Code. That's it.

提交回复
热议问题