From 72657e1bf419f7d75c1bff325366a7bfb6b698fa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 22 Jul 2022 22:42:52 +0200 Subject: [PATCH] :sparkles: New `Universal.WhiteSpace.AnonClassKeywordSpacing` sniff Checks the amount of spacing between the `class` keyword and the open parenthesis (if any) for anonymous class declarations. The expected amount of spacing is configurable and defaults to `0`. The default value of `0` has been decided upon based on scanning a not insignificant number of codebases and determining the prevailing style used for anonymous classes. It is also in line with the previously proposed [errata for PSR12](https://github.com/php-fig/fig-standards/pull/1206). Includes fixer. Includes unit tests. Includes documentation. Includes metrics. --- .../AnonClassKeywordSpacingStandard.xml | 28 +++++ .../AnonClassKeywordSpacingSniff.php | 83 ++++++++++++++ .../AnonClassKeywordSpacingUnitTest.inc | 103 ++++++++++++++++++ .../AnonClassKeywordSpacingUnitTest.inc.fixed | 100 +++++++++++++++++ .../AnonClassKeywordSpacingUnitTest.php | 63 +++++++++++ 5 files changed, 377 insertions(+) create mode 100644 Universal/Docs/WhiteSpace/AnonClassKeywordSpacingStandard.xml create mode 100644 Universal/Sniffs/WhiteSpace/AnonClassKeywordSpacingSniff.php create mode 100644 Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.inc create mode 100644 Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.inc.fixed create mode 100644 Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.php diff --git a/Universal/Docs/WhiteSpace/AnonClassKeywordSpacingStandard.xml b/Universal/Docs/WhiteSpace/AnonClassKeywordSpacingStandard.xml new file mode 100644 index 00000000..4eff64c3 --- /dev/null +++ b/Universal/Docs/WhiteSpace/AnonClassKeywordSpacingStandard.xml @@ -0,0 +1,28 @@ + + + + + + + + class($param) +{ + public function __construct($p) {} +}; + ]]> + + + class ($param) +{ + public function __construct($p) {} +}; + ]]> + + + diff --git a/Universal/Sniffs/WhiteSpace/AnonClassKeywordSpacingSniff.php b/Universal/Sniffs/WhiteSpace/AnonClassKeywordSpacingSniff.php new file mode 100644 index 00000000..f823fc28 --- /dev/null +++ b/Universal/Sniffs/WhiteSpace/AnonClassKeywordSpacingSniff.php @@ -0,0 +1,83 @@ +getTokens(); + + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { + return; + } + + // Check spacing + // Auto-fix when not a comment bteween. + SpacesFixer::checkAndFix( + $phpcsFile, + $stackPtr, + $nextNonEmpty, + (int) $this->spacing, + 'There must be %1$s between the class keyword and the open parenthesis for an anonymous class. Found: %2$s', + 'Incorrect', + 'error', + 0, + 'Anon class: space between keyword and open parenthesis' + ); + + // $phpcsFile->recordMetric($reportPtr, self::METRIC_NAME_SRC, 'same namespace'); + // $phpcsFile->addError($error, $reportPtr, $errorCode, $data); + } +} diff --git a/Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.inc b/Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.inc new file mode 100644 index 00000000..fc238705 --- /dev/null +++ b/Universal/Tests/WhiteSpace/AnonClassKeywordSpacingUnitTest.inc @@ -0,0 +1,103 @@ + + */ + public function getErrorList() + { + return [ + 28 => 1, + 29 => 1, + 30 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 56 => 1, + 57 => 1, + 60 => 1, + 61 => 1, + 68 => 1, + 69 => 1, + 70 => 1, + 87 => 1, + 88 => 1, + 91 => 1, + 92 => 1, + 93 => 1, + ]; + } + + /** + * Returns the lines where warnings should occur. + * + * @return array + */ + public function getWarningList() + { + return []; + } +}