From 23a930c5600055dd8c5195b8a9a8e46a61ec42c3 Mon Sep 17 00:00:00 2001 From: Bram Leeda Date: Mon, 28 Aug 2023 14:45:02 +0200 Subject: [PATCH] Fix the support for array syntax to also support iterators / classes. --- src/Constraint/Typehint/PhpDocParser.php | 3 +- .../IterableTypedFullProperty.php | 28 ++++++++++++++++++ .../data/All/DocComment/IteratorTypedFull.php | 29 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/data/success/Regular/Types/CompoundTypes/IterableTypedFullProperty.php create mode 100644 tests/Unit/Constraint/Typehint/data/All/DocComment/IteratorTypedFull.php diff --git a/src/Constraint/Typehint/PhpDocParser.php b/src/Constraint/Typehint/PhpDocParser.php index 569902d..d187731 100644 --- a/src/Constraint/Typehint/PhpDocParser.php +++ b/src/Constraint/Typehint/PhpDocParser.php @@ -41,7 +41,8 @@ public function getReturnTypehint(string $originalDocComment): ?string return null; } - $docComment = preg_replace('/array<(.*?),\s(.*?)>/', 'array<$1,$2>', $docComment); + // Convert array to array + $docComment = preg_replace('/(\w+)<(.*?),\s(.*?)>/', '$1<$2,$3>', $docComment); if ($docComment === null) { return null; // @codeCoverageIgnore } diff --git a/tests/Integration/data/success/Regular/Types/CompoundTypes/IterableTypedFullProperty.php b/tests/Integration/data/success/Regular/Types/CompoundTypes/IterableTypedFullProperty.php new file mode 100644 index 0000000..7804841 --- /dev/null +++ b/tests/Integration/data/success/Regular/Types/CompoundTypes/IterableTypedFullProperty.php @@ -0,0 +1,28 @@ + */ + private $property = false; + + /** + * @return iterable + */ + public function getProperty(): iterable + { + return $this->property; + } + + /** + * @param iterable $property + */ + public function setProperty(iterable $property): self + { + $this->property = $property; + + return $this; + } +} diff --git a/tests/Unit/Constraint/Typehint/data/All/DocComment/IteratorTypedFull.php b/tests/Unit/Constraint/Typehint/data/All/DocComment/IteratorTypedFull.php new file mode 100644 index 0000000..3133f14 --- /dev/null +++ b/tests/Unit/Constraint/Typehint/data/All/DocComment/IteratorTypedFull.php @@ -0,0 +1,29 @@ + $param + * + * @return ArrayIterator + */ + public function testMethod(ArrayIterator $param): ArrayIterator + { + return $param; + } + + public function getExpectedType(): Type + { + return new Collection(new Fqsen('\\' . ArrayIterator::class), new String_(), new String_()); + } +}