View source  
  <?php
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class OptionsTSniff implements Sniff {
  
  public function register() {
    return [
      T_CONSTANT_ENCAPSED_STRING,
    ];
  }
  
  
  public function process(File $phpcsFile, $stackPtr) {
    
    $tokens = $phpcsFile
      ->getTokens();
    if ($tokens[$stackPtr]['content'] !== '"#options"' && $tokens[$stackPtr]['content'] !== "'#options'") {
      return;
    }
    
    $statementEnd = $phpcsFile
      ->findNext(T_SEMICOLON, $stackPtr + 1);
    $arrayString = $phpcsFile
      ->getTokensAsString($stackPtr + 1, $statementEnd - $stackPtr);
    
    $arrayString = preg_replace('/\\s+/', '', $arrayString);
    if (strpos($arrayString, '=>array(') !== 0 && strpos($arrayString, ']=array(') !== 0 && strpos($arrayString, '=>[') !== 0 && strpos($arrayString, ']=[') !== 0) {
      return;
    }
    
    $arrayToken = $phpcsFile
      ->findNext([
      T_ARRAY,
      T_OPEN_SHORT_ARRAY,
    ], $stackPtr + 1);
    $nestedParenthesis = [];
    if (isset($tokens[$arrayToken]['nested_parenthesis']) === true) {
      $nestedParenthesis = $tokens[$arrayToken]['nested_parenthesis'];
    }
    if ($tokens[$arrayToken]['code'] === T_ARRAY) {
      $statementEnd = $tokens[$arrayToken]['parenthesis_closer'];
      $nestedParenthesis[$tokens[$arrayToken]['parenthesis_opener']] = $tokens[$arrayToken]['parenthesis_closer'];
    }
    else {
      $statementEnd = $tokens[$arrayToken]['bracket_closer'];
    }
    
    $formElements = [
      "'checkboxes'",
      "'radios'",
      "'select'",
      "'tableselect'",
    ];
    
    $startArray = $phpcsFile
      ->findStartOfStatement($stackPtr, [
      T_DOUBLE_ARROW,
      T_OPEN_SHORT_ARRAY,
      T_OPEN_PARENTHESIS,
      T_COMMA,
    ]);
    
    $findType = $phpcsFile
      ->findNext(T_CONSTANT_ENCAPSED_STRING, $startArray + 1, $statementEnd, false, "'#type'");
    
    if ($findType === false) {
      return;
    }
    
    $valueType = $phpcsFile
      ->findNext(T_CONSTANT_ENCAPSED_STRING, $findType + 1, null, false);
    
    $arrow = $phpcsFile
      ->findNext(T_DOUBLE_ARROW, $arrayToken + 1, $statementEnd, false, null, true);
    while ($arrow !== false) {
      $arrayValue = $phpcsFile
        ->findNext(T_WHITESPACE, $arrow + 1, $statementEnd, true);
      $valueNestedParenthesis = [];
      if (isset($tokens[$arrayValue]['nested_parenthesis']) === true) {
        $valueNestedParenthesis = $tokens[$arrayValue]['nested_parenthesis'];
      }
      
      if ($tokens[$arrayValue]['code'] === T_CONSTANT_ENCAPSED_STRING && is_numeric(substr($tokens[$arrayValue]['content'], 1, -1)) === false && strlen($tokens[$arrayValue]['content']) > 5 && in_array($tokens[$valueType]['content'], $formElements) === true && $valueNestedParenthesis === $nestedParenthesis) {
        
        $afterValue = $phpcsFile
          ->findNext(T_WHITESPACE, $arrayValue + 1, $statementEnd, true);
        if ($tokens[$afterValue]['code'] === T_COMMA || $tokens[$afterValue]['code'] === T_CLOSE_PARENTHESIS) {
          $warning = '#options values usually have to run through t() for translation';
          $phpcsFile
            ->addWarning($warning, $arrayValue, 'TforValue');
        }
      }
      $arrow = $phpcsFile
        ->findNext(T_DOUBLE_ARROW, $arrow + 1, $statementEnd, false, null, true);
    }
    
  }
}