Unverified Commit de37d530 authored by Mitch Portier's avatar Mitch Portier Committed by GitHub
Browse files

feat(ClassComment): Add a sniff to check for "Class Example" doc comments (#3156856 by Arkener)

parent 73756944
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -123,6 +123,31 @@ class ClassCommentSniff implements Sniff
            }
        }

        $comment = [];
        for ($i = $start; $i < $commentEnd; $i++) {
            if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) {
                break;
            }

            if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
                $comment[] = $tokens[$i]['content'];
            }
        }

        $words = explode(' ', implode(' ', $comment));
        if (count($words) <= 2) {
            $className = $phpcsFile->getDeclarationName($stackPtr);

            foreach ($words as $word) {
                // Check if the comment contains the class name.
                if (strpos($word, $className) !== false) {
                    $error = 'The class short comment should describe what the class does and not simply repeat the class name';
                    $phpcsFile->addWarning($error, $commentEnd, 'Short');
                    break;
                }
            }
        }

    }//end process()


+14 −0
Original line number Diff line number Diff line
@@ -8,3 +8,17 @@
trait Bingo {

}

/**
 * Trait Bongo
 */
trait Bongo {

}

/**
 * Trait Bango.
 */
trait Bango {

}
+14 −0
Original line number Diff line number Diff line
@@ -11,3 +11,17 @@
trait Bingo {

}

/**
 * Trait Bongo.
 */
trait Bongo {

}

/**
 * Trait Bango.
 */
trait Bango {

}
+4 −1
Original line number Diff line number Diff line
@@ -37,7 +37,10 @@ class ClassCommentUnitTest extends CoderSniffUnitTest
     */
    protected function getWarningList(string $testFile): array
    {
        return [];
        return [
            14 => 1,
            21 => 1,
        ];

    }//end getWarningList()