Issues (71)

src/Container.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL;
6
7
use Closure;
8
use GraphQL\Error\Error;
9
use Override;
0 ignored issues
show
The type Override 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...
10
use Psr\Container\ContainerInterface;
11
use ReflectionClass;
12
use ReflectionException;
13
14
use function assert;
15
use function strtolower;
16
17
/**
18
 * Used in many places to create a container of types and services
19
 */
20
abstract class Container implements ContainerInterface
21
{
22
    /** @var mixed[] */
23
    protected array $register = [];
24
25
    #[Override]
26
    public function has(string $id): bool
27
    {
28
        return isset($this->register[strtolower($id)]);
29
    }
30
31
    /** @throws Error */
32
    #[Override]
33
    public function get(string $id): mixed
34
    {
35
        $id = strtolower($id);
36
37
        if (! $this->has($id)) {
38
            throw new Error($id . ' is not registered');
39
        }
40
41
        if ($this->register[$id] instanceof Closure) {
42
            $closure = $this->register[$id];
43
44
            $this->register[$id] = $closure($this);
45
        }
46
47
        return $this->register[$id];
48
    }
49
50
    /**
51
     * This allows for a duplicate id to overwrite an existing registration
52
     */
53
    public function set(string $id, mixed $value): self
54
    {
55
        $id = strtolower($id);
56
57
        $this->register[$id] = $value;
58
59
        return $this;
60
    }
61
62
    /**
63
     * This function allows for buildable types.  The Type\Connection type is created this way
64
     * because it relies on the entity object type.  To create a custom buildable object type
65
     * it must implement the Buildable interface.
66
     *
67
     * @param class-string $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
68
     *
69
     * @throws Error
70
     * @throws ReflectionException
71
     */
72
    public function build(string $className, string $typeName, mixed ...$params): mixed
73
    {
74
        if ($this->has($typeName)) {
75
            return $this->get($typeName);
76
        }
77
78
        $reflectionClass = new ReflectionClass($className);
79
        assert($reflectionClass->implementsInterface(Buildable::class));
80
81
        return $this
82
            ->set($typeName, new $className($this, $typeName, $params))
83
            ->get($typeName);
84
    }
85
}
86