1

I am trying to install phpcodesniffer in my magento module project. Below is my composer.json

{
    "name": "test/myModule",
    "description": "testin",
    "type": "magento2-module",
    "version": "1.0.0",
    "require": {
        "php": "^8.1"
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "3.*",
        "magento/magento-coding-standard": "^31.0"
    },
    "autoload": {
      "files": [ "registration.php" ],
      "psr-4": {
        "Test\\Module\\": ""
      }
    }
  }

Now I can successfully run composer update/install in my folder. However when I run the command vendor/bin/phpcs --standard=Magento2 ./ I get an error like below

PHP Fatal error:  Uncaught Error: Class "Magento\Framework\Component\ComponentRegistrar" not found in /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/registration.php:2
Stack trace:
#0 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/composer/autoload_real.php(55): require()
#1 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/composer/autoload_real.php(38): composerRequireabd7530303d7978f314768d362cfeb3f()
#2 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/autoload.php(25): ComposerAutoloaderInitabd7530303d7978f314768d362cfeb3f::getLoader()
#3 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/squizlabs/php_codesniffer/autoload.php(79): include('...')
#4 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/squizlabs/php_codesniffer/bin/phpcs(17): PHP_CodeSniffer\Autoload::load()
#5 /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/vendor/bin/phpcs(120): include('...')
#6 {main}
  thrown in /home/cloudpanel/htdocs/magento2.mgt/app/code/Test/Module/registration.php on line 2 

How can I run the php codesniffer here?

footy
  • 5,803
  • 13
  • 48
  • 96
  • I tried running composer require magento/framework but I am not able to find any such dependency. What is the right dependency? – footy Feb 10 '23 at 07:44
  • Wait a minute :) `not found in … Test/Module/registration.php` the problem sounds more like the module itself isn’t declaring dependencies - this is just the first attempt to load it. – AD7six Feb 10 '23 at 09:26
  • What do you mean? I am new to magento. How should the module declare dependencies – footy Feb 10 '23 at 17:22
  • 1
    Please add `Test/Module/registration.php` to the question to clarify what code you are asking about. The question you just asked is not magneto specific – AD7six Feb 10 '23 at 18:25
  • 1
    I'm also with @AD7six, looks to me that `"files": [ "registration.php" ],` is triggering it and without showing the code of it, I couldn't make any good suggestions. – hakre Feb 11 '23 at 10:48
  • There's an implied misunderstanding here I think: static analysis tools typically have to load the code to analyze it (with [reflections](https://www.php.net/manual/en/book.reflection.php), in the case of [phpcs](https://github.com/squizlabs/PHP_CodeSniffer/search?q=reflection)), they aren't just looking at the code as text in isolation. As such, if there's an error loading/parsing the code (missing dependency, parse error) the only thing you'll see is a message pointing out that one fundamental problem. Alternatively: the code has to work for static analysis to be useful :) – AD7six Feb 11 '23 at 19:35

0 Answers0