Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: PHP8.2 - handle union and intersection types for DNF types #6804

Merged
merged 8 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 31 additions & 62 deletions src/Tokenizer/AbstractTypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,44 @@
abstract class AbstractTypeTransformer extends AbstractTransformer
{
/**
* @param array{0: int, 1?: string}|string $originalToken
* @param array{0: int, 1: string}|string $originalToken
*/
protected function doProcess(Tokens $tokens, int $index, $originalToken): void
protected function doProcess(Tokens $tokens, int $candidateIndex, $originalToken): void
{
if (!$tokens[$index]->equals($originalToken)) {
if (!$tokens[$candidateIndex]->equals($originalToken)) {
return;
}

$prevIndex = $this->getPreviousTokenCandidate($tokens, $index);

/** @var Token $prevToken */
$prevToken = $tokens[$prevIndex];

if ($prevToken->isGivenKind([
CT::T_TYPE_COLON, // `:` is part of a function return type `foo(): X|Y`
CT::T_TYPE_ALTERNATION, // `|` is part of a union (chain) `X|Y`
CT::T_TYPE_INTERSECTION,
T_STATIC, T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE, // `var X|Y $a;`, `private X|Y $a` or `public static X|Y $a`
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, // promoted properties
])) {
$this->replaceToken($tokens, $index);

return;
}

if (\defined('T_READONLY') && $prevToken->isGivenKind(T_READONLY)) { // @TODO: drop condition when PHP 8.1+ is required
$this->replaceToken($tokens, $index);

return;
$index = $candidateIndex;
while ($index > 0) {
--$index;

if ($tokens[$index]->equalsAny([';', '{', '}', '='])) {
return;
}

$blockType = Tokens::detectBlockType($tokens[$index]);
if (null !== $blockType && !$blockType['isStart']) {
$index = $tokens->findBlockStart($blockType['type'], $index);

continue;
}

if ($tokens[$index]->isGivenKind([
CT::T_TYPE_COLON, // `:` is part of a function return type `foo(): X|Y`
CT::T_TYPE_ALTERNATION, // `|` is part of a union (chain) `X|Y`
CT::T_TYPE_INTERSECTION,
T_STATIC, T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE, // `var X|Y $a;`, `private X|Y $a` or `public static X|Y $a`
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, // promoted properties
T_FN, T_FUNCTION,
T_CATCH,
])) {
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
$this->replaceToken($tokens, $candidateIndex);

return;
}
}

if (!$prevToken->equalsAny(['(', ','])) {
return;
}

$prevPrevTokenIndex = $tokens->getPrevMeaningfulToken($prevIndex);

if ($tokens[$prevPrevTokenIndex]->isGivenKind(T_CATCH)) {
$this->replaceToken($tokens, $index);

return;
}

$functionKinds = [[T_FUNCTION], [T_FN]];
$functionIndex = $tokens->getPrevTokenOfKind($prevIndex, $functionKinds);

if (null === $functionIndex) {
return;
}

$braceOpenIndex = $tokens->getNextTokenOfKind($functionIndex, ['(']);
$braceCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $braceOpenIndex);

if ($braceCloseIndex < $index) {
return;
}

$this->replaceToken($tokens, $index);
}

abstract protected function replaceToken(Tokens $tokens, int $index): void;

private function getPreviousTokenCandidate(Tokens $tokens, int $index): int
{
$candidateIndex = $tokens->getTokenNotOfKindsSibling($index, -1, [T_CALLABLE, T_NS_SEPARATOR, T_STRING, CT::T_ARRAY_TYPEHINT, T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]);

return $tokens[$candidateIndex]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)
? $this->getPreviousTokenCandidate($tokens, $tokens->getPrevTokenOfKind($index, [[T_ATTRIBUTE]]))
: $candidateIndex
;
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Integration/misc/PHP8_2.test
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ trait WithConstants
private const THREE = 'three';
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
}

// https://wiki.php.net/rfc/dnf_types
function generateSlug((HasTitle&HasId)|null $post)
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
{
throw new \Exception('not implemented');
}

--INPUT--
<?php

Expand Down Expand Up @@ -191,3 +197,9 @@ trait WithConstants {
protected const TWO = 'two';
private const THREE = 'three';
}

// https://wiki.php.net/rfc/dnf_types
function generateSlug((HasTitle&HasId)|null $post)
{
throw new \Exception('not implemented');
}
45 changes: 45 additions & 0 deletions tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,49 @@ public static function provideProcess81Cases(): iterable
],
];
}

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

public static function provideProcess82Cases(): iterable
{
yield 'disjunctive normal form types parameter' => [
'<?php function foo((A&B)|D $x): void {}',
[
10 => CT::T_TYPE_ALTERNATION,
],
];

yield 'disjunctive normal form types return' => [
'<?php function foo(): (A&B)|D {}',
[
13 => CT::T_TYPE_ALTERNATION,
],
];

yield 'disjunctive normal form types parameters' => [
'<?php function foo(
(A&B)|C|D $x,
A|(B&C)|D $y,
A|B|(C&D) $z,
): void {}',
[
11 => CT::T_TYPE_ALTERNATION,
13 => CT::T_TYPE_ALTERNATION,
20 => CT::T_TYPE_ALTERNATION,
26 => CT::T_TYPE_ALTERNATION,
33 => CT::T_TYPE_ALTERNATION,
35 => 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 @@ -323,4 +323,47 @@ function f( #[Target(\'a\')] #[Target(\'b\')] #[Target(\'c\')] #[Target(\'d\')]
],
];
}

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

public static function provideProcess82Cases(): iterable
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
{
yield 'disjunctive normal form types parameter' => [
'<?php function foo((A&B)|D $x): void {}',
[
7 => CT::T_TYPE_INTERSECTION,
],
];

yield 'disjunctive normal form types return' => [
'<?php function foo(): (A&B)|D {}',
[
10 => CT::T_TYPE_INTERSECTION,
],
];

yield 'disjunctive normal form types parameters' => [
'<?php function foo(
(A&B)|C|D $x,
A|(B&C)|D $y,
(A&B)|(C&D) $z,
): void {}',
[
8 => CT::T_TYPE_INTERSECTION,
23 => CT::T_TYPE_INTERSECTION,
34 => CT::T_TYPE_INTERSECTION,
40 => CT::T_TYPE_INTERSECTION,
],
];
}
}