From 6bf7ba5282dbbcfe67afa4e7c137cd74dcc66597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Thu, 16 Dec 2021 12:02:37 +0000 Subject: [PATCH] PhpdocTo(Param|Property|Return)TypeFixer - fix for type intersections --- src/DocBlock/TypeExpression.php | 10 +++------- .../Phpdoc/NoSuperfluousPhpdocTagsFixer.php | 4 ++-- tests/DocBlock/AnnotationTest.php | 4 ++-- tests/DocBlock/TypeExpressionTest.php | 4 ++-- .../PhpdocToParamTypeFixerTest.php | 18 ++++++++++++++++++ .../PhpdocToPropertyTypeFixerTest.php | 9 +++++++++ .../PhpdocToReturnTypeFixerTest.php | 18 ++++++++++++++++++ 7 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/DocBlock/TypeExpression.php b/src/DocBlock/TypeExpression.php index 2f3cef808db..1919eb614ef 100644 --- a/src/DocBlock/TypeExpression.php +++ b/src/DocBlock/TypeExpression.php @@ -29,7 +29,7 @@ final class TypeExpression * @internal */ public const REGEX_TYPES = ' - (? # alternation of several types separated by `|` + (? # several types separated by `|` or `&` (? # single type \?? # optionally nullable (?: @@ -88,13 +88,9 @@ final class TypeExpression [\\\\\w-]++ ) ) - (?: # intersection - \h*&\h* - (?&type) - )* ) (?: - \h*\|\h* + \h*[|&]\h* (?&type) )* ) @@ -129,7 +125,7 @@ public function __construct(string $value, ?NamespaceAnalysis $namespace, array $this->types[] = $matches['type']; $value = Preg::replace( - '/^'.preg_quote($matches['type'], '/').'(\h*\|\h*)?/', + '/^'.preg_quote($matches['type'], '/').'(\h*[|&]\h*)?/', '', $value ); diff --git a/src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php b/src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php index b052af28bee..7f919d1fa01 100644 --- a/src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php +++ b/src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php @@ -386,7 +386,7 @@ private function parseTypeHint(Tokens $tokens, int $index): array while (true) { $type = ''; - while ($tokens[$index]->isGivenKind([T_NS_SEPARATOR, T_STATIC, T_STRING, CT::T_ARRAY_TYPEHINT, T_CALLABLE, CT::T_TYPE_INTERSECTION])) { + while ($tokens[$index]->isGivenKind([T_NS_SEPARATOR, T_STATIC, T_STRING, CT::T_ARRAY_TYPEHINT, T_CALLABLE])) { $type .= $tokens[$index]->getContent(); $index = $tokens->getNextMeaningfulToken($index); } @@ -397,7 +397,7 @@ private function parseTypeHint(Tokens $tokens, int $index): array $types[] = $type; - if (!$tokens[$index]->isGivenKind(CT::T_TYPE_ALTERNATION)) { + if (!$tokens[$index]->isGivenKind([CT::T_TYPE_ALTERNATION, CT::T_TYPE_INTERSECTION])) { break; } diff --git a/tests/DocBlock/AnnotationTest.php b/tests/DocBlock/AnnotationTest.php index f63f06bcbac..b412fe1e0f7 100644 --- a/tests/DocBlock/AnnotationTest.php +++ b/tests/DocBlock/AnnotationTest.php @@ -382,11 +382,11 @@ public function provideTypeParsingCases(): array '/** @var class-string */', ], [ - ['A&B'], + ['A', 'B'], '/** @var A&B */', ], [ - ['A & B'], + ['A', 'B'], '/** @var A & B */', ], [ diff --git a/tests/DocBlock/TypeExpressionTest.php b/tests/DocBlock/TypeExpressionTest.php index f947c128f6f..350f3a817ff 100644 --- a/tests/DocBlock/TypeExpressionTest.php +++ b/tests/DocBlock/TypeExpressionTest.php @@ -69,8 +69,8 @@ public function provideGetTypesCases(): \Generator yield ['null|true|false|1|1.5|\'a\'|"b"', ['null', 'true', 'false', '1', '1.5', "'a'", '"b"']]; yield ['int | "a" | A, E>', ['int', '"a"', 'A, E>']]; yield ['class-string', ['class-string']]; - yield ['A&B', ['A&B']]; - yield ['A & B', ['A & B']]; + yield ['A&B', ['A', 'B']]; + yield ['A & B', ['A', 'B']]; yield ['array{1: bool, 2: bool}', ['array{1: bool, 2: bool}']]; yield ['array{a: int|string, b?: bool}', ['array{a: int|string, b?: bool}']]; yield ['array{\'a\': "a", "b"?: \'b\'}', ['array{\'a\': "a", "b"?: \'b\'}']]; diff --git a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php index 5bea348705f..de611e34547 100644 --- a/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php +++ b/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php @@ -416,6 +416,24 @@ function bar() { } ', ], + 'intersection types' => [ + ' [ + ' [ + ' [ + ' [ + ' [ + ' [ + ' [ + ' [ + '