awesomemotive / wpforms-phpcs

PHP Coding Standards used by the WPForms team.
https://wpforms.com
GNU General Public License v2.0
11 stars 1 forks source link

Exclusions to some generic sniffs #13

Closed ihorvorotnov closed 2 years ago

ihorvorotnov commented 3 years ago

Some sniffs are triggered in specific cases that should be excluded. I'll update the list during further testing.

More exclusions may be added later...

wppunk commented 3 years ago

The fix can be wrapper for the Generic.Commenting.DocComment sniff which skip the /** @lang CSS */ PHPDocs.

Quick solution:

<?php

namespace WPForms\Sniffs\Generic\Commenting\DocComment;

use PHP_CodeSniffer\Files\File;

class DocCommentSniff extends \PHP_CodeSniffer\Standards\Generic\Sniffs\Commenting\DocCommentSniff {

    public function process( File $phpcsFile, $stackPtr ) {

        $tokens = $phpcsFile->getTokens();

        if (
            isset( $tokens[ $stackPtr ]['comment_closer'] ) === false
            || (
                $tokens[ $tokens[ $stackPtr ]['comment_closer'] ]['content'] === ''
                && $tokens[ $stackPtr ]['comment_closer'] === ( $phpcsFile->numTokens - 1 )
            )
        ) {
            // Don't process an unfinished comment during live coding.
            return;
        }

        $commentEnd = $tokens[ $stackPtr ]['comment_closer'];

        $empty = [
            T_DOC_COMMENT_WHITESPACE,
            T_DOC_COMMENT_STAR,
        ];

        $short = $phpcsFile->findNext( $empty, ( $stackPtr + 1 ), $commentEnd, true );

        if ( $short && $tokens[ $short ]['code'] === T_DOC_COMMENT_TAG && $tokens[ $short ]['content'] === '@lang' ) {
            return;
        }

        parent::process( $phpcsFile, $stackPtr );
    }
}