squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Windows/Cygwin. Wrong include path in phpcs when installed through PEAR? #1354

Closed Tahiche closed 7 years ago

Tahiche commented 7 years ago

Hi, I´m using Windows with cygwin (I know, terrible) and couldn´t get Codesniffer working with my IDE (Atom). Installed through PEAR and everything seemed right, files in place, etc, but my ID kept complaining about phpcs.bat. From the comand line in the php directory I tried running "phpcs.bat -i" and got no output. Tried "php phpcs -i" and it worked...

This made no sense... so i checked "phpcs"

if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
    include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
} else {
    include_once 'PHP/CodeSniffer/CLI.php';
}

But checking the installation, from where phpcs resides the correct path would be "pear/PHP/CodeSniffer/CLI.php" as that´s how pear installed it. Why does it work when invoked as "php phpcs -i"?. Because PEAR (go-pear.phar) added this line to my php.ini

;***** Added by go-pear
include_path=".;C:\MAMP\bin\php\php7.0.0\pear"
;*****

So it was resolving the include. But when invoked through phpcs.bat it is executing php as an .exe executable, again this is Windows with cygwin.... if "%PHPBIN%" == "" set PHPBIN=C:\MAMP\bin\php\php7.0.0\php.exe I´m guessing this php.exe is not affected by the php.ini and thus the pear include path is not interpreted.

To fix this I simply corrected the include path to point at the PEAR directory.

if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
    include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
} else {
    //include_once 'PHP/CodeSniffer/CLI.php';
    include_once 'pear/PHP/CodeSniffer/CLI.php';
}

Now it works and the IDE doesn´t complain.

EDIT: If i run "php.exe -i | grep .ini " it shows the php.ini file (C:\MAMP\bin\php\php7.0.0\php.ini) so it should parse it, I don´t know what the actual problem is and my "fix" might be wrong, but it´s working. I´d appreciate it if anyone could ponit out the possible cause of the problem.

gsherwood commented 7 years ago

Changing the PHPCS source code is not a great idea as it will break with every new release. It would be better for you to find out where php.exe is getting its ini file from, then adding the include_path setting in there as well.

I'm going to close this as it doesn't have anything to do with PHPCS specifically, but hopefully you find a good solution.

jrfnl commented 7 years ago

@Tahiche I've had similar issue running PHPCS on Windows before.

Here are a couple of things which may help:

In my phpcs.bat, I've got the below - changes:

set PHPBIN=C:\MAMP\bin\php\php7.0.0\php.exe
if "%PHPBIN%" == "" set PHPBIN=C:\MAMP\bin\php\php7.0.0\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 include_path="%PHPBIN%" -f "C:\MAMP\bin\php\php7.0.0\phpcs" -- %*

You may need to fiddle a bit with this to see what works for you, but hopefully this will help.

Tahiche commented 7 years ago

@jrfnl Thanks for you help, you pointed me in the right direction. Your script wasn´t working, I beleive becauseinclude_path="%PHPBIN%" is pointing to C:\MAMP\bin\php\php7.0.0\php.exe and not the directory. This works: "%PHPBIN%" -d include_path="C:\MAMP\bin\php\php7.0.0\pear" "C:\MAMP\bin\php\php7.0.0\phpcs" %* So it´s telling php to look in the "pear" folder and phpcs then uses the correct path and finds the class.

This also works: "%PHPBIN%" -c "C:\MAMP\conf\php7.0.0" "C:\MAMP\bin\php\php7.0.0\phpcs" %* The "-c" tells php to look for the php.ini file in that directory. That´s where pear added it´s include path on install

;***** Added by go-pear
include_path=".;C:\MAMP\bin\php\php7.0.0\pear"
;*****

So it seems, again, that when php.exe is called inside phpcs.bat it doesnçt know what php.ini to use... If i call it from the command line "php --ini" shows the correct php.ini file...

As @gsherwood points out, changing the phpcs is a bad idea. It´s probably better to change the .bat although it will also be overriden on update. I wish i could find out why php is not parsing the correct php.ini or at least the "include_path" in that file... If I run"%PHPBIN%" --ini in phpcs.bat this is what i get... "C:\MAMP\bin\php\php7.0.0\php.ini" so it´s not returning the correct path, it´s pointing to the "bin" folder where php.exe resides, not the "conf" folder where MAMP put it. I have no idea about where this is set.

I´ll try something like an alias "alias php='php -c C:\MAMP\bin\php\php7.0.0\php.ini " see if it works, or a symlink... to avoid changing the .bat

Thanks again!!

jrfnl commented 7 years ago

@Tahiche Glad to hear you got it working. Did you also add the PHP and PEAR directory to the Windows PATH system variables ? That should help too. (first bullet above)

Tahiche commented 7 years ago

@jrfnl Just figured it out. There´s actually 2 php.ini files in the MAMP installation, one in the "conf" directory and one in "C:\MAMP\bin\php\php7.0.0", the dir where php.exe resides, that´s the one php.exe is using whereas the command "php" was using the one in the "conf" directory. I set the pear include path in the right php.ini and modified my .bash_profile to make sure the command loads the right php.ini and deleted the one in "conf". Don´t see the point in having 2 php.ini files, it´s just confusing.

jrfnl commented 7 years ago

@Tahiche I'm not familiar with MAMP specifically, but with other similar setups, you generally have two php.ini files for good reason. Deleting one is generally speaking not a good idea.

So it's a good idea to keep both and allows you to have different settings for running PHP through the Apache/browser versus running it from the command line. Chances are that if the one in the conf directory is missing, Apache will recreate it on restart.