Index: contrib/phpcs/Observium/Sniffs/PHP/ListDeclarationSniff.php =================================================================== --- contrib/phpcs/Observium/Sniffs/PHP/ListDeclarationSniff.php (revision 0) +++ contrib/phpcs/Observium/Sniffs/PHP/ListDeclarationSniff.php (revision 0) @@ -0,0 +1,144 @@ +getTokens(); + + $listStart = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false); + $listEnd = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr, null, false); + + $keywordStart = $tokens[$stackPtr]['column']; + + if ($listStart !== ($stackPtr + 1)) { + $error = 'There must be no space between the List keyword and the opening parenthesis'; + $phpcsFile->addError($error, $stackPtr, 'SpaceAfterKeyword'); + } + + if ($tokens[$listStart + 1]['code'] === T_WHITESPACE && $tokens[$listStart + 1]['content'] !== $phpcsFile->eolChar) { + $error = 'There must be no space after the opening parenthesis of a List statement.'; + $phpcsFile->addError($error, $stackPtr, 'SpaceAfterParenthesis'); + } + + if ($tokens[$listStart]['line'] === $tokens[$listEnd]['line']) { + // Single line list. + // Check if there are multiple values. If so, then it has to be multiple lines + // unless it is contained inside a function call or condition. + $valueCount = 0; + $commas = array(); + for ($i = ($listStart + 1); $i < $listEnd; $i++) { + // Skip bracketed statements, like function calls. + if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { + $i = $tokens[$i]['parenthesis_closer']; + continue; + } + + if ($tokens[$i]['code'] === T_COMMA) { + $valueCount++; + $commas[] = $i; + } + } + + // Validate argument spacing + if ($valueCount > 0) { + $conditionCheck = $phpcsFile->findPrevious(array(T_OPEN_PARENTHESIS, T_SEMICOLON), ($stackPtr - 1), null, false); + + // We have a multiple value list that is inside a condition or + // function. Check its spacing is correct. + foreach ($commas as $comma) { + // Skip consecutive commas and commas at the end of the list + if (in_array($tokens[($comma + 1)]['code'], array(T_COMMA, T_CLOSE_PARENTHESIS))) { + continue; + } + if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) { + $content = $tokens[($comma + 1)]['content']; + $error = 'Expected 1 space between comma and "%s"; 0 found'; + $data = array($content); + $phpcsFile->addError($error, $comma, 'NoSpaceAfterComma', $data); + } else { + $spaceLength = strlen($tokens[($comma + 1)]['content']); + if ($spaceLength !== 1) { + $content = $tokens[($comma + 2)]['content']; + $error = 'Expected 1 space between comma and "%s"; %s found'; + $data = array( + $content, + $spaceLength, + ); + $phpcsFile->addError($error, $comma, 'SpaceAfterComma', $data); + } + } + + if ($tokens[($comma - 1)]['code'] === T_WHITESPACE) { + $content = $tokens[($comma - 2)]['content']; + $spaceLength = strlen($tokens[($comma - 1)]['content']); + $error = 'Expected 0 spaces between "%s" and comma; %s found'; + $data = array( + $content, + $spaceLength, + ); + $phpcsFile->addError($error, $comma, 'SpaceBeforeComma', $data); + } + }//end foreach + }//end if + + // Validate closing bracket + if ($tokens[$listEnd - 1]['code'] === T_WHITESPACE) { + $error = 'There must be no space before the closing parenthesis of a List statement.'; + $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeParenthesis'); + } + + return; + }//end if + + // Multi-line + $nextToken = $stackPtr; + $lastComma = $stackPtr; + + while(($nextToken = $phpcsFile->findNext(T_COMMA, ($nextToken + 1), $listEnd)) !== false) { + #echo("content = " . $tokens[$nextToken]['type'] . "\n"); + if ($tokens[$nextToken - 1]['code'] === T_WHITESPACE) { + $content = $tokens[$nextToken - 2]['content']; + $spaceLength = strlen($tokens[($nextToken - 1)]['content']); + $error = 'Expected 0 spaces between "%s" and comma; %s found'; + $data = array( + $content, + $spaceLength, + ); + $phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma', $data); + } + } + }//end process() + +}//end class + +?>