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