Skip to content

Commit

Permalink
Add static analysis test
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Hatlak <[email protected]>
  • Loading branch information
jhatlak committed Mar 22, 2024
1 parent 85416ee commit a6149b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@
<code><![CDATA[MixedPluginManager]]></code>
</UnusedClass>
</file>
<file src="test/StaticAnalysis/ServiceLocatorInterfaceConsumer.php">
<UnusedClass>
<code><![CDATA[ServiceLocatorInterfaceConsumer]]></code>
</UnusedClass>
</file>
<file src="test/StaticAnalysis/ServiceManagerConfiguration.php">
<UnusedClass>
<code><![CDATA[ServiceManagerConfiguration]]></code>
Expand Down
65 changes: 65 additions & 0 deletions test/StaticAnalysis/ServiceLocatorInterfaceConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace LaminasTest\ServiceManager\StaticAnalysis;

use DateTimeImmutable;
use Laminas\ServiceManager\ServiceLocatorInterface;

use function assert;

final class ServiceLocatorInterfaceConsumer
{
public function canInferTypeFromGet(): void
{
$serviceProvider = $this->getServiceProvider();

$date = $serviceProvider->get(DateTimeImmutable::class);
echo $date->format('Y-m-d H:i:s');

$value = $serviceProvider->get('foo');
assert($value === 'bar');
}

public function canInferTypeFromBuild(): void
{
$serviceProvider = $this->getServiceProvider();

$date = $serviceProvider->build(DateTimeImmutable::class);
echo $date->format('Y-m-d H:i:s');

$value = $serviceProvider->build('foo');
assert($value === 'bar');
}

private function getServiceProvider(): ServiceLocatorInterface
{
$services = [
'foo' => 'bar',
DateTimeImmutable::class => new DateTimeImmutable(),
];
return new class ($services) implements ServiceLocatorInterface {
public function __construct(private readonly array $services)
{
}

public function has(string $id): bool
{
return isset($this->services[$id]);
}

public function build(string $name, ?array $options = null): mixed
{
/** @psalm-suppress MixedReturnStatement Yes indeed, can return mixed. */
return $this->services[$name] ?? null;
}

public function get(string $id): mixed
{
/** @psalm-suppress MixedReturnStatement Yes indeed, can return mixed. */
return $this->services[$id] ?? null;
}
};
}
}

0 comments on commit a6149b9

Please sign in to comment.