Skip to content

Commit 3b8ebd2

Browse files
committed
Core: Defer to upstream PSR12.Keywords.ShortFormTypeKeywords sniff (PHPCS 3.3.0)
The WPCS native `WordPress.PHP.TypeCasts` sniff used to check that: * Type casts are in lowercase; * Boolean and integer casts are short form; * The `(double)` cast is not used; * Discourages the use of the `(unset)` and `(binary)` casts. The lowercase checks has been removed in WPCS 2.0.0 in favour of the upstream `Generic.PHP.LowerCaseType` sniff via 1582. Asides from that sniff, PHPCS 3.3.0 also introduces the `PSR12.Keywords.ShortFormTypeKeywords` sniff which checks that boolean and integer casts are in short form. This PR removes that check from the `WordPress.PHP.TypeCasts` sniff in favour of using the upstream sniff. There is currently one known bug in the upstream sniff - a false positive when a type cast contains whitespace, see squizlabs/PHP_CodeSniffer 2331 -, however, as WPCS also includes the `Squiz.WhiteSpace.CastSpacing` sniff which forbids whitespace within type casts anyway, this is not a reason to delay. Includes updating the ruleset inline documentation to be in line with the current text in the handbook. Includes updating the sniff class documentation to be in line with the current checks contained in the sniff. Includes removing unit tests which are no longer relevant.
1 parent ab41be9 commit 3b8ebd2

File tree

5 files changed

+22
-69
lines changed

5 files changed

+22
-69
lines changed

WordPress-Core/ruleset.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,14 @@
241241

242242
<!-- Rule: Perform logical comparisons, like so: if ( ! $foo ) { -->
243243

244-
<!-- Covers rule: When type casting, do it like so: $foo = (boolean) $bar; -->
244+
<!-- Covers rule: Type casts must be lowercase. Always prefer the short form
245+
of type casts, (int) instead of (integer) and (bool) rather than (boolean).
246+
For float casts use (float). -->
245247
<rule ref="Generic.Formatting.SpaceAfterCast"/>
246248
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
247249
<rule ref="WordPress.WhiteSpace.CastStructureSpacing"/>
248250
<rule ref="WordPress.PHP.TypeCasts"/>
251+
<rule ref="PSR12.Keywords.ShortFormTypeKeywords"/>
249252
<!-- N.B.: This sniff also checks the case of (parameter/return) type declarations, not just type casts. -->
250253
<rule ref="Generic.PHP.LowerCaseType"/>
251254

WordPress/Sniffs/PHP/TypeCastsSniff.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* Verifies the correct usage of type cast keywords.
1717
*
1818
* Type casts should be:
19-
* - lowercase;
20-
* - short form, i.e. (bool) not (boolean);
2119
* - normalized, i.e. (float) not (real).
2220
*
2321
* Additionally, the use of the (unset) and (binary) casts is discouraged.
@@ -27,6 +25,8 @@
2725
* @package WPCS\WordPressCodingStandards
2826
*
2927
* @since 1.2.0
28+
* @since 2.0.0 No longer checks that type casts are lowercase or short form.
29+
* Relevant PHPCS native sniffs have been included in the rulesets instead.
3030
*/
3131
class TypeCastsSniff extends Sniff {
3232

@@ -36,10 +36,12 @@ class TypeCastsSniff extends Sniff {
3636
* @return array
3737
*/
3838
public function register() {
39-
$targets = Tokens::$castTokens;
40-
unset( $targets[ \T_ARRAY_CAST ], $targets[ \T_OBJECT_CAST ] );
41-
42-
return $targets;
39+
return array(
40+
\T_DOUBLE_CAST,
41+
\T_UNSET_CAST,
42+
\T_STRING_CAST,
43+
\T_BINARY_CAST,
44+
);
4345
}
4446

4547
/**
@@ -55,39 +57,7 @@ public function process_token( $stackPtr ) {
5557
$typecast = str_replace( ' ', '', $this->tokens[ $stackPtr ]['content'] );
5658
$typecast_lc = strtolower( $typecast );
5759

58-
$this->phpcsFile->recordMetric( $stackPtr, 'Typecast encountered', $typecast );
59-
6060
switch ( $token_code ) {
61-
case \T_BOOL_CAST:
62-
if ( '(bool)' !== $typecast_lc ) {
63-
$fix = $this->phpcsFile->addFixableError(
64-
'Short form type keywords must be used; expected "(bool)" but found "%s"',
65-
$stackPtr,
66-
'LongBoolFound',
67-
array( $typecast )
68-
);
69-
70-
if ( true === $fix ) {
71-
$this->phpcsFile->fixer->replaceToken( $stackPtr, '(bool)' );
72-
}
73-
}
74-
break;
75-
76-
case \T_INT_CAST:
77-
if ( '(int)' !== $typecast_lc ) {
78-
$fix = $this->phpcsFile->addFixableError(
79-
'Short form type keywords must be used; expected "(int)" but found "%s"',
80-
$stackPtr,
81-
'LongIntFound',
82-
array( $typecast )
83-
);
84-
85-
if ( true === $fix ) {
86-
$this->phpcsFile->fixer->replaceToken( $stackPtr, '(int)' );
87-
}
88-
}
89-
break;
90-
9161
case \T_DOUBLE_CAST:
9262
if ( '(float)' !== $typecast_lc ) {
9363
$fix = $this->phpcsFile->addFixableError(

WordPress/Tests/PHP/TypeCastsUnitTest.inc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<?php
22

33
// OK.
4-
$a = (bool) $b;
5-
$a = (int) $b;
64
$a = (float) $b;
75
$a = (string) $b;
86
$a = (array) $b;
97
$a = (object) $b;
108

119
// Error: Wrong form.
12-
$a = (boolean) $b;
13-
$a = (integer) $b;
1410
$a = (double) $b;
1511
$a = (real) $b;
1612

@@ -22,15 +18,11 @@ $a = b"binary $string"; // Warning.
2218

2319
// Test recognition with whitespace within the cast.
2420
// OK.
25-
$a = ( bool ) $b;
26-
$a = ( int ) $b;
2721
$a = ( float) $b;
2822
$a = (string ) $b;
2923
$a = ( array) $b;
3024
$a = (object ) $b;
3125

32-
$a = ( boolean ) $b; // Error.
33-
$a = ( integer) $b; // Error.
3426
$a = (double ) $b; // Error.
3527
$a = ( real ) $b; // Error.
3628
$a = ( unset ) $b; // Warning.

WordPress/Tests/PHP/TypeCastsUnitTest.inc.fixed

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<?php
22

33
// OK.
4-
$a = (bool) $b;
5-
$a = (int) $b;
64
$a = (float) $b;
75
$a = (string) $b;
86
$a = (array) $b;
97
$a = (object) $b;
108

119
// Error: Wrong form.
12-
$a = (bool) $b;
13-
$a = (int) $b;
1410
$a = (float) $b;
1511
$a = (float) $b;
1612

@@ -22,15 +18,11 @@ $a = b"binary $string"; // Warning.
2218

2319
// Test recognition with whitespace within the cast.
2420
// OK.
25-
$a = ( bool ) $b;
26-
$a = ( int ) $b;
2721
$a = ( float) $b;
2822
$a = (string ) $b;
2923
$a = ( array) $b;
3024
$a = (object ) $b;
3125

32-
$a = (bool) $b; // Error.
33-
$a = (int) $b; // Error.
3426
$a = (float) $b; // Error.
3527
$a = (float) $b; // Error.
3628
$a = ( unset ) $b; // Warning.

WordPress/Tests/PHP/TypeCastsUnitTest.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ class TypeCastsUnitTest extends AbstractSniffUnitTest {
2828
*/
2929
public function getErrorList() {
3030
return array(
31-
12 => 1,
32-
13 => 1,
33-
14 => 1,
34-
15 => 1,
35-
32 => 1,
36-
33 => 1,
37-
34 => 1,
38-
35 => 1,
31+
10 => 1,
32+
11 => 1,
33+
26 => 1,
34+
27 => 1,
3935
);
4036
}
4137

@@ -46,12 +42,12 @@ public function getErrorList() {
4642
*/
4743
public function getWarningList() {
4844
return array(
49-
18 => 1,
50-
19 => 1,
51-
20 => ( version_compare( PHPCSHelper::get_version(), '3.4.0', '<' ) === true ? 0 : 1 ),
52-
21 => 1,
53-
36 => 1,
54-
37 => 1,
45+
14 => 1,
46+
15 => 1,
47+
16 => ( version_compare( PHPCSHelper::get_version(), '3.4.0', '<' ) === true ? 0 : 1 ),
48+
17 => 1,
49+
28 => 1,
50+
29 => 1,
5551
);
5652
}
5753
}

0 commit comments

Comments
 (0)