Passed
Push — type-registry ( c25a28...f90912 )
by Michael
23:20
created

TypeRegistry::findTypeName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
crap 2
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 already instantiated type 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 37063
    public function get(string $name) : Type
26
    {
27 37063
        if (! isset($this->instances[$name])) {
28 21
            throw DBALException::unknownColumnType($name);
29
        }
30
31 37063
        return $this->instances[$name];
32
    }
33
34
    /**
35
     * Find a name for the given type.
36
     *
37
     * @throws DBALException
38
     */
39 21
    public function lookupName(Type $type) : string
40
    {
41 21
        $name = $this->findTypeName($type);
42
43 21
        if ($name === null) {
44 21
            throw DBALException::typeNotRegistered($type);
45
        }
46
47 21
        return $name;
48
    }
49
50
    /**
51
     * Checks if there is a type of the given name.
52
     */
53 2875
    public function has(string $name) : bool
54
    {
55 2875
        return isset($this->instances[$name]);
56
    }
57
58
    /**
59
     * Adds a custom type to the type map.
60
     *
61
     * @throws DBALException
62
     */
63 334
    public function register(string $name, Type $type) : void
64
    {
65 334
        if (isset($this->instances[$name])) {
66 42
            throw DBALException::typeExists($name);
67
        }
68
69 334
        if ($this->findTypeName($type) !== null) {
70
            throw DBALException::typeAlreadyRegistered($type);
71
        }
72
73 334
        $this->instances[$name] = $type;
74 334
    }
75
76
    /**
77
     * Overrides an already defined type to use a different implementation.
78
     *
79
     * @throws DBALException
80
     */
81 84
    public function override(string $name, Type $type) : void
82
    {
83 84
        if (! isset($this->instances[$name])) {
84 21
            throw DBALException::typeNotFound($name);
85
        }
86
87 63
        if (! in_array($this->findTypeName($type), [$name, null], true)) {
88 21
            throw DBALException::typeAlreadyRegistered($type);
89
        }
90
91 42
        $this->instances[$name] = $type;
92 42
    }
93
94
    /**
95
     * Gets the map of all registered types and their corresponding type instances.
96
     *
97
     * @return Type[]
98
     */
99 20645
    public function all() : array
100
    {
101 20645
        return $this->instances;
102
    }
103
104 334
    private function findTypeName(Type $type) : ?string
105
    {
106 334
        $name = array_search($type, $this->instances, true);
107
108 334
        if ($name === false) {
109 334
            return null;
110
        }
111
112 63
        return $name;
113
    }
114
}
115