Skip to content

Commit 6f1aff7

Browse files
authored
v1.7.1 Fix autowire helper and implements testsing tool (#10)
1 parent 868ad1d commit 6f1aff7

12 files changed

+38
-179
lines changed

.php-cs-fixer.dist.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

composer.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@
1313
"php": "^8.2",
1414
"micro/dependency-injection": "^1.6.4"
1515
},
16-
"require-dev": {
17-
"ergebnis/composer-normalize": "^2.29",
18-
"friendsofphp/php-cs-fixer": "^3.13",
19-
"phpstan/phpstan": "^1.9",
20-
"phpunit/php-code-coverage": "^9.2",
21-
"phpunit/phpunit": "^9.5",
22-
"vimeo/psalm": "^5.2"
23-
},
2416
"autoload": {
2517
"psr-4": {
2618
"Micro\\Component\\DependencyInjection\\Autowire\\": "src/"
@@ -33,7 +25,8 @@
3325
},
3426
"config": {
3527
"allow-plugins": {
36-
"ergebnis/composer-normalize": true
28+
"ergebnis/composer-normalize": true,
29+
"micro/testing-tool": true
3730
},
3831
"sort-packages": true
3932
},
@@ -56,5 +49,8 @@
5649
"composer normalize",
5750
"@coverage"
5851
]
52+
},
53+
"require-dev": {
54+
"micro/testing-tool": "^1.7"
5955
}
6056
}

phpcs.xml

Lines changed: 0 additions & 63 deletions
This file was deleted.

phpmd.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 0 additions & 4 deletions
This file was deleted.

phpunit.xml.dist

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/AutowireHelper.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function autowire(string|array|callable $target): callable
4646
return new $target(...$arguments);
4747
}
4848

49-
if (\is_object($target) && \is_callable($target)) {
49+
if (\is_object($target)) {
5050
$arguments = $this->resolveArguments([$target, '__invoke']);
5151

5252
return \call_user_func($target, ...$arguments);
@@ -56,14 +56,14 @@ public function autowire(string|array|callable $target): callable
5656
$this->throwAutowireException($target, '');
5757
}
5858

59-
$object = $target[0] ?? null;
60-
$method = $target[1] ?? null;
59+
$object = $target[0] ?? null; // @phpstan-ignore-line
60+
$method = $target[1] ?? null; // @phpstan-ignore-line
6161
$arguments = null;
6262

6363
if (\is_object($object)) {
6464
if (!$method) {
6565
if (!\is_callable($object)) {
66-
$this->throwAutowireException($target, sprintf('Object `%s` is not callable.', $object::class));
66+
$this->throwAutowireException($target, \sprintf('Object `%s` is not callable.', $object::class));
6767
}
6868

6969
if (!($object instanceof \Closure)) {
@@ -117,7 +117,7 @@ protected function resolveStringAsObject(string $target): object
117117
*
118118
* @phpstan-ignore-next-line
119119
*/
120-
protected function throwAutowireException(string|array|callable $target, string $message, \Throwable $parent = null): void
120+
protected function throwAutowireException(string|array|callable $target, string $message, ?\Throwable $parent = null): void
121121
{
122122
if (\is_array($target)) {
123123
$target = $target[0] ?? null;
@@ -131,13 +131,13 @@ protected function throwAutowireException(string|array|callable $target, string
131131
$target = $target::class;
132132
}
133133

134-
throw new AutowireException(sprintf('Can not autowire "%s". %s', $target, $message), 0, $parent);
134+
throw new AutowireException(\sprintf('Can not autowire "%s". %s', $target, $message), 0, $parent);
135135
}
136136

137137
/**
138138
* @phpstan-ignore-next-line
139139
*/
140-
protected function resolveArguments(string|array|object $target, string $method = null): array
140+
protected function resolveArguments(string|array|object $target, ?string $method = null): array
141141
{
142142
if (\is_callable($target) && !$method && !\is_string($target)) {
143143
$ref = new \ReflectionFunction($target); // @phpstan-ignore-line
@@ -162,7 +162,7 @@ protected function resolveArguments(string|array|object $target, string $method
162162
}
163163

164164
/**
165-
* @throws \Psr\Container\ContainerExceptionInterface
165+
* @throws ContainerExceptionInterface
166166
* @throws NotFoundExceptionInterface
167167
*
168168
* @phpstan-ignore-next-line
@@ -177,7 +177,7 @@ private function resolveArgumentsFromReflectionParametersObject(array $reflectio
177177
$parameterName = $parameter->getName();
178178
if (!$parameterType) {
179179
if (!$allowedNull) {
180-
throw new \InvalidArgumentException(sprintf('The untyped argument `%s` cannot be autowired.', $parameterName));
180+
throw new \InvalidArgumentException(\sprintf('The untyped argument `%s` cannot be autowired.', $parameterName));
181181
}
182182

183183
$arguments[] = null;
@@ -188,7 +188,7 @@ private function resolveArgumentsFromReflectionParametersObject(array $reflectio
188188
if (
189189
!$parameterType instanceof \ReflectionNamedType
190190
) {
191-
throw new \InvalidArgumentException(sprintf('The argument `%s` has invalid type.', $parameterName));
191+
throw new \InvalidArgumentException(\sprintf('The argument `%s` has invalid type.', $parameterName));
192192
}
193193

194194
$parameterTypeName = $parameterType->getName();

src/AutowireHelperFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
use Psr\Container\ContainerInterface;
1515

16-
readonly class AutowireHelperFactory implements AutowireHelperFactoryInterface
16+
final readonly class AutowireHelperFactory implements AutowireHelperFactoryInterface
1717
{
1818
public function __construct(private ContainerInterface $container)
1919
{
2020
}
2121

22+
#[\Override]
2223
public function create(): AutowireHelperInterface
2324
{
2425
return new AutowireHelper($this->container);

src/ContainerAutowire.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
use Micro\Component\DependencyInjection\Container;
1515

16-
class ContainerAutowire extends Container
16+
/**
17+
* @psalm-suppress UnusedClass
18+
*/
19+
final class ContainerAutowire extends Container
1720
{
1821
private AutowireHelperFactoryInterface $autowireHelperFactory;
1922

@@ -22,23 +25,27 @@ public function __construct(private readonly Container $container)
2225
$this->autowireHelperFactory = new AutowireHelperFactory($this->container);
2326
}
2427

28+
#[\Override]
2529
public function get(string $id): object
2630
{
2731
return $this->container->get($id);
2832
}
2933

34+
#[\Override]
3035
public function has(string $id): bool
3136
{
3237
return $this->container->has($id);
3338
}
3439

40+
#[\Override]
3541
public function register(string $id, callable $service, bool $force = false): void
3642
{
3743
$autowiredCallback = $this->autowireHelperFactory->create()->autowire($service);
3844

3945
$this->container->register($id, $autowiredCallback, $force);
4046
}
4147

48+
#[\Override]
4249
public function decorate(string $id, callable $service, int $priority = 0): void
4350
{
4451
$autowiredCallback = $this->autowireHelperFactory->create()->autowire($service);

src/Exception/AutowireException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
/**
1919
* @author Stanislau Komar <[email protected]>
20+
*
21+
* @psalm-suppress UnusedClass
2022
*/
21-
class AutowireException extends \RuntimeException implements ContainerExceptionInterface
23+
final class AutowireException extends \RuntimeException implements ContainerExceptionInterface
2224
{
2325
}

0 commit comments

Comments
 (0)