From 803eb8585f9540b3c50118817179cced6b58a4b3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 22 Oct 2022 22:17:19 +0200 Subject: [PATCH 1/4] Immutable refactoring --- src/Hooks/TestCaseHandler.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index dd1e6b6..1f0f83c 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -164,8 +164,7 @@ class TestCaseHandler implements } foreach ($specials['dataProvider'] as $line => $provider) { - $provider_docblock_location = clone $method_storage->location; - $provider_docblock_location->setCommentLine($line); + $provider_docblock_location = $method_storage->location->setCommentLine($line); if (false !== strpos($provider, '::')) { [$class_name, $method_id] = explode('::', $provider); @@ -328,9 +327,8 @@ class TestCaseHandler implements $provider_return_type_string, $provider_docblock_location ): void { - $param_type = clone $param_type; if ($is_optional) { - $param_type->possibly_undefined = true; + $param_type = $param_type->setPossiblyUndefined(true); } if ($codebase->isTypeContainedByType($potential_argument_type, $param_type)) { // ok From e5889774b0520a0a87365fd75b94707a32a97ae8 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 31 Oct 2022 21:42:45 +0100 Subject: [PATCH 2/4] BC fixes --- src/Hooks/TestCaseHandler.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index 1f0f83c..f6b4e5a 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -8,10 +8,12 @@ use PhpParser\Comment\Doc; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use Psalm\Codebase; +use Psalm\CodeLocation; use Psalm\DocComment; use Psalm\Exception\DocblockParseException; use Psalm\IssueBuffer; use Psalm\Issue; +use Psalm\PhpUnitPlugin\VersionUtils; use Psalm\Plugin\EventHandler\AfterClassLikeAnalysisInterface; use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface; use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface; @@ -21,6 +23,7 @@ use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent; use Psalm\Storage\ClassLikeStorage; use Psalm\Type; use Psalm\Type\Atomic\TNull; +use Psalm\Type\Union; use RuntimeException; class TestCaseHandler implements @@ -164,7 +167,13 @@ class TestCaseHandler implements } foreach ($specials['dataProvider'] as $line => $provider) { - $provider_docblock_location = $method_storage->location->setCommentLine($line); + if (VersionUtils::packageVersionIs('vimeo/psalm', '>=', '5.0')) { + /** @var CodeLocation */ + $provider_docblock_location = $method_storage->location->setCommentLine($line); + } else { + $provider_docblock_location = clone $method_storage->location; + $provider_docblock_location->setCommentLine($line); + } if (false !== strpos($provider, '::')) { [$class_name, $method_id] = explode('::', $provider); @@ -328,7 +337,13 @@ class TestCaseHandler implements $provider_docblock_location ): void { if ($is_optional) { - $param_type = $param_type->setPossiblyUndefined(true); + if (method_exists($param_type, 'setPossiblyUndefined')) { + /** @var Union */ + $param_type = $param_type->setPossiblyUndefined(true); + } else { + $param_type = clone $param_type; + $param_type->possibly_undefined = true; + } } if ($codebase->isTypeContainedByType($potential_argument_type, $param_type)) { // ok From 1421076bc80ed60258a8a3291dad2a94c9758f12 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 31 Oct 2022 21:44:32 +0100 Subject: [PATCH 3/4] Improve --- src/Hooks/TestCaseHandler.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index f6b4e5a..0ecf897 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -167,12 +167,12 @@ class TestCaseHandler implements } foreach ($specials['dataProvider'] as $line => $provider) { - if (VersionUtils::packageVersionIs('vimeo/psalm', '>=', '5.0')) { - /** @var CodeLocation */ - $provider_docblock_location = $method_storage->location->setCommentLine($line); - } else { + if (VersionUtils::packageVersionIs('vimeo/psalm', '<', '5.0')) { $provider_docblock_location = clone $method_storage->location; $provider_docblock_location->setCommentLine($line); + } else { + /** @var CodeLocation */ + $provider_docblock_location = $method_storage->location->setCommentLine($line); } if (false !== strpos($provider, '::')) { From 9044b30475630b2498b97f742943610ccef3ee1a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 1 Nov 2022 13:40:16 +0100 Subject: [PATCH 4/4] Fix --- src/Hooks/TestCaseHandler.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index 0ecf897..57a64b7 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -337,12 +337,12 @@ class TestCaseHandler implements $provider_docblock_location ): void { if ($is_optional) { - if (method_exists($param_type, 'setPossiblyUndefined')) { - /** @var Union */ - $param_type = $param_type->setPossiblyUndefined(true); - } else { + if (VersionUtils::packageVersionIs('vimeo/psalm', '<', '5.0')) { $param_type = clone $param_type; $param_type->possibly_undefined = true; + } else { + /** @var Union */ + $param_type = $param_type->setPossiblyUndefined(true); } } if ($codebase->isTypeContainedByType($potential_argument_type, $param_type)) {