Skip to content

Commit

Permalink
Classes/NewClasses: detect new classes in PHP 8.2 DNF types
Browse files Browse the repository at this point in the history
This updates the sniff to also detect new classes when used in DNF types.

Includes unit tests.
  • Loading branch information
jrfnl committed May 24, 2024
1 parent 96072c3 commit a21251e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PHPCompatibility/Sniffs/Classes/NewClassesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ private function checkTypeDeclaration($phpcsFile, $stackPtr, $typeString)
{
// Strip off potential nullable indication.
$typeString = \ltrim($typeString, '?');
$types = \preg_split('`[|&]`', $typeString, -1, \PREG_SPLIT_NO_EMPTY);
$types = \preg_split('`[|&()]`', $typeString, -1, \PREG_SPLIT_NO_EMPTY);

if (empty($types) === true) {
return;
Expand Down
9 changes: 9 additions & 0 deletions PHPCompatibility/Tests/Classes/NewClassesUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,12 @@ class MyRandom extends \Random\Randomizer {
function CheckAllTypeParts(
NotATarget|XMLReader $a
) : NotATarget|ReflectionType {}

// Test support for PHP 8.2 DNF types.
class DNFTypes {
public function paramTypeA((NotATarget&AlsoNotATarget)|string $okay) {}

public null|(IntlPartsIterator&IntlIterator) $property;
public function paramTypeB((\ReflectionClassConstant&\ReflectionNamedType)|int $param) {}
public function returnType($param) : (\FFI\CData&\FFI\CType)|SomethingElse {}
}
13 changes: 7 additions & 6 deletions PHPCompatibility/Tests/Classes/NewClassesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,18 @@ public static function dataNewClass()
['IntlBreakIterator', '5.4', [58, 98, 138], '5.5'],
['IntlRuleBasedBreakIterator', '5.4', [59, 99, 139], '5.5'],
['IntlCodePointBreakIterator', '5.4', [60, 100, 140], '5.5'],
['IntlPartsIterator', '5.4', [351, 464], '5.5'],
['IntlIterator', '5.4', [352, 464], '5.5'],
['IntlPartsIterator', '5.4', [351, 464, 495], '5.5'],
['IntlIterator', '5.4', [352, 464, 495], '5.5'],
['UConverter', '5.4', [276], '5.5'],
['GMP', '5.5', [281], '5.6'],
['IntlChar', '5.6', [277, 448], '7.0'],
['ReflectionType', '5.6', [308, 489], '7.0'],
['ReflectionGenerator', '5.6', [309], '7.0'],
['ReflectionClassConstant', '7.0', [306, 465], '7.1'],
['ReflectionNamedType', '7.0', [359, 465], '7.1'],
['ReflectionClassConstant', '7.0', [306, 465, 496], '7.1'],
['ReflectionNamedType', '7.0', [359, 465, 496], '7.1'],
['FFI', '7.3', [346], '7.4'],
['FFI\CData', '7.3', [347, 466], '7.4'],
['FFI\CType', '7.3', [347, 466], '7.4'],
['FFI\CData', '7.3', [347, 466, 497], '7.4'],
['FFI\CType', '7.3', [347, 466, 497], '7.4'],
['ReflectionReference', '7.3', [344, 455], '7.4'],
['WeakReference', '7.3', [345, 455], '7.4'],
['PhpToken', '7.4', [415], '8.0'],
Expand Down Expand Up @@ -320,6 +320,7 @@ public static function dataNoFalsePositives()
[441],
[453],
[462],
[493],
];
}

Expand Down

0 comments on commit a21251e

Please sign in to comment.