PHPCSStandards/PHPCSUtils

PHPCSUtils causes `ERROR: No sniffs were registered`

Closed this issue ยท 7 comments

As of squizlabs/PHP_CodeSniffer@7df3c33 PHPCS throws an expectation No sniffs were registered if there are no sniffs registered.

Since PHPCSUtils is included as standard without sniffs, it throws an exception on exec phpcs --standard=PHPCSUtils -es.

A possible solution might be to include an empty sniff to avoid this.

DummySniff
<?php
/**
 * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
 *
 * @package   PHPCSUtils
 * @copyright 2019-2020 PHPCSUtils Contributors
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
 * @link      https://github.com/PHPCSStandards/PHPCSUtils
 */

namespace PHPCSUtils\Sniffs\Load;

/**
 * Dummy Sniff.
 *
 * This sniff doesn't do anything. It's just here to avoid PHPCS exception.
 *
 * @since 1.0.0
 */
class UtilsLoadedSniff
{

    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return [];
    }

    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token in
     *                                               the stack passed in $tokens.
     *
     * @return void
     */
    public function process($phpcsFile, $stackPtr)
    {
    }
}

ping @jrfnl @gsherwood

jrfnl commented

@thelovekesh Why are you running phpcs --standard=PHPCSUtils ? I mean, I'd like to understand your use-case here as in my mind, there is none.

PHPCSUtils is not a standard to be run by end-users. It is a set of helper functions for external PHPCS standards.

If this is caused by PHPCSUtils being listed as a "standard" via phpcs -i, there is already an issue open about that in the PHPCS repo: squizlabs/PHP_CodeSniffer#2760

jrfnl commented

A possible solution might be to include an empty sniff to avoid this.

Note: I will not include an empty sniff.

The error/exception thrown by PHPCS is there for a reason and is pointing out something helpful, i.e. the scan being done is useless as-is as no sniffs are registered. This should be solved by whomever is running the scan, not in PHPCSUtils.

Why are you running phpcs --standard=PHPCSUtils ? I mean, I'd like to understand your use-case here as in my mind, there is none.

@jrfnl You can find one here. According to this, all sniffs are get listed and then revalidated if there are any duplicate or obsolete sniffs.

I will try to handle it on upstream but removing the PHPCSUtils from phpcs -i per squizlabs/PHP_CodeSniffer#2760 doesn't seems a great solution either as the user will not be able to learn if PHPCSUtils is loaded.

jrfnl commented

Why are you running phpcs --standard=PHPCSUtils ? I mean, I'd like to understand your use-case here as in my mind, there is none.

@jrfnl You can find one here. According to this, all sniffs are get listed and then revalidated if there are any duplicate or obsolete sniffs.

Que ? I still don't understand why that is done or even why that would ever be useful ? - I mean, rulesets will often include the same sniff twice, once from within a standard and once to add custom properties, so it just doesn't sound right to begin with, but then I may not understand correctly what that custom program really does (and I am resisting the temptation to look into it more deeply as I'm officially on holiday).

I will try to handle it on upstream

And that's how it should be. If you need help getting that sorted, let me know and I can possibly help you with that once I'm back from my break.

but removing the PHPCSUtils from phpcs -i per squizlabs/PHP_CodeSniffer#2760 doesn't seems a great solution either as the user will not be able to learn if PHPCSUtils is loaded.

Agreed, see my latest comment in that thread.

Thanks for catching up. Will add you to the upstream conversation once I start one.

I'm officially on holiday

I know from Twitter ๐Ÿ˜„ ; Enjoy the break ๐ŸŽ‰ !

Sorry for the trouble. I found a flag to ignore standards without sniffs on the tools which I am using.

I am closing this and will continue the discussion on squizlabs/PHP_CodeSniffer#2760

jrfnl commented

@thelovekesh Glad to hear you found a good solution.