PhpStorm Can not run PHP Code CodeSniffer
Asked Answered
S

12

10

Im trying to use CodeSniffer in PhpStorm.

In settings->PHP->CLI Interpreter I linked to the php.exe Im using https://windows.php.net/ but also tried it with Cygwin and XAMPP.

PhpStorm shows me the right PHP Version 7.2.5 and the php.ini

In the CodeSniffer configuration I selected the phpcs.bat When Im clicking on Validate I just got

Can not run PHP Code

Also added everything to the PATH

Did I miss something?

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Sukin answered 9/8, 2018 at 7:56 Comment(8)
Try running the phpcs file instead of the phpcs.bat fileSuperconductivity
Then I get Cannot run program "...\phpcs": CreateProcess error=193, %1 is not a valid Win32-ApplicationSukin
1) Screenshots with settings please. Details matters. 2) Is you php.exe is system-wide discoverable? I mean -- will it show the right path if you run where php in terminal (outside of IDE)?Charioteer
I added some screensSukin
You are executing phpcs.bat -- perfect. .BAT file inside calls your php.exe (as it does not know anything about PhpStorm and PHP Interpreters and stuff) ... and right now it looks like php.exe is not actually discoverable by OS. You need to put the path to it into system PATH variable. That's what error message suggests me...Charioteer
When Im calling where php it shows me the correct path to the php.exe. When im calling php *\phpcs.bat in cmd it will be executed without an errorSukin
Check idea.log for possible hints then (Help | Show Log in Explorer)Charioteer
there are no hints. Also did a new install via pearSukin
K
3

You should put phpcs.bat and phpcs in your PHP folder - e.g. d:\program\php\phpcs. The CodeSniffer itself should reside in d:\program\php\PEAR\PHP\CodeSniffer - there will be a script autoload.php and a subfolder src.

Then in Settings -> Languages & Frameworks -> PHP -> Code Sniffer you specify the path to phpcs.bat and Validate it.

Then in Settings -> Editor -> Inspections you find the node PHP Code Sniffer validation and enable it. After enabling it you will be able to configure it - specifically choose the coding standard.

enter image description here enter image description here

This is my phpcs

#!D:\PROGRAM\Inet\Design\php\php.exe
<?php
/**
 * PHP_CodeSniffer detects violations of a defined coding standard.
 *
 * @author    Greg Sherwood <[email protected]>
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

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

$runner   = new PHP_CodeSniffer\Runner();
$exitCode = $runner->runPHPCS();
exit($exitCode); 

This is my phpcs.bat

@echo off
REM PHP_CodeSniffer detects violations of a defined coding standard.
REM 
REM @author    Greg Sherwood <[email protected]>
REM @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
REM @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence

if "%PHPBIN%" == "" set PHPBIN=D:\PROGRAM\Inet\Design\php\php.exe
if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
set PHPBIN=%PHP_PEAR_PHP_BIN%
:RUN
"%PHPBIN%" "D:\PROGRAM\Inet\Design\php\phpcs" %* 

This is my PEAR_ENV.reg which I have imported into Windows Registry

REGEDIT4
[HKEY_CURRENT_USER\Environment]
"PHP_PEAR_SYSCONF_DIR"="D:\\PROGRAM\\Inet\\Design\\php"
"PHP_PEAR_INSTALL_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\pear"
"PHP_PEAR_DOC_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\docs"
"PHP_PEAR_BIN_DIR"="D:\\PROGRAM\\Inet\\Design\\php"
"PHP_PEAR_DATA_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\data"
"PHP_PEAR_PHP_BIN"="D:\\PROGRAM\\Inet\\Design\\php\\php.exe"
"PHP_PEAR_TEST_DIR"="Z:\\Temp\\"
Karol answered 9/8, 2018 at 8:28 Comment(4)
I did it like in your description, but I cant Validate itSukin
Perhaps Validate shows some meaningful error message?Karol
Thank you! I installed everything new with pear and now it works. But still dont know what was wrong :)Sukin
@TedLoganHow did you install with PEAR on windows?Glisson
L
3

I know this is not a satisfying solution, but for me (on Windows 10, having installed CodeSniffer as global composer installation) it got resolved by restarting PhpStorm.

Landscapist answered 21/5, 2020 at 19:3 Comment(2)
I reached the mouth of the rabbits hole when I found this gentle reminder to try rebooting. AND IT WORKED. Thanks!Heptane
I came where when setting up on ubuntu, with docker. After restarting it helped me to make show custom coding standard.Wolfy
H
2

In case somebody stumbles on this looking for how to set up PhpStorm tools that are installed within WSL/WSL2, this JetBrains issue thread helped me.

Basically, you want to set your CLI interpreter to the version of PHP within your WSL OS (for me, Ubuntu) under Languages & Frameworks > PHP. Then, under Quality Tools, you want to create a new configuration (the plus sign on the top-left side of the pop-up window that appears after you press the "..." button), select your CLI interpreter, and configure it from there. Be sure to use forward slashes for the path (e.g. "/home/[...]") and make it relative to the interpreter's OS (i.e. no need to start the path with "\\$wsl[...]"). After saving, you can select that Interpreter next to Configuration (instead of "Local") for that tool.

Honeysweet answered 14/2, 2022 at 22:15 Comment(0)
C
0

I'm using OpenServer with PHP 7.0-64x I only have composer and PHP in my Env. vars.

but before i tested it in console and everything was just fine phpcs version: 3.3.2 (latest for these days) smth like this:

C:\OpenServer\domains\phpWorkingWithFiles\vendor\bin>phpcs --standard=PSR2 C:\OpenServer\domains\phpWorkingWithFiles\classes\Recipe.php

or like this

C:\OpenServer\domains\phpWorkingWithFiles>vendor\bin\phpcs classes/ --colors

had the same error I had reran the Phpstorm and it started working

Contend answered 14/11, 2018 at 14:49 Comment(0)
E
0

i had this problem too;then i remove phpcs which is in the same dir with phpcs.bat,and it worked.

Enteron answered 18/3, 2019 at 6:59 Comment(0)
C
0

We also had the same problem and there was no PHP installed.

To be able to load code sniffer you MUST have PHP installed (locally on your PC/Laptop)!

Chigoe answered 26/6, 2019 at 12:1 Comment(0)
P
0

Try to run it with php cli to see the error text. In my case it was:

$ php /home/alex/apps/code-style/code-style/scripts/phpcs.php
PHP Fatal error:  require_once(): Failed opening required '/home/daniel/apps/code-style/code-style/scripts/../vendor/autoload.php' (include_path='.:/usr/share/php') in /home/daniel/apps/code-style/code-style/scripts/phpcs.php on line 3

(I forgot to run composer install command in project root).

Plutonian answered 16/8, 2019 at 13:44 Comment(0)
D
0

For me restarting PC solved issue. Tried restarting PHPStorm and relogin from Windows user, but did not work

Dirge answered 13/12, 2020 at 13:26 Comment(0)
D
0

In my case adding PHP to Windows Environment path solved the issue.

Even though, I set the path of PHP in PHPStorm Settings -> Language & Frameworks-> PHP -> CLI Interpreter I still got this error, until I set it in Windows Environment Variables.

In my case error message was Can not run PHP_CodeSniffer when I tried to validate the path of phpcs.bat

Dirge answered 22/2, 2021 at 10:57 Comment(0)
N
0

I was getting the error Cannot run program "phpcs.phar": CreateProcess error=193, %1 is not a valid Win32-Application when I selected the phpcs.phar file in PHPStorm. My %PATH% included my PHP installation directory and I could run php from a command prompt.

The error from PHPStorm implied that it was trying to launch phpcs.phar as an executable, rather than passing it to PHP with php phpcs.phar. I fixed the issue by creating a phpcs.bat containing the following:

@php C:\absolute\path\to\phpcs.phar %1

I then selected my phpcs.bat in PHPStorm, instead of phpcs.phar. I was then able to validate and run the tool from within PHPStorm. The same approach worked for the other code quality tools.

Nucleo answered 11/11, 2021 at 21:46 Comment(0)
K
0

Configure the interpreter and choose the PHP version that you want to use.

enter image description here

Next, navigate to Quality Tools and enable PHP CS Fixer with the correct PHP Interpreter.

enter image description here

Kp answered 13/10, 2023 at 4:33 Comment(0)
M
0

I had the same problem on MacOs using MAMP with path pointing php to a custom batch file that switches the version depending on the local folder it is called from, so the default config was never going to work for me.

I edited the <path to project>/vendor/bin/phpcs and <path to project>/vendor/bin/phpcbf and swapped the top line of

#!/usr/bin/env php

with

#!/usr/bin/env <path to PHP file in MAMP>

Seems to validate after that

Middaugh answered 27/10, 2023 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.