Issues (23)

src/Definition/CreateDefinitionsMethods.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of di
5
 *
6
 * For the full copyright and license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Di\Definition;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\NotFoundExceptionInterface;
16
use ReflectionClass;
17
use ReflectionException;
18
use Slick\Di\Container;
19
use Slick\Di\Definition\Attributes\Autowire;
20
use Slick\Di\DefinitionInterface;
0 ignored issues
show
The type Slick\Di\DefinitionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Slick\Di\Inspector\ConstructorArgumentInspector;
0 ignored issues
show
The type Slick\Di\Inspector\ConstructorArgumentInspector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Slick\Di\Inspector\MethodArgumentInspector;
23
24
/**
25
 * CreateDefinitionsMethods
26
 *
27
 * @package Slick\Di\Definition
28
 */
29
trait CreateDefinitionsMethods
30
{
31
32
    /**
33
     * Creates the definition for registered data
34
     *
35
     * If value is a callable then the definition is Factory, otherwise
36
     * it will create a Value definition.
37
     *
38
     * @param callable|mixed $value
39
     * @param array $parameters
40
     *
41
     * @return Value|Alias|Factory
42
     * @see Factory, Value
43
     *
44
     */
45
    protected function createDefinition(
46
        mixed $value,
47
        array $parameters = []
48
    ): Value|Alias|Factory {
49
        if (is_callable($value)) {
50
            return new Factory($value, $parameters);
51
        }
52
        return $this->createValueDefinition($value);
53
    }
54
55
    /**
56
     * Creates a definition for provided name and value pair
57
     *
58
     * If $value is a string prefixed with '@' it will create an Alias
59
     * definition. Otherwise, a Value definition will be created.
60
     *
61
     * @param mixed  $value
62
     *
63
     * @return Value|Alias
64
     */
65
    protected function createValueDefinition(mixed $value): Value|Alias
66
    {
67
        if (is_string($value) && str_contains($value, '@')) {
68
            return new Alias($value);
69
        }
70
71
        return new Value($value);
72
    }
73
74
    /**
75
     * @param string $className
76
     * @param Container $container
77
     * @param mixed ...$arguments
78
     * @return DefinitionInterface
79
     * @throws ReflectionException
80
     * @throws ContainerExceptionInterface
81
     * @throws NotFoundExceptionInterface
82
     */
83
    protected function createFromClass(string $className, Container $container, ...$arguments): DefinitionInterface
84
    {
85
        $definition = (new ObjectDefinition($className))
86
            ->setContainer($container)
87
        ;
88
89
        $classReflection = new ReflectionClass($className);
90
        $arguments = (new ConstructorArgumentInspector(
91
            $classReflection,
92
            $container,
93
            $arguments
94
        ))
95
            ->arguments();
96
        call_user_func_array([$definition, 'with'], $arguments);
97
98
        foreach ($classReflection->getMethods() as $method) {
99
            $autowireAttributes = $method->getAttributes(Autowire::class);
100
            if (empty($autowireAttributes)) {
101
                continue;
102
            }
103
            $attribute = $autowireAttributes[0]->newInstance();
104
            call_user_func_array([$definition, 'call'], [$method->getName()]);
105
            call_user_func_array(
106
                [$definition, 'with'],
107
                (new MethodArgumentInspector($method, $container, $attribute->services()))->arguments()
108
            );
109
        }
110
111
        return $definition;
112
    }
113
}
114