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