Skip to content

Commit

Permalink
Fix paslm issues (wip)
Browse files Browse the repository at this point in the history
Signed-off-by: tux-rampage <tuxrampage@gmail.com>
  • Loading branch information
tux-rampage committed Oct 22, 2020
1 parent 2606f69 commit 0de1829
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
7 changes: 7 additions & 0 deletions psalm.xml
Expand Up @@ -14,6 +14,13 @@
</projectFiles>

<issueHandlers>
<DeprecatedClass>
<errorLevel type="suppress">
<!-- LaminasTest\Di\Resolver\AbstractInjection is deprecated - psalm should not report its usage there -->
<referencedClass name="Laminas\Di\Resolver\AbstractInjection" />
<referencedClass name="LaminasTest\Di\Resolver\AbstractInjectionTest" />
</errorLevel>
</DeprecatedClass>
<InternalMethod>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
Expand Down
17 changes: 13 additions & 4 deletions src/CodeGenerator/AbstractInjector.php
Expand Up @@ -19,10 +19,10 @@
*/
abstract class AbstractInjector implements InjectorInterface
{
/** @var string[]|FactoryInterface[] */
/** @var array<string, class-string<FactoryInterface>|FactoryInterface> */
protected $factories = [];

/** @var FactoryInterface[] */
/** @var array<string, FactoryInterface> */
private $factoryInstances = [];

/** @var ContainerInterface */
Expand Down Expand Up @@ -51,7 +51,12 @@ private function setFactory(string $type, FactoryInterface $factory): void
{
$this->factoryInstances[$type] = $factory;
}


/**
* @template T
* @param string|class-string<T> $type
* @return FactoryInterface<T>
*/
private function getFactory(string $type): FactoryInterface
{
if (isset($this->factoryInstances[$type])) {
Expand All @@ -71,7 +76,11 @@ public function canCreate(string $name): bool
return isset($this->factories[$name]) || $this->injector->canCreate($name);
}

/** @return mixed */
/**
* @template T
* @param string|class-string<T> $name
* @return T
*/
public function create(string $name, array $options = [])
{
if (isset($this->factories[$name])) {
Expand Down
5 changes: 4 additions & 1 deletion src/CodeGenerator/FactoryInterface.php
Expand Up @@ -12,12 +12,15 @@

use Psr\Container\ContainerInterface;

/**
* @template T extends object
*/
interface FactoryInterface
{
/**
* Create an instance
*
* @return object
* @return T
*/
public function create(ContainerInterface $container, array $options);
}
12 changes: 7 additions & 5 deletions src/Injector.php
Expand Up @@ -118,9 +118,10 @@ public function canCreate(string $name): bool
/**
* Create the instance with auto wiring
*
* @param string $name Class name or service alias
* @template T
* @param string|class-string<T> $name Class name or service alias
* @param array $parameters Constructor parameters, keyed by the parameter name.
* @return object|null
* @return T
* @throws ClassNotFoundException
* @throws RuntimeException
*/
Expand Down Expand Up @@ -150,9 +151,10 @@ public function create(string $name, array $parameters = [])
*
* Any parameters provided will be used as constructor arguments only.
*
* @param string $name The type name to instantiate.
* @template T
* @param string|class-string<T> $name The type name to instantiate.
* @param array $params Constructor arguments, keyed by the parameter name.
* @return object
* @return T
* @throws InvalidCallbackException
* @throws ClassNotFoundException
*/
Expand All @@ -169,7 +171,7 @@ protected function createInstance(string $name, array $params)
));
}

if (! class_exists($class) || interface_exists($class)) {
if (! class_exists($class)) {
throw new ClassNotFoundException(sprintf(
'Class or interface by name %s does not exist',
$class
Expand Down
4 changes: 3 additions & 1 deletion src/InjectorInterface.php
Expand Up @@ -23,8 +23,10 @@ public function canCreate(string $name): bool;
/**
* Create a new instance of a class or alias
*
* @template T extends object
* @param string|class-string<T> $name
* @param array $options Parameters used for instanciation
* @return object The resulting instace
* @return T The resulting instace
* @throws Exception\ExceptionInterface When an error occours during instanciation.
*/
public function create(string $name, array $options = []);
Expand Down
4 changes: 2 additions & 2 deletions test/Resolver/AbstractInjectionTest.php
Expand Up @@ -19,9 +19,9 @@

class AbstractInjectionTest extends TestCase
{
public function testUsageIsDeprecated()
public function testUsageIsDeprecated(): void
{
$this->expectDeprecation(Deprecated::class);
$this->expectDeprecation();
$this->expectDeprecationMessage(sprintf(
'%s is deprecated, please migrate to %s',
AbstractInjection::class,
Expand Down

0 comments on commit 0de1829

Please sign in to comment.