Skip to content

Commit abab1cb

Browse files
committed
OperatorSpacing: check spacing around instanceof operator
PHPCS 3.3.0 introduces a `PSR12.Operators.OperatorSpacing` sniff. I've investigated whether that sniff would be a better sniff to extend for the `WordPress.WhiteSpace.OperatorSpacing` sniff than the `Squiz.WhiteSpace.OperatorSpacing` sniff which is currently being extended and have concluded that it is not for the following reason: * The PSR12 sniff demands _at least_ one space instead of _exactly_ one space (both allow for new lines with the correct properties). All the same, the `PSR12` sniff did include an additional token - `T_INSTANCEOF` - which makes sense to add to the WPCS sniff. Includes unit tests.
1 parent ab41be9 commit abab1cb

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class OperatorSpacingSniff extends PHPCS_Squiz_OperatorSpacingSniff {
5454
public function register() {
5555
$tokens = parent::register();
5656
$tokens[ \T_BOOLEAN_NOT ] = \T_BOOLEAN_NOT;
57+
$tokens[ \T_INSTANCEOF ] = \T_INSTANCEOF;
5758
$logical_operators = Tokens::$booleanOperators;
5859

5960
// Using array union to auto-dedup.

WordPress/Tests/WhiteSpace/OperatorSpacingUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ if ( $a === $b
6464

6565
if ( $a === $b or
6666
$b === $c ) {}
67+
68+
// Instanceof
69+
if ( MyClass instanceof SomeOtherClass ) {}
70+
if ( MyClass instanceof SomeOtherClass ) {}

WordPress/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ if ( $a === $b
6464

6565
if ( $a === $b or
6666
$b === $c ) {}
67+
68+
// Instanceof
69+
if ( MyClass instanceof SomeOtherClass ) {}
70+
if ( MyClass instanceof SomeOtherClass ) {}

WordPress/Tests/WhiteSpace/OperatorSpacingUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getErrorList() {
4242
49 => 2,
4343
50 => 2,
4444
51 => 2,
45+
69 => 2,
4546
);
4647
}
4748

0 commit comments

Comments
 (0)