Failed Conditions
Pull Request — master (#3354)
by Michael
39:06 queued 17:31
created

TypeRegistry   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 14
eloc 24
dl 0
loc 99
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A override() 0 11 3
A has() 0 3 1
A get() 0 7 2
A lookupName() 0 9 2
A findTypeName() 0 9 2
A register() 0 11 3
A getMap() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Types;
6
7
use Doctrine\DBAL\DBALException;
8
use function array_search;
9
use function in_array;
10
11
/**
12
 * @internal
13
 */
14
final class TypeRegistry
15
{
16
    /** @var Type[] Map of type names and their corresponding objects. */
17
    private $instances = [];
18
19
    /**
20
     * Factory method to create type instances.
21
     * Type instances are implemented as flyweights.
22
     *
23
     * @throws DBALException
24
     */
25 42289
    public function get(string $name) : Type
26
    {
27 42289
        if (! isset($this->instances[$name])) {
28 24
            throw DBALException::unknownColumnType($name);
29
        }
30
31 42289
        return $this->instances[$name];
32
    }
33
34
    /**
35
     * Find a name for the given type.
36
     *
37
     * @throws DBALException
38
     */
39 24
    public function lookupName(Type $type) : string
40
    {
41 24
        $name = $this->findTypeName($type);
42
43 24
        if ($name === null) {
44 24
            throw DBALException::typeNotRegistered($type);
45
        }
46
47 24
        return $name;
48
    }
49
50
    /**
51
     * Checks if there is a type of the given name.
52
     */
53 3169
    public function has(string $name) : bool
54
    {
55 3169
        return isset($this->instances[$name]);
56
    }
57
58
    /**
59
     * Adds a custom type to the type map.
60
     *
61
     * @throws DBALException
62
     */
63 379
    public function register(string $name, Type $type) : void
64
    {
65 379
        if (isset($this->instances[$name])) {
66 48
            throw DBALException::typeExists($name);
67
        }
68
69 379
        if ($this->findTypeName($type) !== null) {
70
            throw DBALException::typeAlreadyRegistered($type);
71
        }
72
73 379
        $this->instances[$name] = $type;
74 379
    }
75
76
    /**
77
     * Overrides an already defined type to use a different implementation.
78
     *
79
     * @throws DBALException
80
     */
81 96
    public function override(string $name, Type $type) : void
82
    {
83 96
        if (! isset($this->instances[$name])) {
84 24
            throw DBALException::typeNotFound($name);
85
        }
86
87 72
        if (! in_array($this->findTypeName($type), [$name, null], true)) {
88 24
            throw DBALException::typeAlreadyRegistered($type);
89
        }
90
91 48
        $this->instances[$name] = $type;
92 48
    }
93
94
    /**
95
     * Gets the map of all registered types and their corresponding type instances.
96
     *
97
     * @return Type[]
98
     */
99 23610
    public function getMap() : array
100
    {
101 23610
        return $this->instances;
102
    }
103
104 379
    private function findTypeName(Type $type) : ?string
105
    {
106 379
        $name = array_search($type, $this->instances, true);
107
108 379
        if ($name === false) {
109 379
            return null;
110
        }
111
112 72
        return $name;
113
    }
114
}
115