• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php
1
<?php
2
/**
3
 * Tests that the file name and the name of the class contained within the file match.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class ClassFileNameSniff implements Sniff
17
{
18

19

20
    /**
21
     * Returns an array of tokens this test wants to listen for.
22
     *
23
     * @return array<int|string>
24
     */
25
    public function register()
3✔
26
    {
27
        $targets = Tokens::OO_SCOPE_TOKENS;
3✔
28
        unset($targets[T_ANON_CLASS]);
3✔
29

30
        return $targets;
3✔
31
    }
32

33

34
    /**
35
     * Processes this test, when one of its tokens is encountered.
36
     *
37
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
     * @param int                         $stackPtr  The position of the current token in
39
     *                                               the stack passed in $tokens.
40
     *
41
     * @return int|void Integer stack pointer to skip forward or void to continue
42
     *                  normal file processing.
43
     */
44
    public function process(File $phpcsFile, int $stackPtr)
3✔
45
    {
46
        $fullPath = $phpcsFile->getFilename();
3✔
47
        if ($fullPath === 'STDIN') {
3✔
UNCOV
48
            return $phpcsFile->numTokens;
×
49
        }
50

51
        $fileName  = basename($fullPath);
3✔
52
        $fileNoExt = substr($fileName, 0, strrpos($fileName, '.'));
3✔
53
        $extension = substr($fileName, (strrpos($fileName, '.') + 1));
3✔
54

55
        $tokens = $phpcsFile->getTokens();
3✔
56
        $ooName = $phpcsFile->getDeclarationName($stackPtr);
3✔
57
        if ($ooName === '') {
3✔
58
            // Probably parse error/live coding.
59
            return;
3✔
60
        }
61

62
        if ($ooName !== $fileNoExt) {
3✔
63
            $error = 'Filename doesn\'t match %s name; expected file name "%s"';
3✔
64
            $data  = [
2✔
65
                $tokens[$stackPtr]['content'],
3✔
66
                $ooName . '.' . $extension,
3✔
67
            ];
2✔
68
            $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
3✔
69
        }
70
    }
1✔
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc