Skip to content

Commit

Permalink
Make anonymous providers serialisable
Browse files Browse the repository at this point in the history
Replaced anonymous class with ProviderProvider in ProviderSetProvider. This makes it serialisable.This improves code reuse and readability by consolidating instance retrieval logic into a dedicated class.
  • Loading branch information
koriym committed Sep 6, 2024
1 parent f6f39ef commit 4a5009a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
30 changes: 30 additions & 0 deletions src/di/ProviderProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ray\Di;

use Ray\Di\Di\Set;

final class ProviderProvider implements ProviderInterface

Check failure on line 9 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Class Ray\Di\ProviderProvider implements generic interface Ray\Di\ProviderInterface but does not specify its types: T
{
/** @var InjectorInterface */
private $injector;

/** @var InjectorInterface */
private $interface;

Check failure on line 15 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Property Ray\Di\ProviderProvider::$interface is unused.

Check failure on line 15 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

PropertyNotSetInConstructor: Property Ray\Di\ProviderProvider::$interface is not defined in constructor of Ray\Di\ProviderProvider or in any private or final methods called in the constructor

/** @var Set */
private $set;

Check failure on line 18 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Property Ray\Di\ProviderProvider::$set with generic class Ray\Di\Di\Set does not specify its types: T

public function __construct(InjectorInterface $injector, Set $set)

Check failure on line 20 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Method Ray\Di\ProviderProvider::__construct() has parameter $set with generic class Ray\Di\Di\Set but does not specify its types: T
{
$this->injector = $injector;
$this->set = $set;
}

public function get()

Check failure on line 26 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Method \Ray\Di\ProviderProvider::get() does not have return type hint nor @return annotation for its return value.
{
return $this->injector->getInstance($this->set->interface, $this->set->name);

Check failure on line 28 in src/di/ProviderProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedReturnStatement: Possibly-mixed return value
}
}
26 changes: 1 addition & 25 deletions src/di/ProviderSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ public function get()
throw new SetNotFound((string) $this->ip->getParameter());
}

return new class ($this->injector, $set) implements ProviderInterface
{
/** @var InjectorInterface */
private $injector;

/** @var Set<object> */
private $set;

/**
* @param Set<object> $set
*/
public function __construct(InjectorInterface $injector, Set $set)
{
$this->injector = $injector;
$this->set = $set;
}

/**
* @return mixed
*/
public function get()
{
return $this->injector->getInstance($this->set->interface, $this->set->name);
}
};
return new ProviderProvider($this->injector, $set);
}
}

0 comments on commit 4a5009a

Please sign in to comment.