1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Thomas Ernest <[email protected]> |
10
|
|
|
* @copyright 2011 Thomas Ernest |
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff. |
17
|
|
|
* |
18
|
|
|
* Tests that the file name matchs the name of the class that it contains in lower case. |
19
|
|
|
* |
20
|
|
|
* @category PHP |
21
|
|
|
* @package PHP_CodeSniffer |
22
|
|
|
* @author Thomas Ernest <[email protected]> |
23
|
|
|
* @copyright 2011 Thomas Ernest |
24
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
25
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace CodeIgniter\Sniffs\NamingConventions; |
29
|
|
|
|
30
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
31
|
|
|
use PHP_CodeSniffer\Files\File; |
32
|
|
|
|
33
|
|
|
class ValidFileNameSniff implements Sniff |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Returns an array of tokens this test wants to listen for. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function register() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
|
|
T_CLASS, |
44
|
|
|
T_INTERFACE, |
45
|
|
|
); |
46
|
|
|
}//end register() |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Processes this test, when one of its tokens is encountered. |
51
|
|
|
* |
52
|
|
|
* @param File $phpcsFile The file being scanned. |
53
|
|
|
* @param int $stackPtr The position of the current token in the |
54
|
|
|
* stack passed in $tokens. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function process(File $phpcsFile, $stackPtr) |
59
|
|
|
{ |
60
|
|
|
$tokens = $phpcsFile->getTokens(); |
61
|
|
|
// computes the expected filename based on the name of the class or interface that it contains. |
62
|
|
|
$decNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr); |
63
|
|
|
$decName = $tokens[$decNamePtr]['content']; |
64
|
|
|
$expectedFileName = strtolower($decName); |
65
|
|
|
// extracts filename without extension from its path. |
66
|
|
|
$fullPath = $phpcsFile->getFilename(); |
67
|
|
|
$fileNameAndExt = basename($fullPath); |
68
|
|
|
$fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.')); |
69
|
|
|
|
70
|
|
|
if ($expectedFileName !== $fileName) { |
71
|
|
|
$errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.'; |
|
|
|
|
72
|
|
|
$errorMessage = sprintf( |
73
|
|
|
$errorTemplate, |
74
|
|
|
$fileName, |
75
|
|
|
strtolower($tokens[$stackPtr]['content']), // class or interface |
|
|
|
|
76
|
|
|
$decName, |
77
|
|
|
$expectedFileName |
78
|
|
|
); |
79
|
|
|
$phpcsFile->addError($errorMessage, 0); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
}//end process() |
82
|
|
|
}//end class |
83
|
|
|
|
84
|
|
|
?> |
|
|
|
|
85
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.