Skip to content

Commit 51e0b7d

Browse files
feat: Php8.1 update.
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
1 parent 404e48f commit 51e0b7d

10 files changed

Lines changed: 232 additions & 298 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Jitesoft
3+
Copyright (c) Jitesoft
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"description": "PSR-11 container with constructor injection.",
44
"type": "library",
55
"require": {
6-
"php": ">=8",
6+
"php": ">=8.1",
77
"psr/container": "^2",
88
"jitesoft/exceptions": "^2.4"
99
},
1010
"require-dev": {
1111
"phpunit/phpunit": "^9.5",
12-
"squizlabs/php_codesniffer": "^3.6"
12+
"squizlabs/php_codesniffer": "^3.7"
1313
},
1414
"license": "MIT",
1515
"authors": [

composer.lock

Lines changed: 159 additions & 239 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Container.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33
Container.php - Part of the container project.
44
5-
© - Jitesoft 2017
5+
© - Jitesoft
66
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
77
namespace Jitesoft\Container;
88

9-
use ArrayAccess;
109
use Jitesoft\Exceptions\Psr\Container\ContainerException;
1110
use Jitesoft\Exceptions\Psr\Container\NotFoundException;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Psr\Container\NotFoundExceptionInterface;
1213

1314
/**
1415
* Simple naive implementation of a Dependency container with constructor injection.
@@ -57,7 +58,7 @@ public function clear(): void {
5758
* @param boolean $singleton If the created object is intended to be treated as a single instance on creation.
5859
*
5960
* @return boolean
60-
* @throws ContainerException Thrown in case the entry already exist.
61+
* @throws ContainerException|ContainerExceptionInterface Thrown in case the entry already exist.
6162
*/
6263
public function set(string $abstract,
6364
mixed $concrete,
@@ -85,7 +86,8 @@ public function set(string $abstract,
8586
* @param mixed $concrete Concrete value to bind to the abstract value.
8687
* @param boolean $singleton If the created object is intended to be treated as a single instance on creation.
8788
*
88-
* @throws NotFoundException Thrown in case the 'abstract' does not exist.
89+
* @throws NotFoundException|NotFoundExceptionInterface Thrown in case the 'abstract' does not exist.
90+
* @throws ContainerException|ContainerExceptionInterface Thrown in case entry could not be set.
8991
*
9092
* @return void
9193
*/
@@ -104,13 +106,13 @@ public function rebind(string $abstract,
104106
/**
105107
* Unset a given abstract removing it from the container.
106108
*
107-
* @param string $id Identifier of the value to remove entry for.
109+
* @param string|mixed $id Identifier of the value to remove entry for.
108110
*
109-
* @throws NotFoundException Thrown if the abstract is not found.
111+
* @throws NotFoundException|NotFoundExceptionInterface Thrown if the abstract is not found.
110112
*
111113
* @return void
112114
*/
113-
public function unset(string $id): void {
115+
public function unset(mixed $id): void {
114116
if (!$this->has($id)) {
115117
throw new NotFoundException(
116118
'Could not remove the given entity because it was not set.'
@@ -125,12 +127,12 @@ public function unset(string $id): void {
125127
*
126128
* @param string|mixed $id Identifier of the entry to look for.
127129
*
128-
* @throws NotFoundException No entry was found for **this** identifier.
129-
* @throws ContainerException On resolve error.
130+
* @throws NotFoundException|NotFoundExceptionInterface No entry was found for **this** identifier.
131+
* @throws ContainerException|ContainerExceptionInterface On resolve error.
130132
*
131133
* @return mixed Entry.
132134
*/
133-
public function get($id): mixed {
135+
public function get(mixed $id): mixed {
134136
if (array_key_exists($id, $this->bindings)) {
135137
return $this->bindings[$id]->resolve(new Injector($this));
136138
}
@@ -154,7 +156,7 @@ public function get($id): mixed {
154156
*
155157
* @return boolean
156158
*/
157-
public function has($id): bool {
159+
public function has(mixed $id): bool {
158160
return array_key_exists($id, $this->bindings);
159161
}
160162

@@ -163,42 +165,42 @@ public function has($id): bool {
163165
*
164166
* @return boolean
165167
*/
166-
public function offsetExists($offset): bool {
168+
public function offsetExists(mixed $offset): bool {
167169
return $this->has($offset);
168170
}
169171

170172
/**
171173
* @param string|mixed $offset Offset to fetch.
172174
*
173-
* @throws NotFoundException No entry was found for **this** identifier.
174-
* @throws ContainerException Error while retrieving the entry.
175+
* @throws NotFoundException|NotFoundExceptionInterface No entry was found for **this** identifier.
176+
* @throws ContainerException|ContainerExceptionInterface Error while retrieving the entry.
175177
*
176178
* @return mixed
177179
*/
178-
public function offsetGet($offset): mixed {
180+
public function offsetGet(mixed $offset): mixed {
179181
return $this->get($offset);
180182
}
181183

182184
/**
183185
* @param string|mixed $offset Offset to set.
184186
* @param mixed $value Value to set to the offset.
185187
*
186-
* @throws ContainerException Thrown if offset does not exist.
188+
* @throws ContainerException|ContainerExceptionInterface Thrown if offset does not exist.
187189
*
188190
* @return void
189191
*/
190-
public function offsetSet($offset, $value): void {
192+
public function offsetSet(mixed $offset, mixed $value): void {
191193
$this->set($offset, $value);
192194
}
193195

194196
/**
195197
* @param string|mixed $offset Offset to unset
196198
*
197-
* @throws NotFoundException Thrown if offset does not exist.
199+
* @throws NotFoundException|NotFoundExceptionInterface Thrown if offset does not exist.
198200
*
199201
* @return void
200202
*/
201-
public function offsetUnset($offset): void {
203+
public function offsetUnset(mixed $offset): void {
202204
$this->unset($offset);
203205
}
204206

@@ -212,9 +214,10 @@ public function offsetUnset($offset): void {
212214
* @param mixed $concrete Concrete value to bind to the abstract value.
213215
*
214216
* @return boolean
215-
* @throws ContainerException Thrown in case the entry already exist.
217+
* @throws ContainerException|ContainerExceptionInterface Thrown in case the entry already exist.
216218
*/
217219
public function singleton(string $abstract, mixed $concrete): bool {
218220
return $this->set($abstract, $concrete, true);
219221
}
222+
220223
}

src/ContainerEntry.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33
ContainerEntry.php - Part of the container project.
44
5-
© - Jitesoft 2018
5+
© - Jitesoft
66
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
77
namespace Jitesoft\Container;
88

99
use Jitesoft\Exceptions\Psr\Container\ContainerException;
1010
use Jitesoft\Exceptions\Psr\Container\NotFoundException;
11-
use ReflectionFunction;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Psr\Container\NotFoundExceptionInterface;
13+
use ReflectionException;
1214

1315
/**
1416
* Class ContainerEntry
1517
*
1618
* @internal
1719
*/
1820
class ContainerEntry {
19-
/** @var mixed */
20-
protected $concrete;
21+
protected mixed $concrete;
2122
protected bool $isSingleton;
2223
protected bool $isCreated;
2324
protected string $abstract;
@@ -47,8 +48,9 @@ public function __construct(string $abstract,
4748
*
4849
* @return mixed
4950
*
50-
* @throws ContainerException Thrown in case the container fails in injection.
51-
* @throws NotFoundException Thrown in case the value is not found.
51+
* @throws ContainerException|ContainerExceptionInterface Thrown in case the container fails in injection.
52+
* @throws NotFoundException|NotFoundExceptionInterface Thrown in case the value is not found.
53+
* @throws ReflectionException Thrown if instantiation failed.
5254
* @internal
5355
*/
5456
public function resolve(Injector $injector): mixed {
@@ -64,20 +66,17 @@ public function resolve(Injector $injector): mixed {
6466
}
6567
}
6668

67-
68-
if (is_callable($this->concrete)) {
69+
if (is_callable($this->concrete)) {
6970
$object = $injector->invoke($this->concrete);
7071
if ($this->isSingleton) {
7172
$this->isCreated = true;
72-
$this->concrete = $object;
73+
$this->concrete = $object;
7374
}
7475

7576
return $object;
7677
}
7778

78-
79-
80-
return $object;
79+
return $object ?? null;
8180
}
8281

8382
}

src/ContainerInterface.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
ContainerInterface.php - Part of the container project.
24
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
37
namespace Jitesoft\Container;
48

59
use ArrayAccess;
610
use Jitesoft\Exceptions\Psr\Container\ContainerException;
711
use Jitesoft\Exceptions\Psr\Container\NotFoundException;
12+
use Psr\Container\ContainerExceptionInterface;
813
use Psr\Container\ContainerInterface as PsrContainerInterface;
14+
use Psr\Container\NotFoundExceptionInterface;
915

1016
interface ContainerInterface extends PsrContainerInterface, ArrayAccess {
17+
1118
/**
1219
* Clear the container.
1320
*
@@ -26,9 +33,11 @@ public function clear(): void;
2633
* @param boolean $singleton If the created object is intended to be treated as a single instance on creation.
2734
*
2835
* @return boolean
29-
* @throws ContainerException Thrown in case the entry already exist.
36+
* @throws ContainerException|ContainerExceptionInterface Thrown in case the entry already exist.
3037
*/
31-
public function set(string $abstract, mixed $concrete, bool $singleton = false): bool;
38+
public function set(string $abstract,
39+
mixed $concrete,
40+
bool $singleton = false): bool;
3241

3342
/**
3443
* Re-bind a value to a given abstract.
@@ -38,18 +47,21 @@ public function set(string $abstract, mixed $concrete, bool $singleton = false):
3847
* @param mixed $concrete Concrete value to bind to the abstract value.
3948
* @param boolean $singleton If the created object is intended to be treated as a single instance on creation.
4049
*
41-
* @throws NotFoundException Thrown in case the 'abstract' does not exist.
50+
* @throws NotFoundException|NotFoundExceptionInterface Thrown in case the 'abstract' does not exist.
51+
* @throws ContainerException|ContainerExceptionInterface Thrown in case entry could not be set.
4252
*
4353
* @return void
4454
*/
45-
public function rebind(string $abstract, mixed $concrete, bool $singleton = false): void;
55+
public function rebind(string $abstract,
56+
mixed $concrete,
57+
bool $singleton = false): void;
4658

4759
/**
4860
* Unset a given abstract removing it from the container.
4961
*
5062
* @param string $id Identifier of the value to remove entry for.
5163
*
52-
* @throws NotFoundException Thrown if the abstract is not found.
64+
* @throws NotFoundException|NotFoundExceptionInterface Thrown if the abstract is not found.
5365
*
5466
* @return void
5567
*/
@@ -65,7 +77,8 @@ public function unset(string $id): void;
6577
* @param mixed $concrete Concrete value to bind to the abstract value.
6678
*
6779
* @return boolean
68-
* @throws ContainerException Thrown in case the entry already exist.
80+
* @throws ContainerException|ContainerExceptionInterface Thrown in case the entry already exist.
6981
*/
7082
public function singleton(string $abstract, mixed $concrete): bool;
83+
7184
}

src/Injector.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33
Injector.php - Part of the container project.
44
5-
© - Jitesoft 2018
5+
© - Jitesoft
66
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
77
namespace Jitesoft\Container;
88

99
use Jitesoft\Exceptions\Psr\Container\ContainerException;
1010
use Jitesoft\Exceptions\Psr\Container\NotFoundException;
11+
use Psr\Container\ContainerExceptionInterface;
1112
use Psr\Container\ContainerInterface;
13+
use Psr\Container\NotFoundExceptionInterface;
1214
use ReflectionClass;
1315
use ReflectionException;
1416
use ReflectionFunction;
1517
use ReflectionNamedType;
1618
use ReflectionParameter;
17-
use ReflectionType;
18-
use ReflectionUnionType;
1919

2020
/**
2121
* Class Injector
@@ -48,8 +48,8 @@ public function __construct(?ContainerInterface $container = null) {
4848
* @param string $className Name of the class to create.
4949
* @param array $bindings Key value bindings list. Not required if a container exists.
5050
*
51-
* @throws ContainerException Thrown if the container fails to create class.
52-
* @throws NotFoundException Thrown if type hint was not found.
51+
* @throws ContainerException|ContainerExceptionInterface Thrown if the container fails to create class.
52+
* @throws NotFoundException|NotFoundExceptionInterface Thrown if type hint was not found.
5353
* @throws ReflectionException Thrown if instantiation failed.
5454
*
5555
* @internal
@@ -65,12 +65,12 @@ public function create(string $className, array $bindings = []): object {
6565

6666
// Does the class have a constructor?
6767
if ($class->getConstructor() !== null) {
68-
$ctr = $class->getConstructor();
68+
$ctr = $class->getConstructor();
6969

7070
if ($ctr === null) {
7171
throw new ContainerException(
7272
sprintf(
73-
'Class with name %s does not have a resolvable constructor.',
73+
'Class with name %s does not have a constructor.',
7474
$className
7575
)
7676
);
@@ -107,8 +107,8 @@ public function create(string $className, array $bindings = []): object {
107107
* If a binding array is passed through this method and a container already exists, the bindings will take
108108
* precedence over the container.
109109
*
110-
* @throws ContainerException
111-
* @throws NotFoundException
110+
* @throws ContainerException|ContainerExceptionInterface
111+
* @throws NotFoundException|NotFoundExceptionInterface
112112
*/
113113
public function invoke(callable $callable, array $bindings = []): mixed {
114114
try {
@@ -129,8 +129,8 @@ public function invoke(callable $callable, array $bindings = []): mixed {
129129
*
130130
* @return array List of resolved parameters.
131131
*
132-
* @throws ContainerException Thrown if the container fails to create class.
133-
* @throws NotFoundException Thrown if type hint was not found.
132+
* @throws ContainerException|ContainerExceptionInterface Thrown if the container fails to create class.
133+
* @throws NotFoundException|NotFoundExceptionInterface Thrown if type hint was not found.
134134
*/
135135
private function getParameters(array $params, array $bindings): array {
136136
// Get all the parameters that the class require, if any.
@@ -163,18 +163,17 @@ function ($param) use ($bindings) {
163163
*/
164164
private function getTypeHint(ReflectionParameter $param): string {
165165
$type = $param->getType();
166-
if (!$type || !($type instanceof ReflectionNamedType)) {
167-
/** @noinspection ProperNullCoalescingOperatorUsageInspection */
166+
if (!($type instanceof ReflectionNamedType)) {
167+
// @noinspection ProperNullCoalescingOperatorUsageInspection
168168
throw new NotFoundException(
169169
sprintf(
170170
'Failed to resolve type for parameter %s (type %s).',
171171
$param->getName(),
172-
$type ?? 'null'
172+
$type ?? 'null'
173173
)
174174
);
175175
}
176176

177-
/** @var $type ReflectionNamedType */
178177
return $type->getName();
179178
}
180179

0 commit comments

Comments
 (0)