-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Custom rule must end in "Sniff"?? #2481
Comments
You missed the first line of the tutorial:
Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial Basically, even though the format of the XML file is basically the same, there is a big difference between a custom ruleset, i.e. a A custom ruleset can include existing sniffs from registered standards, include other standards, include other ruleset files etc. It's a configuration file. An (external) standard can do all that + introduce new sniffs. Standards have to be registered with PHPCS though to be recognized. So, let's take a step back.
If you follow the above steps, I expect you'll have more success. Take note of the following:
Hope this helps. Let me know if I can clarify further. |
Exceptional description. Think this could replace the docs that are there. Thank you so much! |
Glad it helped! |
PHPCS has specific naming and directory layout requirements for external standards which the `Security` standard did not comply with. While things sort of worked with the symlink hack, the net effect was: - The PHPCS autoloader did not work. - None of the PHPCS ruleset configuration options worked as PHPCS could not match sniffs to files. - Some sniffs would never load. This PR fixes this by: 1. Setting the base namespace to `PHPCS_SecurityAudit\Security` and annotating this in the `ruleset.xml` file in the correct manner. 2. Fixing all namespaces and uses thereof throughout the codebase. 3. Fixing the `Drupal8/Utils.php` file which was missing the namespace and was still referring to an out-of-date class name to extend. 4. Fixing the namespace names and file names of the CVE sniffs. - The namespace of a sniff has to reflect its path in the standard. - The file name has to reflect the name of the sniff. 5. Fixing the names of the CVE sniffs in the example rulesets 6. Removing the symlink file and all references to it. Instead `require` the [DealerDirect Composer PHPCS plugin](https://github.com/Dealerdirect/phpcodesniffer-composer-installer) which will sort out the `installed_paths` for PHPCS . 7. Setting the minimum PHPCS version to `3.0.2` as prior to that external standards weren't fully supported in the 3.x branch. 8. Removing the `autoload` section in `composer.json`. This is no longer needed and in certain situations can cause conflicts/fatal errors. References: * https://github.com/squizlabs/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial * https://github.com/squizlabs/PHP_CodeSniffer/wiki/Version-3.0-Upgrade-Guide * squizlabs/PHP_CodeSniffer#2481 (comment) * squizlabs/PHP_CodeSniffer#2606 * squizlabs/PHP_CodeSniffer#1469 * https://github.com/Dealerdirect/phpcodesniffer-composer-installer Fixes 47
PHPCS has specific naming and directory layout requirements for external standards which the `Security` standard did not comply with. While things sort of worked with the symlink hack, the net effect was: - The PHPCS autoloader did not work. - None of the PHPCS ruleset configuration options worked as PHPCS could not match sniffs to files. - Some sniffs would never load. This PR fixes this by: 1. Setting the base namespace to `PHPCS_SecurityAudit\Security` and annotating this in the `ruleset.xml` file in the correct manner. 2. Fixing all namespaces and uses thereof throughout the codebase. 3. Fixing the `Drupal8/Utils.php` file which was missing the namespace and was still referring to an out-of-date class name to extend. 4. Fixing the namespace names and file names of the CVE sniffs. - The namespace of a sniff has to reflect its path in the standard. - The file name has to reflect the name of the sniff. 5. Fixing the names of the CVE sniffs in the example rulesets 6. Removing the symlink file and all references to it. Instead `require` the [DealerDirect Composer PHPCS plugin](https://github.com/Dealerdirect/phpcodesniffer-composer-installer) which will sort out the `installed_paths` for PHPCS . 7. Setting the minimum PHPCS version to `3.0.2` as prior to that external standards weren't fully supported in the 3.x branch. 8. Removing the `autoload` section in `composer.json`. This is no longer needed and in certain situations can cause conflicts/fatal errors. References: * https://github.com/squizlabs/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial * https://github.com/squizlabs/PHP_CodeSniffer/wiki/Version-3.0-Upgrade-Guide * squizlabs/PHP_CodeSniffer#2481 (comment) * squizlabs/PHP_CodeSniffer#2606 * squizlabs/PHP_CodeSniffer#1469 * https://github.com/Dealerdirect/phpcodesniffer-composer-installer Fixes 47
Never used this library before; so, sorry for the bumps.
Created ruleset:
Worked like a charm. Created custom rule per documentation, I think:
Updated ruleset:
Received error:
Tried just folder in ruleset:
Received error:
Changed file name to include the suffix "Sniff" (b/c maybe??):
Running phpcs resulted in no error; however, the custom sniff is ignored.
Very confused on what I'm doing wrong here. Pretty sure I'm following the docs but they're pretty verbose for me there might be a key requirement buried in there I'm missing.
Step 1 (after getting PHPCS): Create
.phpcs.xml
(see above).Step 2: Create sniffer class file that implements
Sniff
interface:Step 3: Point the
.phpcs.xml
to the newly created class.Step 4: Profit??
Any assistance would be greatly appreciated.
The text was updated successfully, but these errors were encountered: