comparison vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/General/DescriptionTSniff.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_DescriptionTSniff
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Checks that string values for #description in render 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_DescriptionTSniff 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 "#description".
45 $tokens = $phpcsFile->getTokens();
46 if ($tokens[$stackPtr]['content'] !== '"#description"' && $tokens[$stackPtr]['content'] !== "'#description'") {
47 return;
48 }
49
50 // Look for an array pattern that starts to define #description values.
51 $statementEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
52 $arrayString = $phpcsFile->getTokensAsString(($stackPtr + 1), ($statementEnd - $stackPtr));
53 // Cut out all the white space.
54 $arrayString = preg_replace('/\s+/', '', $arrayString);
55
56 if (strpos($arrayString, '=>"') !== 0 && strpos($arrayString, ']="') !== 0
57 && strpos($arrayString, "=>'") !== 0 && strpos($arrayString, "]='") !== 0
58 ) {
59 return;
60 }
61
62 $stringToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 1));
63 $content = strip_tags($tokens[$stringToken]['content']);
64
65 if (strlen($content) > 5) {
66 $warning = '#description values usually have to run through t() for translation';
67 $phpcsFile->addWarning($warning, $stringToken);
68 }
69
70 }//end process()
71
72
73 }//end class