Visual Studio Code: Unable to locate phpcs

后端 未结 15 1844
梦毁少年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.

    0 讨论(0)
  • 2021-02-01 02:25

    I had this same error and I noticed that phpcs was working from the command line but not working in vscode. I had to read the linter.js file (of the vscode extension) to see where this error is coming from. This fix is more of an info.

    So, to fix this: First, run the command phpcs --version and check if the command does not print any warning or error before outputting a line similar to PHP_CodeSniffer version x.x.x (stable) by Squiz (HTTP://www.squiz.net).

    If any error or warning (usually unrelated to phpcs) is thrown before the line showing the phpcs version, please go fix that warning or error first. According to linter.js, it expects that the first output (when phpcs.bat is executed) must start with the PHP_CodeSniffer version string. Thus, If anything should print before the PHP_CodeSniffer version ...., then it throws the error.

    So, You MUST fix any warning or error printed before the phpcs version if you want phpcs to work in VSCode. I hope this fix works for you. cheers!

    0 讨论(0)
  • 2021-02-01 02:26

    paste the following in your terminal for system-wide installation

    composer global require squizlabs/php_codesniffer
    

    This stopped the error message from popping up in mine.

    0 讨论(0)
提交回复
热议问题