diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 966d560a..5a34f811 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -241,6 +241,9 @@ Element\DateTime::class Element\DateTime::class + + parent::get($name, $options) + $aliases $factories diff --git a/src/FormElementManager.php b/src/FormElementManager.php index 41f75aa6..cc7a99a2 100644 --- a/src/FormElementManager.php +++ b/src/FormElementManager.php @@ -321,9 +321,10 @@ public function configure(array $config) * createFromInvokable() will use these and pass them to the instance * constructor if not null and a non-empty array. * - * @param class-string|string $name Service name of plugin to retrieve. + * @template T of ElementInterface + * @param class-string|string $name Service name of plugin to retrieve. * @param null|array $options Options to use when creating the instance. - * @psalm-return ($name is class-string ? ElementInterface : mixed) + * @return ($name is class-string ? T : ElementInterface) */ public function get($name, ?array $options = null): mixed { diff --git a/test/FormElementManagerTest.php b/test/FormElementManagerTest.php index 9a5bdf49..2b62eefa 100644 --- a/test/FormElementManagerTest.php +++ b/test/FormElementManagerTest.php @@ -25,6 +25,7 @@ use function array_pop; use function array_shift; +use function assert; use function count; use function method_exists; use function strtoupper; @@ -44,6 +45,7 @@ protected function setUp(): void public function testInjectToFormFactoryAware(): void { $form = $this->manager->get('Form'); + assert($form instanceof Form); self::assertSame($this->manager, $form->getFormFactory()->getFormElementManager()); } @@ -59,6 +61,7 @@ public function testInjectsFormElementManagerToFormComposedByFormFactoryAwareEle return $form; }); $form = $this->manager->get('my-form'); + assert($form instanceof Form); self::assertSame($factory, $form->getFormFactory()); self::assertSame($this->manager, $form->getFormFactory()->getFormElementManager()); } diff --git a/test/StaticAnalysis/FormElementManagerType.php b/test/StaticAnalysis/FormElementManagerType.php index faf203da..fc442655 100644 --- a/test/StaticAnalysis/FormElementManagerType.php +++ b/test/StaticAnalysis/FormElementManagerType.php @@ -7,6 +7,7 @@ use Laminas\Form\Element\Checkbox; use Laminas\Form\ElementInterface; use Laminas\Form\FormElementManager; +use LaminasTest\Form\TestAsset\NewProductForm; final class FormElementManagerType { @@ -19,8 +20,18 @@ public function getReturnsAnElementInterfaceWhenGivenAClassString(): ElementInte return $this->manager->get(Checkbox::class); } - public function getReturnsMixedWhenGivenAnAlias(): mixed + public function getReturnsElementInterfaceWhenGivenAnAlias(): ElementInterface { return $this->manager->get('foo'); } + + public function getReturnsObjectOfClassWhenGivenFQCN(): NewProductForm + { + return $this->manager->get(NewProductForm::class); + } + + public function getReturnsElementInterfaceWhenGivenInvalidClassStringType(): ElementInterface + { + return $this->manager->get(self::class); + } }