Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ $var = (boolean)/* comment */ $var2;

$var = ( int )$spacesInsideParenthesis;
$var = ( int )$tabsInsideParenthesis;

$var = (void) callMe();
$var = (void)callMe();
$var = (void) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ $var = (boolean)/* comment */ $var2;

$var = ( int ) $spacesInsideParenthesis;
$var = ( int ) $tabsInsideParenthesis;

$var = (void) callMe();
$var = (void) callMe();
$var = (void) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function getErrorList($testFile = '')
97 => 1,
99 => 1,
100 => 1,
103 => 1,
104 => 1,
];

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ $var = array(
);

(bool) $a ? echo $b : echo $c;

$var = (void) callMe();
$var =(void) callMe();
$var = (void) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ $var = array(
);

(bool) $a ? echo $b : echo $c;

$var = (void) callMe();
$var = (void) callMe();
$var = (void) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function getErrorList()
53 => 1,
55 => 1,
56 => 1,
68 => 1,
69 => 1,
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ class DNFTypes {
interface PHP84HookedProperty {
public String $readable { get; }
}

$php85 = (VOID) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ class DNFTypes {
interface PHP84HookedProperty {
public string $readable { get; }
}

$php85 = (void) callMe();
1 change: 1 addition & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function getErrorList($testFile = '')
136 => 1,
139 => 2,
143 => 1,
146 => 1,
];

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ $cntPages = ceil(count($items) / parent::ON_PAGE);
$nsRelative = namespace\doSomething($items + 10);
$partiallyQualified = Partially\qualified($items + 10);
$fullyQualified = \Fully\qualified($items + 10);

$php85voidcast = (void) CallMeOnce() + (void) CallMeTwice();
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ $cntPages = ceil(count($items) / parent::ON_PAGE);
$nsRelative = namespace\doSomething($items + 10);
$partiallyQualified = Partially\qualified($items + 10);
$fullyQualified = \Fully\qualified($items + 10);

$php85voidcast = ((void) CallMeOnce() + (void) CallMeTwice());
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function getErrorList($testFile = '')
189 => 1,
193 => 1,
194 => 2,
209 => 1,
];

default:
Expand Down
2 changes: 2 additions & 0 deletions src/Standards/Squiz/Tests/WhiteSpace/CastSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ $var = ( int ) $var2;

$var = (binary) $var2;
$var = ( binary ) $var2;

$php85 = ( void) callMe();
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ $var = (int) $var2;

$var = (binary) $var2;
$var = (binary) $var2;

$php85 = (void) callMe();
11 changes: 6 additions & 5 deletions src/Standards/Squiz/Tests/WhiteSpace/CastSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ final class CastSpacingUnitTest extends AbstractSniffTestCase
public function getErrorList()
{
return [
3 => 1,
4 => 1,
5 => 1,
6 => 1,
9 => 1,
3 => 1,
4 => 1,
5 => 1,
6 => 1,
9 => 1,
11 => 1,
];
}

Expand Down
52 changes: 52 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,58 @@ protected function tokenize(string $code)
continue;
}

/*
Detect PHP 8.5+ void casting and assign the casts their own token.

Mind: type cast tokens _may_ contain whitespace, but no new lines and no comments.
*/

if (PHP_VERSION_ID < 80500
&& $token[0] === '('
) {
$content = $token[0];
$i = ($stackPtr + 1);

if (is_array($tokens[$i]) === true
&& $tokens[$i][0] === T_WHITESPACE
&& strpos($tokens[$i][1], "\n") === false
&& strpos($tokens[$i][1], "\r") === false
) {
$content .= $tokens[$i][1];
++$i;
}

if (is_array($tokens[$i]) === true
&& $tokens[$i][0] === T_STRING
&& strtolower($tokens[$i][1]) === 'void'
) {
$content .= $tokens[$i][1];
++$i;

if (is_array($tokens[$i]) === true
&& $tokens[$i][0] === T_WHITESPACE
&& strpos($tokens[$i][1], "\n") === false
&& strpos($tokens[$i][1], "\r") === false
) {
$content .= $tokens[$i][1];
++$i;
}

if ($tokens[$i][0] === ')') {
$content .= $tokens[$i][0];

$finalTokens[$newStackPtr] = [
'code' => T_VOID_CAST,
'type' => 'T_VOID_CAST',
'content' => $content,
];
$newStackPtr++;
$stackPtr = $i;
continue;
}
}
}

/*
If this is a heredoc, PHP will tokenize the whole
thing which causes problems when heredocs don't
Expand Down
6 changes: 6 additions & 0 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
define('T_PRIVATE_SET', 'PHPCS_T_PRIVATE_SET');
}

// Some PHP 8.5 tokens, replicated for lower versions.
if (defined('T_VOID_CAST') === false) {
define('T_VOID_CAST', 'PHPCS_T_VOID_CAST');
}

// Tokens used for parsing doc blocks.
define('T_DOC_COMMENT_STAR', 'PHPCS_T_DOC_COMMENT_STAR');
define('T_DOC_COMMENT_WHITESPACE', 'PHPCS_T_DOC_COMMENT_WHITESPACE');
Expand Down Expand Up @@ -266,6 +271,7 @@ final class Tokens
T_OBJECT_CAST => T_OBJECT_CAST,
T_UNSET_CAST => T_UNSET_CAST,
T_BINARY_CAST => T_BINARY_CAST,
T_VOID_CAST => T_VOID_CAST,
];

/**
Expand Down
130 changes: 130 additions & 0 deletions tests/Core/Tokenizers/PHP/TypecastsTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/*
* Safeguard that all type casts past and present are tokenized correctly.
*
* This is also intended as an early warning system for when type cast tokens would be removed
* for type casts which are no longer supported by PHP itself.
*
* Pertinent type cast changes in PHP:
* - (unset) cast was introduced in PHP 5.0.
* - (binary) cast was introduced in PHP 5.2.1.
* - (unset) cast was deprecated in PHP 7.2 and removed in PHP 8.0.
* - (real) cast was deprecated in PHP 7.4 and removed in PHP 8.0.
* - (void) cast was introduced in PHP 8.5.
* - (integer), (boolean), (double) and (binary) casts are deprecated since PHP 8.5 and will be removed in PHP 9.0.
*/

/* testNotATypeCast1 */
$a = (NOT_A_TYPECAST);

/* testBool */
$a = (bool) $b;

/* testSpacyUppercaseBool */
$a = ( BOOL) $b;

/* testBoolean */
$a = (boolean) $b;

/* testSpacyUppercaseBoolean */
$a = ( BOOLEAN ) $b;

/* testInt */
$a = (int) $b;

/* testSpacyUppercaseInt */
$a = ( INT ) $b;

/* testInteger */
$a = (integer) $b;

/* testSpacyUppercaseInteger */
$a = ( INTEGER ) $b;

/* testFloat */
$a = (float) $b;

/* testSpacyUppercaseFloat */
$a = ( FLOAT ) $b;

/* testReal */
$a = (real) $b;

/* testSpacyUppercaseReal */
$a = ( REAL ) $b;

/* testDouble */
$a = (double) $b;

/* testSpacyUppercaseDouble */
$a = ( DOUBLE ) $b;

/* testString */
$a = (string) $b;

/* testSpacyUppercaseString */
$a = (STRING ) $b;

/* testBinary */
$a = (binary) $b;

/* testSpacyUppercaseBinary */
$a = ( BINARY ) $b;

/* testArray */
$a = (array) $b;

/* testSpacyUppercaseArray */
$a = ( ARRAY ) $b;

/* testObject */
$a = (object) $b;

/* testSpacyUppercaseObject */
$a = ( OBJECT ) $b;

/* testUnset */
$a = (unset) $b;

/* testSpacyUppercaseUnset */
$a = ( UNSET ) $b;

/* testVoid */
$a = (void) $b;

/* testVoidNested */
$a = ((void) $b);

/* testSpacyVoid */
$a = ( void ) $b;

/* testTabbyVoid */
$a = ( void ) $b; // Tab whitespace.

/* testUppercaseVoid */
$a = (VOID) $b;


/*
* New lines and comments are not allowed in a type cast.
*/

/* testNotATypeCast2 */
$a = (
void
) $b;

/* testNotATypeCast3 */
$a = (

void

) $b;

/* testNotATypeCast4 */
$a = ( /*comment*/ void /*comment */ ) $b;

// Parse error. This must be the last test in the file!
/* testNotATypeCast5 */
$a = (void
Loading