diff --git a/Universal/Docs/WhiteSpace/FirstClassCallableSpacingStandard.xml b/Universal/Docs/WhiteSpace/FirstClassCallableSpacingStandard.xml new file mode 100644 index 0000000..e7714b1 --- /dev/null +++ b/Universal/Docs/WhiteSpace/FirstClassCallableSpacingStandard.xml @@ -0,0 +1,33 @@ + + + + + + + + ...); +array_map( + $this->method(...), + $array +); + ]]> + + + ... ); +array_map( + $this->method( + ... + ), + $array +); + ]]> + + + diff --git a/Universal/Sniffs/WhiteSpace/FirstClassCallableSpacingSniff.php b/Universal/Sniffs/WhiteSpace/FirstClassCallableSpacingSniff.php new file mode 100644 index 0000000..07f5df6 --- /dev/null +++ b/Universal/Sniffs/WhiteSpace/FirstClassCallableSpacingSniff.php @@ -0,0 +1,101 @@ + + */ + public function register() + { + return [\T_ELLIPSIS]; + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @since 1.5.0 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + // Verify this is an ellipsis for a first class callable. + $previousNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); + if ($tokens[$previousNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { + return; + } + + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_CLOSE_PARENTHESIS) { + return; + } + + $spacing = (int) $this->spacing; + + // Check spacing before the ellipsis. + SpacesFixer::checkAndFix( + $phpcsFile, + $previousNonEmpty, + $stackPtr, + $spacing, + 'Incorrect spacing between first class callable open parentheses and ellipsis. Expected: %s, found: %s.', + 'SpacingBefore', + 'error', + 0, + 'First class callables: space before ellipsis' + ); + + // Check spacing after the ellipsis. + SpacesFixer::checkAndFix( + $phpcsFile, + $stackPtr, + $nextNonEmpty, + $spacing, + 'Incorrect spacing between first class callable ellipsis and close parentheses. Expected: %s, found: %s.', + 'SpacingAfter', + 'error', + 0, + 'First class callables: space after ellipsis' + ); + } +} diff --git a/Universal/Tests/WhiteSpace/FirstClassCallableSpacingUnitTest.1.inc b/Universal/Tests/WhiteSpace/FirstClassCallableSpacingUnitTest.1.inc new file mode 100644 index 0000000..f3f7fe5 --- /dev/null +++ b/Universal/Tests/WhiteSpace/FirstClassCallableSpacingUnitTest.1.inc @@ -0,0 +1,49 @@ + Key is the line number, value is the number of expected errors. + */ + public function getErrorList($testFile = '') + { + switch ($testFile) { + case 'FirstClassCallableSpacingUnitTest.1.inc': + return [ + 10 => 1, + 11 => 1, + 12 => 2, + 13 => 2, + 14 => 1, + 16 => 1, + 19 => 2, + + 23 => 1, + 24 => 1, + 25 => 2, + 26 => 2, + 27 => 1, + 30 => 1, + 32 => 1, + + 36 => 1, + 37 => 1, + 38 => 2, + 39 => 2, + 40 => 2, + 41 => 1, + 42 => 1, + 46 => 2, + ]; + + default: + return []; + } + } + + /** + * Returns the lines where warnings should occur. + * + * @return array Key is the line number, value is the number of expected warnings. + */ + public function getWarningList() + { + return []; + } +}