-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from InvisibleSmiley/issue-228
Make `ServiceLocatorInterface#get()` generic
- Loading branch information
Showing
5 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |