Skip to content

Commit

Permalink
Merge pull request #260 from gsteel/form-element-manager-template
Browse files Browse the repository at this point in the history
Improve type inference for FormElementManager::get()
  • Loading branch information
Xerkus authored Jan 10, 2024
2 parents 1c9ac51 + 60f1cea commit ee4a3b6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@
<code>Element\DateTime::class</code>
<code>Element\DateTime::class</code>
</DeprecatedClass>
<LessSpecificReturnStatement>
<code>parent::get($name, $options)</code>
</LessSpecificReturnStatement>
<NonInvariantDocblockPropertyType>
<code>$aliases</code>
<code>$factories</code>
Expand Down
5 changes: 3 additions & 2 deletions src/FormElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElementInterface>|string $name Service name of plugin to retrieve.
* @template T of ElementInterface
* @param class-string<T>|string $name Service name of plugin to retrieve.
* @param null|array<mixed> $options Options to use when creating the instance.
* @psalm-return ($name is class-string<ElementInterface> ? ElementInterface : mixed)
* @return ($name is class-string<ElementInterface> ? T : ElementInterface)
*/
public function get($name, ?array $options = null): mixed
{
Expand Down
3 changes: 3 additions & 0 deletions test/FormElementManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

Expand All @@ -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());
}
Expand Down
13 changes: 12 additions & 1 deletion test/StaticAnalysis/FormElementManagerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}

0 comments on commit ee4a3b6

Please sign in to comment.