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

PHPCSStandards / PHP_CodeSniffer / 13721685724

07 Mar 2025 01:26PM UTC coverage: 78.556%. Remained the same
13721685724

push

github

web-flow
Merge pull request #845 from PHPCSStandards/feature/squiz-classfilename-various-improvements

Squiz/ClassFileName: various improvements

15 of 16 new or added lines in 1 file covered. (93.75%)

24694 of 31435 relevant lines covered (78.56%)

66.41 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 void
43
     */
44
    public function process(File $phpcsFile, $stackPtr)
3✔
45
    {
46
        $fullPath = $phpcsFile->getFilename();
3✔
47
        if ($fullPath === 'STDIN') {
3✔
NEW
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 === null) {
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  = [
1✔
65
                $tokens[$stackPtr]['content'],
3✔
66
                $ooName.'.'.$extension,
3✔
67
            ];
2✔
68
            $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
3✔
69
        }
1✔
70

71
    }//end process()
2✔
72

73

74
}//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

© 2025 Coveralls, Inc