1
|
<?php
|
2
|
/**
|
3
|
* Drupal_Sniffs_Semantics_InstallTSniff.
|
4
|
*
|
5
|
* PHP version 5
|
6
|
*
|
7
|
* @category PHP
|
8
|
* @package PHP_CodeSniffer
|
9
|
* @link http://pear.php.net/package/PHP_CodeSniffer
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Checks that t() and st() are not used in hook_install() and hook_requirements().
|
14
|
*
|
15
|
* @category PHP
|
16
|
* @package PHP_CodeSniffer
|
17
|
* @link http://pear.php.net/package/PHP_CodeSniffer
|
18
|
*/
|
19
|
class Drupal_Sniffs_Semantics_InstallTSniff implements PHP_CodeSniffer_Sniff
|
20
|
{
|
21
|
|
22
|
|
23
|
/**
|
24
|
* Returns an array of tokens this test wants to listen for.
|
25
|
*
|
26
|
* @return array
|
27
|
*/
|
28
|
public function register()
|
29
|
{
|
30
|
return array(T_STRING);
|
31
|
|
32
|
}//end register()
|
33
|
|
34
|
|
35
|
/**
|
36
|
* Processes this test, when one of its tokens is encountered.
|
37
|
*
|
38
|
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
45
|
{
|
46
|
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -7));
|
47
|
// Only check in *.install files.
|
48
|
if ($fileExtension !== 'install') {
|
49
|
return;
|
50
|
}
|
51
|
|
52
|
$fileName = substr(basename($phpcsFile->getFilename()), 0, -8);
|
53
|
$tokens = $phpcsFile->getTokens();
|
54
|
if ($tokens[$stackPtr]['content'] !== ($fileName.'_install')
|
55
|
&& $tokens[$stackPtr]['content'] !== ($fileName.'_requirements')
|
56
|
) {
|
57
|
return;
|
58
|
}
|
59
|
|
60
|
// Check if this is a function definition.
|
61
|
$function = $phpcsFile->findPrevious(
|
62
|
PHP_CodeSniffer_Tokens::$emptyTokens,
|
63
|
($stackPtr - 1),
|
64
|
null,
|
65
|
true
|
66
|
);
|
67
|
if ($tokens[$function]['code'] !== T_FUNCTION) {
|
68
|
return;
|
69
|
}
|
70
|
|
71
|
// Search in the function body for t() calls.
|
72
|
$string = $phpcsFile->findNext(
|
73
|
T_STRING,
|
74
|
$tokens[$function]['scope_opener'],
|
75
|
$tokens[$function]['scope_closer']
|
76
|
);
|
77
|
while ($string !== false) {
|
78
|
if ($tokens[$string]['content'] === 't' || $tokens[$string]['content'] === 'st') {
|
79
|
$opener = $phpcsFile->findNext(
|
80
|
PHP_CodeSniffer_Tokens::$emptyTokens,
|
81
|
($string + 1),
|
82
|
null,
|
83
|
true
|
84
|
);
|
85
|
if ($opener !== false
|
86
|
&& $tokens[$opener]['code'] === T_OPEN_PARENTHESIS
|
87
|
) {
|
88
|
$error = 'Do not use t() or st() in installation phase hooks, use $t = get_t() to retrieve the appropriate localization function name';
|
89
|
$phpcsFile->addError($error, $string, 'TranslationFound');
|
90
|
}
|
91
|
}
|
92
|
|
93
|
$string = $phpcsFile->findNext(
|
94
|
T_STRING,
|
95
|
($string + 1),
|
96
|
$tokens[$function]['scope_closer']
|
97
|
);
|
98
|
}//end while
|
99
|
|
100
|
}//end process()
|
101
|
|
102
|
|
103
|
}//end class
|
104
|
|
105
|
?>
|