Commit b688d841 authored by Klaus Purer's avatar Klaus Purer
Browse files

feat(ClassFileName): Add sniff to check that class name matches file name

parent 91cec40c
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
<?php
/**
 * Largely copied from
 * PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ClassFileNameSniff.
 *
 * Extended to support anonymous classes and Drupal core version.
 */

namespace Drupal\Sniffs\Classes;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use DrupalPractice\Project;

class ClassFileNameSniff implements Sniff
{


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

    }//end register()


    /**
     * 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 int
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        // This check only applies to Drupal 8+, in Drupal 7 we can have classes
        // in all kinds of files.
        if (Project::getCoreVersion($phpcsFile) < 8) {
            return ($phpcsFile->numTokens + 1);
        }

        $fullPath = basename($phpcsFile->getFilename());
        $fileName = substr($fullPath, 0, strrpos($fullPath, '.'));
        if ($fileName === '') {
            // No filename probably means STDIN, so we can't do this check.
            return ($phpcsFile->numTokens + 1);
        }

        $tokens  = $phpcsFile->getTokens();
        $decName = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);

        if ($tokens[$decName]['code'] === T_STRING
            && $tokens[$decName]['content'] !== $fileName
        ) {
            $error = '%s name doesn\'t match filename; expected "%s %s"';
            $data  = [
                ucfirst($tokens[$stackPtr]['content']),
                $tokens[$stackPtr]['content'],
                $fileName,
            ];
            $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
        }

        // Only check the first class in a file, we don't care about helper
        // classes in tests for example.
        return ($phpcsFile->numTokens + 1);

    }//end process()


}//end class
+65 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Sniffs\Classes;

use Drupal\Test\CoderSniffUnitTest;

class ClassFileNameUnitTest extends CoderSniffUnitTest
{


    /**
     * Returns the lines where errors should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array(int => int)
     */
    public function getErrorList($testFile=null)
    {
        switch ($testFile) {
        case 'ClassFileNameUnitTest.php':
            return [3 => 1];
        case 'class_fle_name_test.module':
            return [];
        }

    }//end getErrorList()


    /**
     * Returns the lines where warnings should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @return array(int => int)
     */
    public function getWarningList()
    {
        return [];

    }//end getWarningList()


    /**
     * Returns a list of test files that should be checked.
     *
     * @param string $testFileBase The base path that the unit tests files will have.
     *
     * @return array The list of test files.
     */
    protected function getTestFiles($testFileBase)
    {
        return [
            __DIR__.'/drupal8/ClassFileNameUnitTest.php',
            __DIR__.'/drupal7/class_fle_name_test.module',
        ];

    }//end getTestFiles()


}//end class
+5 −0
Original line number Diff line number Diff line
<?php

class OkName {

}
+1 −0
Original line number Diff line number Diff line
core = 7.x
+5 −0
Original line number Diff line number Diff line
<?php

class WrongName {

}
Loading