From 03b9b65585c3709781bbd52ebb2f228f567dcb85 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 12 Aug 2022 18:40:23 +0200 Subject: [PATCH] :sparkles: New `WordPress.WhiteSpace.ObjectOperatorSpacing` sniff This sniff largely defers to the upstream `Squiz.WhiteSpace.ObjectOperatorSpacing` sniff, which was previously already included. The only real difference between the two sniffs is that for class resolution using `ClassName::class`, new lines around the `::` object operator will not be ignored. The WPCS sniff has the same default property settings as the upstream sniff and changes to the property values should be made in the ruleset. Includes docs. Includes unit tests covering the difference and safeguarding that the sniff doesn't overreach. --- WordPress-Core/ruleset.xml | 2 +- .../ObjectOperatorSpacingStandard.xml | 19 ++++++ .../WhiteSpace/ObjectOperatorSpacingSniff.php | 65 +++++++++++++++++++ .../ObjectOperatorSpacingUnitTest.inc | 55 ++++++++++++++++ .../ObjectOperatorSpacingUnitTest.inc.fixed | 45 +++++++++++++ .../ObjectOperatorSpacingUnitTest.php | 59 +++++++++++++++++ 6 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 WordPress/Docs/WhiteSpace/ObjectOperatorSpacingStandard.xml create mode 100644 WordPress/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php create mode 100644 WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.inc create mode 100644 WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.inc.fixed create mode 100644 WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.php diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index eee861589b..b467c8e930 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -526,7 +526,7 @@ - + diff --git a/WordPress/Docs/WhiteSpace/ObjectOperatorSpacingStandard.xml b/WordPress/Docs/WhiteSpace/ObjectOperatorSpacingStandard.xml new file mode 100644 index 0000000000..47ffb1e8e6 --- /dev/null +++ b/WordPress/Docs/WhiteSpace/ObjectOperatorSpacingStandard.xml @@ -0,0 +1,19 @@ + + + , ?->, ::) should not have any spaces around them, though new lines are allowed except for use with the `::class` constant. + ]]> + + + + ->bar(); + ]]> + + + ?-> bar(); + ]]> + + + diff --git a/WordPress/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php new file mode 100644 index 0000000000..2970fe927d --- /dev/null +++ b/WordPress/Sniffs/WhiteSpace/ObjectOperatorSpacingSniff.php @@ -0,0 +1,65 @@ +getTokens(); + $property_adjusted = false; + + // Check for `::class` and don't ignore new lines in that case. + if ( true === $this->ignoreNewlines + && \T_DOUBLE_COLON === $tokens[ $stackPtr ]['code'] + ) { + $next_non_empty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + if ( \T_STRING === $tokens[ $next_non_empty ]['code'] + && 'class' === strtolower( $tokens[ $next_non_empty ]['content'] ) + ) { + $property_adjusted = true; + $this->ignoreNewlines = false; + } + } + + $return = parent::process( $phpcsFile, $stackPtr ); + + if ( true === $property_adjusted ) { + $this->ignoreNewlines = true; + } + + return $return; + } +} diff --git a/WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.inc b/WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.inc new file mode 100644 index 0000000000..ec042a295f --- /dev/null +++ b/WordPress/Tests/WhiteSpace/ObjectOperatorSpacingUnitTest.inc @@ -0,0 +1,55 @@ + + */ + public function getErrorList() { + return array( + 11 => 2, + 12 => 2, + 13 => 2, + 16 => 2, + 19 => 2, + 22 => 2, + 26 => 2, + 36 => 2, + 37 => 2, + 38 => 2, + 47 => 2, + 51 => 2, + ); + } + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array + */ + public function getWarningList() { + return array(); + } +}