From e7cc0a11171c102061cae603c9978adbd5b62b50 Mon Sep 17 00:00:00 2001 From: George Steel Date: Wed, 10 Jan 2024 11:16:11 +0000 Subject: [PATCH] Refine types for `InputFilterPluginManager::get` and add additional SA test Signed-off-by: George Steel --- psalm-baseline.xml | 2 +- psalm.xml.dist | 5 +++++ src/InputFilterPluginManager.php | 9 ++++++--- test/StaticAnalysis/InputFilterPluginManagerType.php | 5 +++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e7036ac2..a8d27fd5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -282,7 +282,7 @@ get - ? T : InputInterface|InputFilterInterface)]]> + ? T1 : ($name is class-string ? T2 : InputInterface|InputFilterInterface))]]> parent::get($name, $options) diff --git a/psalm.xml.dist b/psalm.xml.dist index f495a985..22355666 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -28,6 +28,11 @@ + + + + + diff --git a/src/InputFilterPluginManager.php b/src/InputFilterPluginManager.php index 4e5101e4..d7b38d20 100644 --- a/src/InputFilterPluginManager.php +++ b/src/InputFilterPluginManager.php @@ -190,9 +190,12 @@ public function validatePlugin($plugin) /** * @inheritDoc - * @template T of InputInterface|InputFilterInterface - * @param class-string|string $name - * @return ($name is class-string ? T : InputInterface|InputFilterInterface) + * phpcs:disable Generic.Files.LineLength.TooLong + * // Template constraint required or we get mixed added to output. Two templates because union does not work + * @template T1 of InputInterface + * @template T2 of InputFilterInterface + * @param class-string|class-string|string $name + * @return ($name is class-string ? T1 : ($name is class-string ? T2 : InputInterface|InputFilterInterface)) */ public function get($name, ?array $options = null) { diff --git a/test/StaticAnalysis/InputFilterPluginManagerType.php b/test/StaticAnalysis/InputFilterPluginManagerType.php index 5c9dd2c6..30c9476b 100644 --- a/test/StaticAnalysis/InputFilterPluginManagerType.php +++ b/test/StaticAnalysis/InputFilterPluginManagerType.php @@ -24,4 +24,9 @@ public function getWithFQCNWillReturnTheObjectOfType(): InputFilterWithTemplated { return $this->manager->get(InputFilterWithTemplatedValues::class); } + + public function getInvalidFQCNReturnsFallbackType(): InputInterface|InputFilterInterface + { + return $this->manager->get(self::class); + } }