Skip to content

Commit 5029e4a

Browse files
author
Robin de Graaf
committed
Fix issue with Reflection types, upgrade to phpunit 9
1 parent 466c40a commit 5029e4a

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Parable PHP DI
22

3+
## 0.3.1
4+
5+
_Fixes_
6+
- Small fix for the new php8 reflection types.
7+
38
## 0.3.0
49

510
_Changes_

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ tests: dependencies
99

1010
coverage: dependencies
1111
rm -rf ./coverage
12-
vendor/bin/phpunit --coverage-html ./coverage tests
12+
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests
1313

1414
tests-clean:
1515
vendor/bin/phpunit --verbose tests
16-
17-
coverage-clean:
18-
rm -rf ./coverage
19-
vendor/bin/phpunit --coverage-html ./coverage tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"php": ">=8.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "^8.0"
19+
"phpunit/phpunit": "^9.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

phpunit.xml

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
beStrictAboutTestsThatDoNotTestAnything="true"
6-
beStrictAboutOutputDuringTests="true"
7-
colors="true"
8-
cacheResult="false"
9-
>
10-
<php>
11-
<ini name="memory_limit" value="1024M" />
12-
<ini name="display_errors" value="stdout" />
13-
<ini name="error_log" value="/dev/null" />
14-
</php>
15-
<filter>
16-
<whitelist processUncoveredFilesFromWhitelist="true">
17-
<directory suffix=".php">./src</directory>
18-
</whitelist>
19-
</filter>
20-
</phpunit>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" colors="true" cacheResult="false">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
</coverage>
8+
<php>
9+
<ini name="memory_limit" value="1024M"/>
10+
<ini name="display_errors" value="stdout"/>
11+
<ini name="error_log" value="/dev/null"/>
12+
</php>
13+
</phpunit>

src/Container.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ public function __construct()
2828
}
2929

3030
/**
31-
* Returns a stored instance or creates a new one and stores it.
32-
*
3331
* @throws ContainerException
3432
*/
35-
public function get(string $name)
33+
public function get(string $name): object
3634
{
3735
$name = $this->getDefinitiveName($name);
3836

@@ -54,12 +52,19 @@ public function has(string $name): bool
5452
return isset($this->instances[$name]);
5553
}
5654

55+
public function assertHas(string $name): void
56+
{
57+
if (!$this->has($name)) {
58+
throw NotFoundException::fromName($name);
59+
}
60+
}
61+
5762
/**
5863
* Build a new instance with stored dependencies.
5964
*
6065
* @throws ContainerException
6166
*/
62-
public function build(string $name)
67+
public function build(string $name): object
6368
{
6469
return $this->createInstance($name, self::USE_STORED_DEPENDENCIES);
6570
}
@@ -69,7 +74,7 @@ public function build(string $name)
6974
*
7075
* @throws ContainerException
7176
*/
72-
public function buildAll(string $name)
77+
public function buildAll(string $name): object
7378
{
7479
return $this->createInstance($name, self::USE_NEW_DEPENDENCIES);
7580
}
@@ -79,7 +84,7 @@ public function buildAll(string $name)
7984
*
8085
* @throws ContainerException
8186
*/
82-
protected function createInstance(string $name, int $useStoredDependencies)
87+
protected function createInstance(string $name, int $useStoredDependencies): object
8388
{
8489
$name = $this->getDefinitiveName($name);
8590

@@ -92,8 +97,8 @@ protected function createInstance(string $name, int $useStoredDependencies)
9297

9398
try {
9499
$dependencies = $this->getDependenciesFor($name, $useStoredDependencies);
95-
} catch (Throwable $e) {
96-
throw new ContainerException($e->getMessage());
100+
} catch (Throwable $t) {
101+
throw new ContainerException($t->getMessage(), $t->getCode(), $t);
97102
}
98103

99104
return new $name(...$dependencies);
@@ -120,6 +125,7 @@ protected function getMapIfExists(string $requested): string
120125
* Get the dependencies for an instance, based on the constructor.
121126
* Optionally use stored dependencies or always create new ones.
122127
*
128+
* @return mixed[]
123129
* @throws ContainerException
124130
*/
125131
public function getDependenciesFor(
@@ -135,10 +141,14 @@ public function getDependenciesFor(
135141
*/
136142
$reflection = @new ReflectionClass($name);
137143
} catch (Throwable $t) {
138-
throw new ContainerException(sprintf(
139-
'Could not create instance for class `%s`.',
140-
$name
141-
));
144+
throw new ContainerException(
145+
sprintf(
146+
'Could not create instance for class `%s`.',
147+
$name
148+
),
149+
$t->getCode(),
150+
$t
151+
);
142152
}
143153

144154
$constructor = $reflection->getConstructor();
@@ -153,15 +163,17 @@ public function getDependenciesFor(
153163
foreach ($parameters as $parameter) {
154164
$type = $parameter->getType();
155165

166+
$builtIn = false;
167+
156168
if ($type instanceof ReflectionNamedType) {
157169
$builtIn = $type->isBuiltin();
158-
} else {
159-
$builtIn = false;
160170
}
161171

162-
$class = $parameter->getType() && $builtIn === false
163-
? new ReflectionClass($parameter->getType()->getName())
164-
: null;
172+
$class = null;
173+
174+
if ($parameter->getType() instanceof ReflectionNamedType && $builtIn === false) {
175+
$class = new ReflectionClass($parameter->getType()->getName());
176+
}
165177

166178
if ($class === null) {
167179
if (!$parameter->isOptional()) {
@@ -173,6 +185,7 @@ public function getDependenciesFor(
173185

174186
$dependencies[] = $parameter->getDefaultValue();
175187

188+
// For the foreach loop
176189
continue;
177190
}
178191

@@ -196,15 +209,11 @@ public function getDependenciesFor(
196209
}
197210

198211
/**
199-
* Store the provided instance with the provided id, or the class name of the object.
212+
* Store the provided instance with the provided name, or the class name of the object.
200213
*/
201-
public function store($instance, string $name = null): void
214+
public function store(object $instance, string $name = null): void
202215
{
203-
if ($name === null) {
204-
$name = get_class($instance);
205-
}
206-
207-
$name = $this->getDefinitiveName($name);
216+
$name = $this->getDefinitiveName($name ?? $instance::class);
208217

209218
$this->instances[$name] = $instance;
210219
}
@@ -218,9 +227,7 @@ public function clear(string $name): void
218227
{
219228
$name = $this->getDefinitiveName($name);
220229

221-
if (!$this->has($name)) {
222-
throw NotFoundException::fromId($name);
223-
}
230+
$this->assertHas($name);
224231

225232
unset($this->instances[$name]);
226233

@@ -238,12 +245,11 @@ public function clear(string $name): void
238245
public function clearExcept(array $keep): void
239246
{
240247
$kept = [];
248+
241249
foreach ($keep as $name) {
242250
$name = $this->getDefinitiveName($name);
243251

244-
if (!$this->has($name)) {
245-
throw NotFoundException::fromId($name);
246-
}
252+
$this->assertHas($name);
247253

248254
$kept[$name] = $this->get($name);
249255
}
@@ -269,7 +275,10 @@ protected function storeRelationship(string $class, string $dependency): void
269275
{
270276
$this->relationships[$class][$dependency] = true;
271277

272-
if (isset($this->relationships[$class][$dependency]) && isset($this->relationships[$dependency][$class])) {
278+
if (isset(
279+
$this->relationships[$class][$dependency],
280+
$this->relationships[$dependency][$class]
281+
)) {
273282
throw new ContainerException(sprintf(
274283
'Cyclical dependency found between `%s` and `%s`.',
275284
$class,

src/Exceptions/NotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class NotFoundException extends Exception
88
{
9-
public static function fromId(string $id)
9+
public static function fromName(string $id)
1010
{
1111
return new self(sprintf(
1212
"No instance found stored for `%s`.",

0 commit comments

Comments
 (0)