PHP CodeSniffer include_once error
Asked Answered
U

11

18

I'm trying to install PHP CodeSniffer on OS X Mountain Lion - and I appear to be getting a strange problem

When running 'phpcs' I get the following error:

PHP Warning:  include_once(PHP/CodeSniffer/CLI.php): failed to open stream: No such
file or directory in /usr/lib/php/pear/bin/phpcs on line 31

PHP Warning:  include_once(): Failed opening 'PHP/CodeSniffer/CLI.php' for inclusion
(include_path='.;/usr/lib/php/pear/share/pear/') in /usr/lib/php/pear/bin/phpcs on line 31

PHP Fatal error:  Class 'PHP_CodeSniffer_CLI' not found in /usr/lib/php/pear/bin/phpcs
on line 34

The file /usr/lib/php/pear/share/pear/PHP/CodeSniffer/CLI.php exists, which is confusing me

Umpteen answered 10/1, 2013 at 11:46 Comment(1)
is /usr/lib/php/pear/share/pear/ in your PHP default include list? (check php.ini)Karena
U
0

Found the problem - I was using the Windows separator (semi-colon) in the include_path instead of the Unix one (colon), so it should've been:

 .:/usr/lib/php/pear/share/pear/

instead of

 .;/usr/lib/php/pear/share/pear/
Umpteen answered 10/1, 2013 at 13:38 Comment(1)
IMHO this is a better answer than the others, just set your include path properly.Tat
U
12

On my configuration the PHP/ path just wasn't where phpcs expected it. I solved it via creating symlink to the missing path.

go to pear directory and run:

ln -s share/pear/PHP/ PHP
Unreasonable answered 18/3, 2014 at 8:42 Comment(2)
Not quite the one I needed, but got me to a solution. ThxAntonelli
My pear directory was in my user folder (~/pear), so I had to add that to the include_path in php.ini.Mofette
K
7

This is perhaps not the best solution, but it requires no change to your path or anything else. In the file phpcs you will find a section with:

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

Just add a new else if with your path to the correct file CLI.php (i.e. '/usr/local/pear/share/pear/PHP/CodeSniffer/CLI.php'):

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

Last but not least document this change for later versions and updates. In the end the solution has to be that the developer of PHPCS makes a more solid construction for finding the CLI.php

Kirstenkirsteni answered 19/12, 2014 at 10:6 Comment(1)
I love this kind of solutions :). Thanks mister, you saved my day .Zumstein
P
6

I got this error when using PHP CodeSniffer installed via Composer.

Fixed it with:

cd /path/to/app
rm -rf vendor/
composer update
Pliers answered 2/4, 2013 at 3:26 Comment(0)
S
1

If you are using MAMP include this in your path:

export PATH=/Applications/MAMP/bin/php/php5.X.XX/lib/php:$PATH

by replacing 5.X.XX with your php version. In my case this was:

export PATH=/Applications/MAMP/bin/php/php5.4.26/lib/php:$PATH
Swordplay answered 17/7, 2014 at 21:9 Comment(0)
G
1

Uninstall it and reinstall it using composer

alias php=/Applications/MAMP/bin/php/php5.6.10/bin/php;
composer global require "squizlabs/php_codesniffer=*";

Source: https://tommcfarlin.com/php-codesniffer-with-composer/

Granite answered 29/9, 2016 at 23:15 Comment(0)
U
0

Found the problem - I was using the Windows separator (semi-colon) in the include_path instead of the Unix one (colon), so it should've been:

 .:/usr/lib/php/pear/share/pear/

instead of

 .;/usr/lib/php/pear/share/pear/
Umpteen answered 10/1, 2013 at 13:38 Comment(1)
IMHO this is a better answer than the others, just set your include path properly.Tat
I
0

In my case -- having installed PHP-OSX -- I had to fix the symlink like so:

cd /usr/local/php5/lib/php/PHP

and then:

ln -s /usr/local/share/pear/PHP/CodeSniffer
ln -s /usr/local/share/pear/PHP/CodeSniffer.php
Interblend answered 19/10, 2014 at 14:8 Comment(0)
B
0

For what it is worth, I installed PHP using homebrew (OS X). First install was 5.6.3, but upgraded over time. The default ini file, you can find the location of the file with:

php -i | grep ini

on osx - still had path references to 5.6.3. When I updated these - specifically the include path, all worked just fine; no symlinks required.

Bunche answered 31/1, 2015 at 16:58 Comment(0)
S
0

This D:\wamp\bin\php\php_VERSION\phpcs modification worked for me on wamp

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

$cli = new PHP_CodeSniffer_CLI();
$cli->runphpcs();
Sclera answered 3/2, 2016 at 0:31 Comment(0)
R
0

I had this issue trying to run phpcs as a non-privileged user, having installed it via PEAR, as root.

Fix for me was to change permissions so that the non-privileged user could access the dependencies:

chmod -R o+rx /usr/local/lib/php/
Reiko answered 2/10, 2017 at 14:24 Comment(0)
B
0

I solved it by adding the correct path to the other directory that PHP scans for INI files (it's listed in phpinfo.php). Execute these three commands from Terminal:

sudo mkdir -p /Library/Server/Web/Config/php

sudo touch /Library/Server/Web/Config/php/local.ini

echo 'include_path = ".:'`pear config-get php_dir`'"' | sudo tee -a /Library/Server/Web/Config/php/local.ini
Blackmore answered 14/4, 2018 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.