Skip to content

Commit b3d4e59

Browse files
author
Wazabii
committed
Structural improvement
1 parent 3630794 commit b3d4e59

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

Container.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public function get(string $identifier, array $args = []): mixed
123123
$this->getter[$identifier] = $service(...$args);
124124
} else {
125125
if (empty($this->getter[$identifier])) {
126-
/** @var string $service */
127-
if (is_string($service)) {
126+
if (is_string($service) && class_exists($service)) {
128127
$reflect = new Reflection($service);
129128
if (count($args) > 0) {
130129
$reflect->setArgs($args);
@@ -168,9 +167,9 @@ public function fetch(string $identifier)
168167
/**
169168
* Get services
170169
* @param string $identifier
171-
* @return array|null
170+
* @return mixed
172171
*/
173-
private function getService(string $identifier)
172+
private function getService(string $identifier): mixed
174173
{
175174
return ($this->services[$identifier] ?? null);
176175
}

Reflection.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ class Reflection
2222

2323
/**
2424
* Start relection of a class or method
25-
* @param string|object $data
25+
* @param class-string|object $classData
2626
*/
27-
public function __construct($data)
27+
public function __construct(string|object $classData)
2828
{
29-
$class = $data;
30-
if (is_string($data) && ($pos = strpos($data, "::")) !== false) {
31-
$class = substr($data, 0, $pos);
32-
$this->method = substr($data, $pos + 2);
29+
if (is_string($classData)) {
30+
if (($pos = strpos($classData, "::")) !== false) {
31+
$classData = substr($classData, 0, $pos);
32+
$this->method = substr($classData, $pos + 2);
33+
}
34+
if (!class_exists($classData)) {
35+
throw new NotFoundException("Could not find the class \"{$classData}\".", 1);
36+
}
3337
}
34-
$this->reflect = new ReflectionClass($class);
38+
$this->reflect = new ReflectionClass($classData);
3539
}
3640

3741
/**
@@ -81,8 +85,8 @@ public function dependencyInjector(): object
8185

8286
/**
8387
* This will return reflection if class exist or error pointing to file where error existed,
84-
* @param string $className
85-
* @param string $fromClass
88+
* @param class-string $className
89+
* @param class-string $fromClass
8690
* @return ReflectionClass
8791
*/
8892
private function initReclusiveReflect(string $className, string $fromClass): ReflectionClass
@@ -100,9 +104,9 @@ private function initReclusiveReflect(string $className, string $fromClass): Ref
100104

101105
/**
102106
* Recursion inject dependancies
103-
* @param array $params
104-
* @param string $fromClass
105-
* @param array $_args
107+
* @param array $params
108+
* @param class-string $fromClass
109+
* @param array $_args
106110
* @return array
107111
*/
108112
private function injectRecursion(array $params, string $fromClass, array $_args = array()): array
@@ -111,7 +115,6 @@ private function injectRecursion(array $params, string $fromClass, array $_args
111115
foreach ($params as $param) {
112116
if ($param->getType() && !$param->getType()->isBuiltin()) {
113117
$classNameA = $param->getType()->getName();
114-
115118
$inst = $this->initReclusiveReflect($classNameA, $fromClass);
116119
$reflectParam = array();
117120
$constructor = $inst->getConstructor();
@@ -160,15 +163,15 @@ private function insertInterfaceClasses(ReflectionClass $inst, string $className
160163

161164
/**
162165
* Will make it posible to set same instance in multiple nested classes
163-
* @param ReflectionClass $inst
164-
* @param ReflectionMethod $constructor
165-
* @param string $classNameA
166-
* @param array $reflectParam
166+
* @param ReflectionClass $inst
167+
* @param ReflectionMethod|null $constructor
168+
* @param string $classNameA
169+
* @param array $reflectParam
167170
* @return array
168171
*/
169172
private function insertMultipleNestedClasses(
170173
ReflectionClass $inst,
171-
ReflectionMethod $constructor,
174+
?ReflectionMethod $constructor,
172175
string $classNameA,
173176
array $reflectParam
174177
): array {

0 commit comments

Comments
 (0)