Skip to content

Commit 1dad96d

Browse files
authored
Merge pull request #1582 from WordPress-Coding-Standards/feature/remove-part-of-typecast-sniff
Core: Defer to upstream `Generic.PHP.LowerCaseType` sniff (PHPCS 3.3.0)
2 parents 1fd3aba + ac5c1a8 commit 1dad96d

File tree

5 files changed

+8
-114
lines changed

5 files changed

+8
-114
lines changed

WordPress-Core/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@
248248
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
249249
<rule ref="WordPress.WhiteSpace.CastStructureSpacing"/>
250250
<rule ref="WordPress.PHP.TypeCasts"/>
251+
<!-- N.B.: This sniff also checks the case of (parameter/return) type declarations, not just type casts. -->
252+
<rule ref="Generic.PHP.LowerCaseType"/>
251253

252254
<!-- Covers rule: ... array items, only include a space around the index if it is a variable. -->
253255
<rule ref="WordPress.Arrays.ArrayKeySpacingRestrictions"/>

WordPress/Sniffs/PHP/TypeCastsSniff.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class TypeCastsSniff extends Sniff {
3636
* @return array
3737
*/
3838
public function register() {
39-
return Tokens::$castTokens;
39+
$targets = Tokens::$castTokens;
40+
unset( $targets[ \T_ARRAY_CAST ], $targets[ \T_OBJECT_CAST ] );
41+
42+
return $targets;
4043
}
4144

4245
/**
@@ -122,33 +125,6 @@ public function process_token( $stackPtr ) {
122125
);
123126
break;
124127
}
125-
126-
/*
127-
* {@internal Once the minimum PHPCS version has gone up to PHPCS 3.3.0+, the lowercase
128-
* check below can be removed in favour of adding the `Generic.PHP.LowerCaseType` sniff
129-
* to the ruleset.
130-
* Note: the `register()` function also needs adjusting in that case to only register the
131-
* targetted type casts above and the metrics recording should probably be adjusted as well.
132-
* The above mentioned Generic sniff records metrics about the case of typecasts, so we
133-
* don't need to worry about those no longer being recorded. They will be, just slightly
134-
* differently.}}
135-
*/
136-
if ( $typecast_lc !== $typecast ) {
137-
$data = array(
138-
$typecast_lc,
139-
$typecast,
140-
);
141-
142-
$fix = $this->phpcsFile->addFixableError(
143-
'PHP type casts must be lowercase; expected "%s" but found "%s"',
144-
$stackPtr,
145-
'NonLowercaseFound',
146-
$data
147-
);
148-
if ( true === $fix ) {
149-
$this->phpcsFile->fixer->replaceToken( $stackPtr, $typecast_lc );
150-
}
151-
}
152128
}
153129

154130
}

WordPress/Tests/PHP/TypeCastsUnitTest.inc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,6 @@ $a = (binary) $b; // Warning.
2020
$a = b"binary string"; // Warning.
2121
$a = b"binary $string"; // Warning.
2222

23-
// Error: Mixed case.
24-
$a = (Bool) $b;
25-
$a = (Boolean) $b; // + wrong form.
26-
$a = (Int) $b;
27-
$a = (Integer) $b; // + wrong form.
28-
$a = (Float) $b;
29-
$a = (Double) $b; // + wrong form.
30-
$a = (Real) $b; // + wrong form.
31-
$a = (String) $b;
32-
$a = (Array) $b;
33-
$a = (Object) $b;
34-
$a = (Unset) $b; // + discouraged.
35-
$a = (Binary) $b; // + discouraged.
36-
37-
// Error: Uppercase.
38-
$a = (BOOL) $b;
39-
$a = (BOOLEAN) $b; // + wrong form.
40-
$a = (INT) $b;
41-
$a = (INTEGER) $b; // + wrong form.
42-
$a = (FLOAT) $b;
43-
$a = (DOUBLE) $b; // + wrong form.
44-
$a = (REAL) $b; // + wrong form.
45-
$a = (STRING) $b;
46-
$a = (ARRAY) $b;
47-
$a = (OBJECT) $b;
48-
$a = (UNSET) $b; // + discouraged.
49-
$a = (BINARY) $b; // + discouraged.
50-
5123
// Test recognition with whitespace within the cast.
5224
// OK.
5325
$a = ( bool ) $b;

WordPress/Tests/PHP/TypeCastsUnitTest.inc.fixed

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,6 @@ $a = (binary) $b; // Warning.
2020
$a = b"binary string"; // Warning.
2121
$a = b"binary $string"; // Warning.
2222

23-
// Error: Mixed case.
24-
$a = (bool) $b;
25-
$a = (bool) $b; // + wrong form.
26-
$a = (int) $b;
27-
$a = (int) $b; // + wrong form.
28-
$a = (float) $b;
29-
$a = (float) $b; // + wrong form.
30-
$a = (float) $b; // + wrong form.
31-
$a = (string) $b;
32-
$a = (array) $b;
33-
$a = (object) $b;
34-
$a = (unset) $b; // + discouraged.
35-
$a = (binary) $b; // + discouraged.
36-
37-
// Error: Uppercase.
38-
$a = (bool) $b;
39-
$a = (bool) $b; // + wrong form.
40-
$a = (int) $b;
41-
$a = (int) $b; // + wrong form.
42-
$a = (float) $b;
43-
$a = (float) $b; // + wrong form.
44-
$a = (float) $b; // + wrong form.
45-
$a = (string) $b;
46-
$a = (array) $b;
47-
$a = (object) $b;
48-
$a = (unset) $b; // + discouraged.
49-
$a = (binary) $b; // + discouraged.
50-
5123
// Test recognition with whitespace within the cast.
5224
// OK.
5325
$a = ( bool ) $b;

WordPress/Tests/PHP/TypeCastsUnitTest.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,10 @@ public function getErrorList() {
3232
13 => 1,
3333
14 => 1,
3434
15 => 1,
35-
24 => 1,
36-
25 => 2,
37-
26 => 1,
38-
27 => 2,
39-
28 => 1,
40-
29 => 2,
41-
30 => 2,
42-
31 => 1,
4335
32 => 1,
4436
33 => 1,
4537
34 => 1,
4638
35 => 1,
47-
38 => 1,
48-
39 => 2,
49-
40 => 1,
50-
41 => 2,
51-
42 => 1,
52-
43 => 2,
53-
44 => 2,
54-
45 => 1,
55-
46 => 1,
56-
47 => 1,
57-
48 => 1,
58-
49 => 1,
59-
60 => 1,
60-
61 => 1,
61-
62 => 1,
62-
63 => 1,
6339
);
6440
}
6541

@@ -74,12 +50,8 @@ public function getWarningList() {
7450
19 => 1,
7551
20 => ( version_compare( PHPCSHelper::get_version(), '3.4.0', '<' ) === true ? 0 : 1 ),
7652
21 => 1,
77-
34 => 1,
78-
35 => 1,
79-
48 => 1,
80-
49 => 1,
81-
64 => 1,
82-
65 => 1,
53+
36 => 1,
54+
37 => 1,
8355
);
8456
}
8557
}

0 commit comments

Comments
 (0)