magento-coding-standard icon indicating copy to clipboard operation
magento-coding-standard copied to clipboard

PHPStorm unable to run phpcs inspection

Open Vinai opened this issue 2 years ago • 12 comments

Preconditions

  1. Tested on Magento Open Source 2.4.4 and Adobe Commerce 2.4.4
  2. PHP 8.1 (I haven't tried 7.4)
  3. PHPStorm version is 2021.2.4

Steps to reproduce

  1. Configure PHP interpreter in PHPStorm
  2. Configure phpcs in PHPStorm to use the Magento coding standard
  3. Enable the phpcs inspections

Expected result

  1. PHPStorm displays phpcs rule violations according to the configuration

Actual result

In the PHStorm event log the message is shown

ERROR: Referenced sniff "PHPCompatibility.FunctionUse.RemovedFunctions" does not exist

Additional Information

Running phpcs on the command line works

vendor/bin/phpcs --standard=vendor/magento/magento-coding-standard/Magento2 path/to/check

The same PHPStorm configuration works on Magento 2.4.3 with PHP 7.4.


If I remove the reference to that sniff from the Magento2/ruleset.xml, phpcs fails with a stacktrace where any other Magento2 rule extending from a PHPCompatibility rule class can't find the parent class.
Maybe this points to an autoload error? However, I quick test by adding "PHPCompatibility\\:": "vendor/phpcompatibility/php-compatibility/PHPCompatibility/" to the root composer.json and dumping the composer autoloader did not change anything.

Vinai avatar May 11 '22 05:05 Vinai

Hi @Vinai. Thank you for your report. To speed up processing of this issue, make sure that you provided sufficient information.

Add a comment to assign the issue: @magento I am working on this


m2-assistant[bot] avatar May 11 '22 05:05 m2-assistant[bot]

I'm facing the same issue. My PHPStorm version is: 2022.1

rafafortes avatar May 11 '22 11:05 rafafortes

For somehow $installedPath ../../phpcompatibility/php-compatibility was missing during inspecting code on PHPStorm but it doesn't while using cli

tuyennn avatar Jun 02 '22 08:06 tuyennn

it's not permanent fixing, but you may apply this patch @tuyennn @Vinai https://gist.github.com/fiko/bdec71978b458cc527df03d26a695b6c

fiko avatar Jul 20 '22 08:07 fiko

I'm facing the same issue. Any news on this?

MeCapron avatar Jul 26 '22 19:07 MeCapron

Any update on this? PHPCS is an important part of our workflow.

snoop0x7b avatar Aug 09 '22 19:08 snoop0x7b

To include Magento Coding Standard as part of PHPStorm inspections, follow the steps:

  • Open PHPStorm preferences
  • Navigate to "Editor" | "Inspections"
  • Select "PHP" | "Quality Tools" | "PHP_CodeSniffer validation"
  • Set "Coding standard" to "Custom" and select path to <project_root>/vendor/magento/magento-coding-standard/Magento2/ruleset.xml
  • Check "Installed standards path" and select path to <project_root>/vendor/php-compatibility/PHPCompatibility

PR adding these instructions to the documentation: https://github.com/AdobeDocs/commerce-php/pull/48

sivaschenko avatar Aug 24 '22 16:08 sivaschenko

I'm seeing this issue when running on CLI too

lfolco avatar Jan 16 '23 22:01 lfolco

Did anyone find a proper fix for this? For us, it is only happening in our pipepline, but not locally. https://gist.github.com/fiko/bdec71978b458cc527df03d26a695b6c is rather hacky.

sprankhub avatar Feb 13 '24 10:02 sprankhub

@sprankhub I fixed this locally by un-setting the 'installed_paths' option within PHPStorm, and relying instead on the installer plug-in. Are you perhaps running your pipeline as root? If so, you'll need to (either switch to a non-root user or) tell Composer that you really want to run plug-ins with https://getcomposer.org/doc/03-cli.md#composer-allow-superuser

fredden avatar Feb 13 '24 10:02 fredden

@fredden thanks, buddy! Using COMPOSER_ALLOW_SUPERUSER indeed fixed it :tada:

sprankhub avatar Feb 13 '24 10:02 sprankhub

I ran into this issue on 2.4.6-p3 and had to modify the composer script bit

"scripts": {
    "post-install-cmd": [
      "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/,../../magento/php-compatibility-fork)"
    ],
    "post-update-cmd": [
      "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/,../../magento/php-compatibility-fork)"
    ]
}

I don't know at what point the paths changed from phpcompatibility/php-compatibility to magento/php-compatibility-fork but inspections now work on my PHPStorm

gowrizrh avatar Feb 28 '24 03:02 gowrizrh