backdrop-contrib / coder_review

"Developer module" which reviews your code identifying coding style problems and where updates to the API are required.
GNU General Public License v2.0
1 stars 7 forks source link

Let's get PHP_CodeSniffer working with Backdrop #10

Open alanmels opened 4 years ago

alanmels commented 4 years ago

I am trying to get PHP_CodeSniffer working with Backdrop. So far I've got it installed on OS level by taking the following steps:

sudo pear channel-update pear.php.net
sudo pear install PHP_CodeSniffer
sudo ln -sv /var/www/docroot/modules/coder_review/coder_sniffer/Backdrop $(pear config-get php_dir)/PHP/CodeSniffer/Standards
sudo phpcs --config-set installed_paths /var/www/docroot/modules/coder_review/coder_sniffer
sudo phpcbf --config-set installed_paths /var/www/docroot/modules/coder_review/coder_sniffer

and now the both the phpcs -i and phpcbf -i commands output:

The installed coding standards are Squiz, Zend, MySource, PSR1, PEAR, PSR2, PSR12 and Backdrop

However, Backdrop is not picking up PHP_CodeSniffer and the status page still shows:

Coder Review 
PHP_CodeSniffer not installed. 
Coder review uses PHP_CodeSniffer to perform better code review checks; Code_Sniffer is not a requirement, but it is helpful. Use "drush install-php-code-sniffer" to help install.

Also running simple phpcs --standard=Backdrop -e command outputs the error:

docker@cli:/var/www/docroot/modules$ phpcs --standard=Backdrop -e

Fatal error: Class 'Generic_Sniffs_Strings_UnnecessaryStringConcatSniff' not found in /var/www/docroot/modules/coder_review/coder_sniffer/Backdrop/Sniffs/Strings/UnnecessaryStringConcatSniff.php on line 30

Call Stack:
    0.0011     371072   1. {main}() /usr/local/bin/phpcs:0
    0.0045     531672   2. PHP_CodeSniffer\Runner->runPHPCS() /usr/local/bin/phpcs:18
    0.0232     901376   3. PHP_CodeSniffer\Runner->init() /usr/local/lib/php/PHP/CodeSniffer/src/Runner.php:70
    0.0309    1138136   4. PHP_CodeSniffer\Ruleset->__construct() /usr/local/lib/php/PHP/CodeSniffer/src/Runner.php:332
    0.1454    1187120   5. PHP_CodeSniffer\Ruleset->registerSniffs() /usr/local/lib/php/PHP/CodeSniffer/src/Ruleset.php:217
    0.1455    1187120   6. PHP_CodeSniffer\Autoload::loadFile() /usr/local/lib/php/PHP/CodeSniffer/src/Ruleset.php:1156
    0.1458    1214456   7. include('/var/www/docroot/modules/coder_review/coder_sniffer/Backdrop/Sniffs/Strings/UnnecessaryStringConcatSniff.php') /usr/local/lib/php/PHP/CodeSniffer/autoload.php:167

docker@cli:/var/www/docroot/modules$ 

Any further hints would be appreciated on how to get PHP_CodeSniffer working with Backdrop.

alanmels commented 4 years ago

The above way was an attempt to get it working without Composer, however this might need to be resolved together with https://github.com/backdrop-contrib/coder_review/issues/3.

alanmels commented 4 years ago

According to https://www.drupal.org/project/coder/issues/2809335 Codesniffer 3 is not supported, so I downgraded back to PHP_CodeSniffer-2.9.1:

sudo pear uninstall PHP_CodeSniffer
sudo pear install PHP_CodeSniffer-2.9.1

the same command phpcs --standard=Backdrop -e is giving different error instead:

docker@cli:/var/www/docroot/modules$ phpcs --standard=Backdrop -e

Fatal error: Uncaught PHP_CodeSniffer_Exception: Referenced sniff "Generic.Formatting.SpaceAfterCast" does not exist in /usr/local/lib/php/PHP/CodeSniffer.php on line 1167

PHP_CodeSniffer_Exception: Referenced sniff "Generic.Formatting.SpaceAfterCast" does not exist in /usr/local/lib/php/PHP/CodeSniffer.php on line 1167

Call Stack:
    0.0009     371528   1. {main}() /usr/local/bin/phpcs:0
    0.0180    1391640   2. PHP_CodeSniffer_CLI->runphpcs() /usr/local/bin/phpcs:25
    0.0186    1436496   3. PHP_CodeSniffer_CLI->process() /usr/local/lib/php/PHP/CodeSniffer/CLI.php:113
    0.0292    1441448   4. PHP_CodeSniffer_CLI->explainStandard() /usr/local/lib/php/PHP/CodeSniffer/CLI.php:948
    0.0294    1442152   5. PHP_CodeSniffer->process() /usr/local/lib/php/PHP/CodeSniffer/CLI.php:1203
    0.0294    1442152   6. PHP_CodeSniffer->initStandard() /usr/local/lib/php/PHP/CodeSniffer.php:505
    0.0298    1442616   7. PHP_CodeSniffer->processRuleset() /usr/local/lib/php/PHP/CodeSniffer.php:578
    0.1026    1463072   8. PHP_CodeSniffer->_expandRulesetReference() /usr/local/lib/php/PHP/CodeSniffer.php:780

docker@cli:/var/www/docroot/modules$
jenlampton commented 4 years ago

I'm adding PHPCS code reviews for my Backdrop contrib projects using github actions. I was able to get results only by using PHP 5.6, and version 1.5.6 of PHPCS.

I ran into all the same problems you hit @alanmels and downgrading PHPCS to earlier versions was the answer each time.

I think Drupal 7 is on version 2 (to get PHP 7.3 support) and Drupal 8 is on version 3 now. See Related: https://www.drupal.org/project/coder/issues/2863898

alanmels commented 4 years ago

Thank you, Jen, for your input. I think your finding needs to be reflected on Backdrop's UI so users avoid hitting the same problems.

jenlampton commented 4 years ago

I also think we should see if we can pull in the Drupal changes to get up to date :) I'll create a separate issue.

edit: https://github.com/backdrop-contrib/coder_review/issues/12 Update to a newer version of PHP_CodeSniffer

izmeez commented 1 year ago

I just added the phpcs.phar with a direct download and was able to get it working with VSCode without any composer.