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

PHPCSStandards / PHP_CodeSniffer / 14023446859

23 Mar 2025 10:07PM UTC coverage: 78.708% (+0.2%) from 78.556%
14023446859

Pull #848

github

web-flow
Merge 4e45f1b53 into a4c38a9a5
Pull Request #848: [Docs] Add XML doc for Squiz.Commenting.BlockComment sniff

24845 of 31566 relevant lines covered (78.71%)

66.4 hits per line

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

95.83
/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::$ooScopeTokens;
3✔
28
        unset($targets[T_ANON_CLASS]);
3✔
29

30
        return $targets;
3✔
31

32
    }//end register()
33

34

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

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

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

63
        if ($ooName !== $fileNoExt) {
3✔
64
            $error = 'Filename doesn\'t match %s name; expected file name "%s"';
3✔
65
            $data  = [
1✔
66
                $tokens[$stackPtr]['content'],
3✔
67
                $ooName.'.'.$extension,
3✔
68
            ];
2✔
69
            $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
3✔
70
        }
1✔
71

72
    }//end process()
2✔
73

74

75
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc