| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_PHP_LowercasePHPFunctionsSniff.
|
|
|
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 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: LowercasePHPFunctionsSniff.php 240382 2007-07-27 05:34:43Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_PHP_LowercasePHPFunctionsSniff.
|
|
|
19 |
*
|
|
|
20 |
* Ensures all calls to inbuilt PHP functions are lowercase.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_PHP_LowercasePHPFunctionsSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
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_ISSET,
|
|
|
44 |
T_ECHO,
|
|
|
45 |
T_PRINT,
|
|
|
46 |
T_RETURN,
|
|
|
47 |
T_BREAK,
|
|
|
48 |
T_CONTINUE,
|
|
|
49 |
T_EMPTY,
|
|
|
50 |
T_EVAL,
|
|
|
51 |
T_EXIT,
|
|
|
52 |
T_LIST,
|
|
|
53 |
T_UNSET,
|
|
|
54 |
T_INCLUDE,
|
|
|
55 |
T_INCLUDE_ONCE,
|
|
|
56 |
T_REQUIRE,
|
|
|
57 |
T_REQUIRE_ONCE,
|
|
|
58 |
T_NEW,
|
|
|
59 |
T_DECLARE,
|
|
|
60 |
T_STRING,
|
|
|
61 |
);
|
|
|
62 |
|
|
|
63 |
}//end register()
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Processes this test, when one of its tokens is encountered.
|
|
|
68 |
*
|
|
|
69 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
70 |
* @param int $stackPtr The position of the current token in
|
|
|
71 |
* the stack passed in $tokens.
|
|
|
72 |
*
|
|
|
73 |
* @return void
|
|
|
74 |
*/
|
|
|
75 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
76 |
{
|
|
|
77 |
$tokens = $phpcsFile->getTokens();
|
|
|
78 |
|
|
|
79 |
if ($tokens[$stackPtr]['code'] !== T_STRING) {
|
|
|
80 |
$content = $tokens[$stackPtr]['content'];
|
|
|
81 |
if ($content !== strtolower($content)) {
|
|
|
82 |
$type = strtoupper($content);
|
|
|
83 |
$expected = strtolower($content);
|
|
|
84 |
$error = "$type keyword must be lowercase; expected \"$expected\" but found \"$content\"";
|
|
|
85 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// Make sure this is a function call.
|
|
|
92 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
93 |
if ($next === false) {
|
|
|
94 |
// Not a function call.
|
|
|
95 |
return;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
|
|
|
99 |
// Not a function call.
|
|
|
100 |
return;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
|
|
104 |
if ($tokens[$prev]['code'] === T_FUNCTION) {
|
|
|
105 |
// Function declaration, not a function call.
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR) {
|
|
|
110 |
// Not an inbuilt function.
|
|
|
111 |
return;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if ($tokens[$prev]['code'] === T_DOUBLE_COLON) {
|
|
|
115 |
// Not an inbuilt function.
|
|
|
116 |
return;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
// Make sure it is an inbuilt PHP function.
|
|
|
120 |
// PHP_CodeSniffer doesn't include/require any files, so no
|
|
|
121 |
// user defined global functions can exist, except for
|
|
|
122 |
// PHP_CodeSniffer ones.
|
|
|
123 |
$content = $tokens[$stackPtr]['content'];
|
|
|
124 |
if (function_exists($content) === false) {
|
|
|
125 |
return;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if ($content !== strtolower($content)) {
|
|
|
129 |
$expected = strtolower($content);
|
|
|
130 |
$error = "Calls to inbuilt PHP functions must be lowercase; expected \"$expected\" but found \"$content\"";
|
|
|
131 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
}//end process()
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
}//end class
|
|
|
138 |
|
|
|
139 |
?>
|