From 9184390f19e33bb47ef81d7323059d2ba720a051 Mon Sep 17 00:00:00 2001 From: Arkener Date: Sat, 7 Mar 2020 13:35:22 +0100 Subject: [PATCH] feat(OptionsT): Add short array support --- .../Sniffs/General/OptionsTSniff.php | 26 ++++++++++++++----- .../Test/General/OptionsTUnitTest.inc | 24 +++++++++++++++++ .../Test/General/OptionsTUnitTest.php | 1 + 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php b/coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php index e4db6162..09fdae7c 100644 --- a/coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php +++ b/coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php @@ -59,24 +59,38 @@ public function process(File $phpcsFile, $stackPtr) // Cut out all the white space. $arrayString = preg_replace('/\s+/', '', $arrayString); - if (strpos($arrayString, '=>array(') !== 0 && strpos($arrayString, ']=array(') !== 0) { + if (strpos($arrayString, '=>array(') !== 0 + && strpos($arrayString, ']=array(') !== 0 + && strpos($arrayString, '=>[') !== 0 + && strpos($arrayString, ']=[') !== 0 + ) { return; } // We only search within the #options array. - $arrayToken = $phpcsFile->findNext(T_ARRAY, ($stackPtr + 1)); - $statementEnd = $tokens[$arrayToken]['parenthesis_closer']; + $arrayToken = $phpcsFile->findNext([T_ARRAY, T_OPEN_SHORT_ARRAY], ($stackPtr + 1)); $nestedParenthesis = []; if (isset($tokens[$arrayToken]['nested_parenthesis']) === true) { $nestedParenthesis = $tokens[$arrayToken]['nested_parenthesis']; } - $nestedParenthesis[$tokens[$arrayToken]['parenthesis_opener']] = $tokens[$arrayToken]['parenthesis_closer']; + 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']; + } // Go through the array by examining stuff after "=>". - $arrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($stackPtr + 1), $statementEnd, false, null, true); + $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']; + } + // We are only interested in string literals that are not numbers // and more than 3 characters long. if ($tokens[$arrayValue]['code'] === T_CONSTANT_ENCAPSED_STRING @@ -84,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr) && strlen($tokens[$arrayValue]['content']) > 5 // Make sure that we don't check stuff in nested arrays within // t() for example. - && $tokens[$arrayValue]['nested_parenthesis'] === $nestedParenthesis + && $valueNestedParenthesis === $nestedParenthesis ) { // We need to make sure that the string is the one and only part // of the array value. diff --git a/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.inc b/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.inc index 972c322d..57145507 100644 --- a/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.inc +++ b/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.inc @@ -32,3 +32,27 @@ $form['display']['status'] = [ '0' => t('Disabled', array(), array('context' => 'test')), ), ]; + +$form['amount'] = [ + '#type' => 'select', + '#title' => t('Amount'), + '#options' => [ + '1' => '1', + '2' => '2', + '3' => '3', + '4' => '4', + '500' => '500', + 'a' => 'a', + 'abc' => 'abc', + 'four' => 'four', + ], +]; + +$form['display']['hide_thumbnail'] = [ + '#title' => t('Hide Thumbnail', [], ['context' => 'test']), + '#type' => 'radios', + '#options' => [ + '1' => t('Yes', [], ['context' => 'test']), + '0' => t('No', [], ['context' => 'test']), + ], +]; diff --git a/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.php b/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.php index a96e1e98..33850930 100644 --- a/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.php +++ b/coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.php @@ -46,6 +46,7 @@ protected function getWarningList(string $testFile): array return [ 14 => 1, 31 => 1, + 47 => 1, ]; }//end getWarningList()