Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught InvalidArgumentException: The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: CompanyNameStandard\Sniffs\JsonSniff #675

Closed
2 of 4 tasks
williamdes opened this issue Nov 12, 2024 · 2 comments · Fixed by #676

Comments

@williamdes
Copy link

williamdes commented Nov 12, 2024

Describe the bug

After #524 I get the following error:

Fatal error: Uncaught InvalidArgumentException: The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: CompanyNameStandard\Sniffs\JsonSniff in /var/www/vendor/squizlabs/php_codesniffer/src/Util/Common.php:546
Stack trace:
#0 /var/www/vendor/squizlabs/php_codesniffer/src/Ruleset.php(1389): PHP_CodeSniffer\Util\Common::getSniffCode('CompanyNameStandar...')
#1 /var/www/vendor/squizlabs/php_codesniffer/src/Ruleset.php(234): PHP_CodeSniffer\Ruleset->populateTokenListeners()
#2 /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php(348): PHP_CodeSniffer\Ruleset->__construct(Object(PHP_CodeSniffer\Config))
#3 /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php(76): PHP_CodeSniffer\Runner->init()
#4 /var/www/vendor/squizlabs/php_codesniffer/bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
#5 /var/www/vendor/bin/phpcs(119): include('/var/www/vendor...')
#6 {main}

The rules are added to a normal phpcs ruleset

I can not figure out how to fix the current situation

Code sample

<?php

namespace CompanyNameStandard\Sniffs;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

class JsonSniff implements Sniff
{
    public function register()
    {
        return [];
//        return [T_INLINE_HTML];
    }

    public function process(File $file, $currentTokenIndex)
    {
    }

}

Custom ruleset

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Company"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">

    <file>.</file>
    <arg name="cache" value=".php_cs.cache" />
    <arg name="extensions" value="php,json" />

    <config name="installed_paths" value="vendor/slevomat/coding-standard"/>

    <rule ref="./CompanyStandard/Sniffs/JsonSniff.php" />
    <rule ref="./CompanyStandard/Sniffs/TranslationSniff.php" />

</ruleset>

To reproduce

Use the ruleset and add the file at the rule location

Expected behavior

Work fine like in the previous version

Versions (please complete the following information)

Operating System Linux Alpine 3.20
PHP version 8.2
PHP_CodeSniffer version 3.11.0
Standard PSR2 + PSR12 + Squiz + custom
Install type Composer

Additional context

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@jrfnl
Copy link
Member

jrfnl commented Nov 12, 2024

@williamdes Thank you for reporting this. I kind of was afraid something might break with the change from #524.

I can not figure out how to fix the current situation

The short answer is that the sniffs do not comply with the directory structure and naming conventions which sniffs must follow, as outlined in: https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial

The sniffs are missing the [Category] directory below the Sniffs directory. Sniffs should not be placed directly in a Sniffs directory.

You can also see this if you run the sniffs over code which triggers an error (I've just hacked it a little to show this, but do test this yourself with PHPCS 3.10.3).

image

While the current sniffs may "work", certain standard PHPCS features will not work intuitively with a setup like this, and may not work at all. I'm talking about features like:

  • Selectively excluding error codes from the sniff by name
  • Setting properties on the sniff by sniff name
  • Use of inline ignore annotations

Fixing the sniffs to follow the required naming conventions will fix the exception and make the sniffs compatible with the above mentioned PHPCS features too.

Having said that, I agree that if the change made in #524 breaks such unconventional setups, this change needs to be (partially) reverted.
If needs be, the problematic part of the change could be moved to the next PHPCS major.

I've now opened PR #676 to fix this. Testing would be much appreciated.


As a side-note (unrelated to the current issue): you may want to clean up the ruleset header as it references an XSD schema of a completely unrelated project.... (PHP Mess Detector)

You should change it to:

<ruleset name="Company"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

@williamdes
Copy link
Author

Thank you @jrfnl !

As a side-note (unrelated to the current issue): you may want to clean up the ruleset header as it references an XSD schema of a completely unrelated project.... (PHP Mess Detector)

Indeed, good catch.

I've now opened PR #676 to fix this. Testing would be much appreciated.

Since another project tested it and I did review it the changes look okay.

If needs be, the problematic part of the change could be moved to the next PHPCS major.

True, this is a breaking change
Maybe a warning with documentation would help.

I applied this patch and it all works fine now:

diff --git a/CompanyStandard/Sniffs/JsonSniff.php b/CompanyStandard/Sniffs/Schemas/CheckJsonIsSafeSniff.php
similarity index 99%
rename from CompanyStandard/Sniffs/JsonSniff.php
rename to CompanyStandard/Sniffs/Schemas/CheckJsonIsSafeSniff.php
index a6b0f0872..013818cdf 100644
--- a/CompanyStandard/Sniffs/JsonSniff.php
+++ b/CompanyStandard/Sniffs/Schemas/CheckJsonIsSafeSniff.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace CompanyStandard\Sniffs;
+namespace CompanyStandard\Sniffs\Schemas;
 
 use PHP_CodeSniffer\Sniffs\Sniff;
 use PHP_CodeSniffer\Files\File;
diff --git a/CompanyStandard/Sniffs/TranslationSniff.php b/CompanyStandard/Sniffs/Translations/DisallowUnusedTranslationSniff.php
similarity index 98%
rename from CompanyStandard/Sniffs/TranslationSniff.php
rename to CompanyStandard/Sniffs/Translations/DisallowUnusedTranslationSniff.php
index c4b76f1f9..93fbc3a99 100644
--- a/CompanyStandard/Sniffs/TranslationSniff.php
+++ b/CompanyStandard/Sniffs/Translations/DisallowUnusedTranslationSniff.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace CompanyStandard\Sniffs;
+namespace CompanyStandard\Sniffs\Translations;
 
 use PHP_CodeSniffer\Sniffs\Sniff;
 use PHP_CodeSniffer\Files\File;
diff --git a/phpcs.xml b/phpcs.xml
index e9de574d7..cc1654a85 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -1,9 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<ruleset name="Company"
-    xmlns="http://pmd.sf.net/ruleset/1.0.0"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
-    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
+<ruleset name="Company">
     <!-- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
     <description>Company PHP_CodeSniffer custom coding standard</description>
 
@@ -27,8 +23,8 @@
 
     <config name="installed_paths" value="vendor/slevomat/coding-standard"/>
 
-    <rule ref="./CompanyStandard/Sniffs/JsonSniff.php" />
-    <rule ref="./CompanyStandard/Sniffs/TranslationSniff.php" />
+    <rule ref="./CompanyStandard/Sniffs/Schemas/CheckJsonIsSafeSniff.php" />
+    <rule ref="./CompanyStandard/Sniffs/Translations/DisallowUnusedTranslationSniff.php" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants