Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,46 @@ 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
&& is_numeric(substr($tokens[$arrayValue]['content'], 1, -1)) === false
&& 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.
Expand Down
24 changes: 24 additions & 0 deletions coder_sniffer/DrupalPractice/Test/General/OptionsTUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function getWarningList(string $testFile): array
return [
14 => 1,
31 => 1,
47 => 1,
];

}//end getWarningList()
Expand Down