Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@17
|
3 * \DrupalPractice\Sniffs\General\OptionsTSniff
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@17
|
10 namespace DrupalPractice\Sniffs\General;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
13 use PHP_CodeSniffer\Sniffs\Sniff;
|
Chris@17
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Checks that values in #otions form arrays are translated.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @category PHP
|
Chris@0
|
19 * @package PHP_CodeSniffer
|
Chris@0
|
20 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
21 */
|
Chris@17
|
22 class OptionsTSniff implements Sniff
|
Chris@0
|
23 {
|
Chris@0
|
24
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @return array
|
Chris@0
|
30 */
|
Chris@0
|
31 public function register()
|
Chris@0
|
32 {
|
Chris@0
|
33 return array(T_CONSTANT_ENCAPSED_STRING);
|
Chris@0
|
34
|
Chris@0
|
35 }//end register()
|
Chris@0
|
36
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
40 *
|
Chris@17
|
41 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
Chris@17
|
42 * @param int $stackPtr The position of the function
|
Chris@17
|
43 * name in the stack.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @return void
|
Chris@0
|
46 */
|
Chris@17
|
47 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
48 {
|
Chris@0
|
49 // Look for the string "#options".
|
Chris@0
|
50 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
51 if ($tokens[$stackPtr]['content'] !== '"#options"' && $tokens[$stackPtr]['content'] !== "'#options'") {
|
Chris@0
|
52 return;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 // Look for an opening array pattern that starts to define #options
|
Chris@0
|
56 // values.
|
Chris@0
|
57 $statementEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
|
Chris@0
|
58 $arrayString = $phpcsFile->getTokensAsString(($stackPtr + 1), ($statementEnd - $stackPtr));
|
Chris@0
|
59 // Cut out all the white space.
|
Chris@0
|
60 $arrayString = preg_replace('/\s+/', '', $arrayString);
|
Chris@0
|
61
|
Chris@0
|
62 if (strpos($arrayString, '=>array(') !== 0 && strpos($arrayString, ']=array(') !== 0) {
|
Chris@0
|
63 return;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 // We only search within the #options array.
|
Chris@0
|
67 $arrayToken = $phpcsFile->findNext(T_ARRAY, ($stackPtr + 1));
|
Chris@0
|
68 $statementEnd = $tokens[$arrayToken]['parenthesis_closer'];
|
Chris@0
|
69
|
Chris@0
|
70 // Go through the array by examining stuff after "=>".
|
Chris@0
|
71 $arrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($stackPtr + 1), $statementEnd, false, null, true);
|
Chris@0
|
72 while ($arrow !== false) {
|
Chris@0
|
73 $arrayValue = $phpcsFile->findNext(T_WHITESPACE, ($arrow + 1), $statementEnd, true);
|
Chris@0
|
74 // We are only interested in string literals that are not numbers
|
Chris@0
|
75 // and more than 3 characters long.
|
Chris@0
|
76 if ($tokens[$arrayValue]['code'] === T_CONSTANT_ENCAPSED_STRING
|
Chris@0
|
77 && is_numeric(substr($tokens[$arrayValue]['content'], 1, -1)) === false
|
Chris@0
|
78 && strlen($tokens[$arrayValue]['content']) > 5
|
Chris@0
|
79 ) {
|
Chris@0
|
80 // We need to make sure that the string is the one and only part
|
Chris@0
|
81 // of the array value.
|
Chris@0
|
82 $afterValue = $phpcsFile->findNext(T_WHITESPACE, ($arrayValue + 1), $statementEnd, true);
|
Chris@0
|
83 if ($tokens[$afterValue]['code'] === T_COMMA || $tokens[$afterValue]['code'] === T_CLOSE_PARENTHESIS) {
|
Chris@0
|
84 $warning = '#options values usually have to run through t() for translation';
|
Chris@0
|
85 $phpcsFile->addWarning($warning, $arrayValue, 'TforValue');
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 $arrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($arrow + 1), $statementEnd, false, null, true);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 }//end process()
|
Chris@0
|
93
|
Chris@0
|
94
|
Chris@0
|
95 }//end class
|