Skip to content

Commit

Permalink
qa: add some more static analysis files regarding plugin managers
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Sep 10, 2023
1 parent de9d8b3 commit fe416be
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/StaticAnalysis/CallablePluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ServiceManager\StaticAnalysis;

use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidArgumentException;

use function is_callable;

/**
* @template-extends AbstractPluginManager<callable>
*/
final class CallablePluginManager extends AbstractPluginManager
{
public function getWhateverPlugin(array|null $options = null): callable
{
if ($options === null) {
return $this->get('foo');
}

return $this->build('foo', $options);
}

public function validateWhateverPlugin(mixed $plugin): callable

Check failure on line 26 in test/StaticAnalysis/CallablePluginManager.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedReturnTypeCoercion

test/StaticAnalysis/CallablePluginManager.php:26:60: MixedReturnTypeCoercion: The declared return type 'callable' for LaminasTest\ServiceManager\StaticAnalysis\CallablePluginManager::validateWhateverPlugin is more specific than the inferred return type 'InstanceType:Laminas\ServiceManager\AbstractPluginManager as mixed' (see https://psalm.dev/197)
{
$this->validate($plugin);
return $plugin;

Check failure on line 29 in test/StaticAnalysis/CallablePluginManager.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedReturnTypeCoercion

test/StaticAnalysis/CallablePluginManager.php:29:16: MixedReturnTypeCoercion: The type 'InstanceType:Laminas\ServiceManager\AbstractPluginManager as mixed' is more general than the declared return type 'callable' for LaminasTest\ServiceManager\StaticAnalysis\CallablePluginManager::validateWhateverPlugin (see https://psalm.dev/197)
}

public function getConcretePlugin(): ConcreteCallablePlugin
{
return self::get(ConcreteCallablePlugin::class);
}

public function buildConcretePlugin(): ConcreteCallablePlugin
{
return self::build(ConcreteCallablePlugin::class);
}

/**
* {@inheritDoc}
*/
public function validate(mixed $instance): void
{
if (! is_callable($instance)) {
throw new InvalidArgumentException('Provided instance is not callable.');
}
}
}
13 changes: 13 additions & 0 deletions test/StaticAnalysis/ConcreteCallablePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ServiceManager\StaticAnalysis;

final class ConcreteCallablePlugin
{
public function __invoke(): mixed
{
return null;
}
}
58 changes: 58 additions & 0 deletions test/StaticAnalysis/UnionPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ServiceManager\StaticAnalysis;

use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidArgumentException;

use function is_callable;

/**
* `laminas-view` HelperPluginManager is providing either an object or a callable and thus needs to provide a union
* return type.
*
* @psalm-type CallableObjectType = object&callable
* @template-extends AbstractPluginManager<callable|CallableObjectType>
*/
final class UnionPluginManager extends AbstractPluginManager
{
/**
* @return callable|CallableObjectType
*/
public function getWhateverPlugin(array|null $options = null): callable|object
{
if ($options === null) {
return $this->get('foo');
}

return $this->build('foo', $options);
}

public function validateWhateverPlugin(mixed $plugin): callable|object

Check failure on line 33 in test/StaticAnalysis/UnionPluginManager.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedReturnTypeCoercion

test/StaticAnalysis/UnionPluginManager.php:33:60: MixedReturnTypeCoercion: The declared return type 'callable|object' for LaminasTest\ServiceManager\StaticAnalysis\UnionPluginManager::validateWhateverPlugin is more specific than the inferred return type 'InstanceType:Laminas\ServiceManager\AbstractPluginManager as mixed' (see https://psalm.dev/197)
{
$this->validate($plugin);
return $plugin;

Check failure on line 36 in test/StaticAnalysis/UnionPluginManager.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedReturnTypeCoercion

test/StaticAnalysis/UnionPluginManager.php:36:16: MixedReturnTypeCoercion: The type 'InstanceType:Laminas\ServiceManager\AbstractPluginManager as mixed' is more general than the declared return type 'callable|object' for LaminasTest\ServiceManager\StaticAnalysis\UnionPluginManager::validateWhateverPlugin (see https://psalm.dev/197)
}

public function getConcretePlugin(): ConcreteCallablePlugin
{
return self::get(ConcreteCallablePlugin::class);
}

public function buildConcretePlugin(): ConcreteCallablePlugin
{
return self::build(ConcreteCallablePlugin::class);
}

/**
* {@inheritDoc}
*/
public function validate(mixed $instance): void
{
if (! is_callable($instance)) {
throw new InvalidArgumentException('Provided instance is not callable.');
}
}
}

0 comments on commit fe416be

Please sign in to comment.