5

I am trying to set up the PHPCS plugin, but so far I am only getting this error:

phpcs: Unknown error ocurred. Please verify that /home/[user]/Documents/offprojects/vet_direct/vetsdirectapp/vendor/bin/phpcs --report=json -q --encoding=UTF-8 --error-severity=5 --warning-severity=5 --stdin-path=/home/[user]/Documents/offprojects/vet_direct/vetsdirectapp/app/Http/Controllers/JobController.php - returns a valid json object.

I followed the installation guide from the plugin page and installed PHPCS both globally and in the project. Also, the command in terminal shows this error:

zsh: command not found: phpcs

Any idea what is wrong here?

Sasha
  • 8,521
  • 23
  • 91
  • 174

5 Answers5

2

This might not help you, but for me, the executable path wasn't correct and the version of PHPCS didn't support PHP 7, it was still using PHP 5.

I'm going to assume you're on Linux based OS.

Since you have already installed PHPCS globally, access the phpcs file. This should be somewhere like ~/.config/composer/vendor/squizlabs/php_codesniffer/bin if you're on Debian based Linux OS (like Ubuntu Focal Fossa 20.04LTS). But it might differ depending on versions.

If you use a text editor to peek the phpcs file inside it (this will be the same on your Executable Path), it might say PHP 5, that's how I found I needed to upgrade mine.

To upgrade your PHPCS do composer global require "squizlabs/php_codesniffer=*" this might fix your issue. After that, you might need to fix your Executable Path again, I had to do mine.

To edit the executable path on Visual Studio Code Command + , (or go to settings) then search for PHP CodeSniffer. Go down to Executable Path and make sure it's correct.

This might not work, but it's worth a try.

Hugo
  • 144
  • 1
  • 11
1

After trying a lof of different things, this is what worked for me. I'm on a Mac and using Zsh.

First, installed PHP Code Sniffer (phpcs) with Composer, as Hugo suggested on the answer above:

composer global require "squizlabs/php_codesniffer=*"

But then, I had to edit my .zshrc and added this line:

export PATH=/Users/YOURUSER/.composer/vendor/bin:$PATH

After that, close the Terminal and I was able to run phpcs -h

ecairol
  • 6,233
  • 1
  • 27
  • 26
0

In my case an error was being thrown by various processes "Could not load xdebug because it is already loaded". This did not show in VSCode, but did if I ran php -v or php --ini.

This is what cleared the error for me: Edit the /etc/php/8.0/apache2/conf.d/xdebug.ini file and comment out the line zend_extension=xdebug.so. Restart apache with sudo systemctl restart apache2, then try php -v. The "Could not load xdebug because it is already loaded" error should be gone. Restart VSCode and this phpcs error should stop showing.

I have VSCode configured to debug php and have the xdebug configuration settings in both the etc/php/php8.0/apache2/php.ini and etc/php/php/8.0/cli/php.ini files.

How I discovered this is I downloaded the source code and searched for where that error was being thrown. What I found was that ANY error in stderr outside of a very narrow range gets this error message. I had noticed this error showing in my terminal, and I researched how to clear it. When I did, the VSCode phpcs error went away!

Jim D
  • 65
  • 1
  • 8
0

On Windows:

  1. Open the XAMPP Control Panel.
  2. Apache -> Config -> php.ini
  3. Search for zend_extension=xdebug.so or something the like and delete it.
  4. Restart Apache.
  5. Try again.
0

In my case, It was happening due to XDebug and resolved after disabling xdebug from Xampp.
Goto C:\xampp\php\php.ini
find and disable (add ;) before following lines
Change :

xdebug.mode=debug
xdebug.start_with_request=yes
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"

to :

;xdebug.mode=debug 
;xdebug.start_with_request=yes  
;zend_extension = "C:\xampp\php\ext\php_xdebug.dll" 


Restart Apache and then vscode. It should fix that.