Skip to content

Commit

Permalink
minor: PHP8.3 const type tokenizing (#7055)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Jul 4, 2023
1 parent 98640cd commit 1ab962b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Tokenizer/AbstractTypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private function isPartOfType(Tokens $tokens, int $index): bool
{
// return types and non-capturing catches
$typeColonIndex = $tokens->getTokenNotOfKindSibling($index, -1, self::TYPE_TOKENS);
if ($tokens[$typeColonIndex]->isGivenKind([T_CATCH, CT::T_TYPE_COLON])) {
if ($tokens[$typeColonIndex]->isGivenKind([T_CATCH, CT::T_TYPE_COLON, T_CONST])) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,36 @@ public function __invoke((Foo&Bar)|string $a): string
],
];
}

/**
* @param array<int, int> $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}

public static function provideProcess83Cases(): iterable
{
yield 'typed const' => [
'<?php
class Foo {
const A|(B&C) Bar = 1;
const (B&C)|A Bar = 2;
const D|(B&C)|A Bar = 3;
}',
[
12 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_OPEN,
16 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_CLOSE,
27 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_OPEN,
31 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_CLOSE,
46 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_OPEN,
50 => CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_CLOSE,
],
];
}
}
38 changes: 38 additions & 0 deletions tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,42 @@ public function foo($a, $b) {
],
];
}

/**
* @param array<int, int> $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}

public static function provideProcess83Cases(): iterable
{
yield 'typed const alternate types' => [
'<?php class Foo { const A|B Bar = 1;}',
[
10 => CT::T_TYPE_ALTERNATION,
],
];

yield 'typed const mixed types' => [
'<?php
class Foo {
const A|\B|array/**/|(Z&V)|callable | X /* X*/ |D Bar = 1;
}
',
[
11 => CT::T_TYPE_ALTERNATION,
14 => CT::T_TYPE_ALTERNATION,
17 => CT::T_TYPE_ALTERNATION,
23 => CT::T_TYPE_ALTERNATION,
26 => CT::T_TYPE_ALTERNATION,
32 => CT::T_TYPE_ALTERNATION,
],
];
}
}
43 changes: 43 additions & 0 deletions tests/Tokenizer/Transformer/TypeIntersectionTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,47 @@ public function foo($a, $b) {
],
];
}

/**
* @param array<int, int> $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}

public static function provideProcess83Cases(): iterable
{
yield 'typed const DNF types 1' => [
'<?php class Foo { const (A&B)|Z Bar = 1;}',
[
11 => CT::T_TYPE_INTERSECTION,
],
];

yield 'typed const DNF types 2' => [
'<?php class Foo { const Z|(A&B) Bar = 1;}',
[
13 => CT::T_TYPE_INTERSECTION,
],
];

yield 'typed const DNF types 3' => [
'<?php class Foo { const Z|(A&B)|X Bar = 1;}',
[
13 => CT::T_TYPE_INTERSECTION,
],
];

yield 'typed const' => [
'<?php class Foo { const A&B Bar = 1;}',
[
10 => CT::T_TYPE_INTERSECTION,
],
];
}
}

0 comments on commit 1ab962b

Please sign in to comment.