Skip to content

Commit 3630794

Browse files
author
Wazabii
committed
Data types
1 parent 2182fcd commit 3630794

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

Container.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public function __call($method, $args)
2626

2727
/**
2828
* Set a container OR factory
29-
* @param string $identifier Uniq identifier
30-
* @param mixed $value Example:
31-
* TestClasses\Test::class,
32-
* TestClasses\Test::class."::__construct",
33-
* TestClasses\Test::class."::getStaticMethod",
34-
* @param array|null $args Pass argumnets to constructor staticMethod if you choose.
35-
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
29+
* @param string $identifier Uniq identifier
30+
* @param mixed $value Example:
31+
* TestClasses\Test::class,
32+
* TestClasses\Test::class."::__construct",
33+
* TestClasses\Test::class."::getStaticMethod",
34+
* @param array|null $args Pass argumnets to constructor staticMethod if you choose.
35+
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
3636
*/
3737
public function set(string $identifier, $value, ?array $args = null, bool $overwrite = false): ContainerInterface
3838
{
@@ -55,9 +55,9 @@ public function set(string $identifier, $value, ?array $args = null, bool $overw
5555
* @param string $identifier Uniq identifier
5656
* @param callable $factory
5757
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
58-
* @return void
58+
* @return self
5959
*/
60-
public function factory(string $identifier, callable $factory, bool $overwrite = false): ContainerInterface
60+
public function factory(string $identifier, callable $factory, bool $overwrite = false): self
6161
{
6262
if (!$overwrite && $this->has($identifier)) {
6363
if (!$this->isFactory($identifier)) {
@@ -94,7 +94,7 @@ public function has(string $identifier): bool
9494
*/
9595
public function isFactory(string $identifier)
9696
{
97-
return (bool)($this->getService($identifier) instanceof \Closure);
97+
return ($this->getService($identifier) instanceof \Closure);
9898
}
9999

100100
/**
@@ -104,7 +104,7 @@ public function isFactory(string $identifier)
104104
*/
105105
public function isContainer($identifier)
106106
{
107-
return (bool)(!$this->isFactory($identifier));
107+
return (!$this->isFactory($identifier));
108108
}
109109

110110
/**
@@ -113,21 +113,20 @@ public function isContainer($identifier)
113113
* @param array $args Is possible to overwrite/add __construct or method argumnets
114114
* @return mixed
115115
*/
116-
public function get(string $identifier, array $args = [])
116+
public function get(string $identifier, array $args = []): mixed
117117
{
118118
if ($service = $this->getService($identifier)) {
119-
if (is_null($args)) {
119+
if (count($args) === 0) {
120120
$args = $this->getArgs($identifier);
121121
}
122-
123-
if (($service instanceof \Closure)) {
122+
if ($this->isFactory($identifier)) {
124123
$this->getter[$identifier] = $service(...$args);
125124
} else {
126125
if (empty($this->getter[$identifier])) {
126+
/** @var string $service */
127127
if (is_string($service)) {
128-
/** @var string $service */
129128
$reflect = new Reflection($service);
130-
if (!is_null($args)) {
129+
if (count($args) > 0) {
131130
$reflect->setArgs($args);
132131
}
133132
$this->getter[$identifier] = $reflect->get();
@@ -179,10 +178,10 @@ private function getService(string $identifier)
179178
/**
180179
* Get arguments
181180
* @param string $identifier
182-
* @return array|null
181+
* @return array
183182
*/
184-
private function getArgs(string $identifier)
183+
private function getArgs(string $identifier): array
185184
{
186-
return ($this->args[$identifier] ?? null);
185+
return ($this->args[$identifier] ?? []);
187186
}
188187
}

Interfaces/ContainerInterface.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ContainerInterface
1919
*
2020
* @return mixed Entry.
2121
*/
22-
public function get(string $identifier);
22+
public function get(string $identifier, array $args = []): mixed;
2323

2424
/**
2525
* Returns true if the container can return an entry for the given identifier.
@@ -33,4 +33,17 @@ public function get(string $identifier);
3333
* @return bool
3434
*/
3535
public function has(string $identifier): bool;
36+
37+
38+
/**
39+
* Set a container OR factory
40+
* @param string $identifier Uniq identifier
41+
* @param mixed $value Example:
42+
* TestClasses\Test::class,
43+
* TestClasses\Test::class."::__construct",
44+
* TestClasses\Test::class."::getStaticMethod",
45+
* @param array|null $args Pass argumnets to constructor staticMethod if you choose.
46+
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
47+
*/
48+
public function set(string $identifier, $value, ?array $args = null, bool $overwrite = false): ContainerInterface;
3649
}

Interfaces/FactoryInterface.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
interface FactoryInterface
1111
{
1212
/**
13-
* Add a single factory.
14-
*
15-
* @param string $identifier The id
16-
* @param callable $factory The callable
17-
* @param bool $factory The callable
18-
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
19-
* @return void
13+
* Same as @set, BUT will only accept a factory
14+
* @param string $identifier Uniq identifier
15+
* @param callable $factory
16+
* @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
17+
* @return self
2018
*/
21-
public function factory(string $identifier, callable $factory, bool $overwrite = false): ContainerInterface;
19+
public function factory(string $identifier, callable $factory, bool $overwrite = false);
2220
}

0 commit comments

Comments
 (0)