|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Squiz_Sniffs_Classes_ClassFileNameSniff. |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category PHP |
| 8 | + * @package PHP_CodeSniffer |
| 9 | + * @author Greg Sherwood <gsherwood@squiz.net> |
| 10 | + * @author Marc McIntyre <mmcintyre@squiz.net> |
| 11 | + * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
| 12 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 13 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 14 | + */ |
| 15 | +namespace PSR2R\Sniffs\Classes; |
| 16 | + |
| 17 | +use PHP_CodeSniffer_File; |
| 18 | +use PSR2R\Tools\AbstractSniff; |
| 19 | + |
| 20 | +/** |
| 21 | + * Squiz_Sniffs_Classes_ClassFileNameSniff. |
| 22 | + * |
| 23 | + * Tests that the file name and the name of the class contained within the file |
| 24 | + * match. |
| 25 | + * |
| 26 | + * @author Greg Sherwood <gsherwood@squiz.net> |
| 27 | + * @author Marc McIntyre <mmcintyre@squiz.net> |
| 28 | + * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
| 29 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 30 | + * @version Release: @package_version@ |
| 31 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 32 | + */ |
| 33 | +class ClassFileNameSniff extends AbstractSniff { |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns an array of tokens this test wants to listen for. |
| 37 | + * |
| 38 | + * @return array |
| 39 | + */ |
| 40 | + public function register() { |
| 41 | + return [ |
| 42 | + T_CLASS, |
| 43 | + T_INTERFACE, |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Processes this test, when one of its tokens is encountered. |
| 49 | + * |
| 50 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 51 | + * @param int $stackPtr The position of the current token in |
| 52 | + * the stack passed in $tokens. |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
| 57 | + $fullPath = basename($phpcsFile->getFilename()); |
| 58 | + $fileName = substr($fullPath, 0, strrpos($fullPath, '.')); |
| 59 | + if ($fileName === '') { |
| 60 | + // No filename probably means STDIN, so we can't do this check. |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $tokens = $phpcsFile->getTokens(); |
| 65 | + |
| 66 | + $previous = $phpcsFile->findPrevious([T_CLASS, T_INTERFACE], $stackPtr - 1); |
| 67 | + if ($previous) { |
| 68 | + // Probably more than a single declaration per file, we only check first one then. |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $decName = $phpcsFile->findNext(T_STRING, $stackPtr); |
| 73 | + |
| 74 | + if ($tokens[$decName]['content'] !== $fileName) { |
| 75 | + $error = '%s name doesn\'t match filename; expected "%s %s"'; |
| 76 | + $data = [ |
| 77 | + ucfirst($tokens[$stackPtr]['content']), |
| 78 | + $tokens[$stackPtr]['content'], |
| 79 | + $fileName, |
| 80 | + ]; |
| 81 | + $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data); |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments