magento/magento-coding-standard

PHPStorm unable to run phpcs inspection

Vinai opened this issue ยท 12 comments

Vinai commented

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.

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


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

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

fiko commented

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

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

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

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: AdobeDocs/commerce-php#48

I'm seeing this issue when running on CLI too

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 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 thanks, buddy! Using COMPOSER_ALLOW_SUPERUSER indeed fixed it ๐ŸŽ‰

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