Failed Conditions
Pull Request — master (#3354)
by Michael
28:01 queued 16:46
created

TypeRegistry::lookupName()   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 get_class;
10
11
/**
12
 * @internal
13
 */
14
final class TypeRegistry
15
{
16
    /** @var Type[] Map of already instantiated type objects. One instance per type (flyweight). */
17
    private $typeInstances = [];
18
19
    /** @var string[] Map of defined types and their classes. */
20
    private $typeClasses = [];
21
22
    /**
23
     * Factory method to create type instances.
24
     * Type instances are implemented as flyweights.
25
     *
26
     * @throws DBALException
27
     */
28 40497
    public function get(string $name) : Type
29
    {
30 40497
        if (isset($this->typeInstances[$name])) {
31 40359
            return $this->typeInstances[$name];
32
        }
33
34 202
        if (isset($this->typeClasses[$name])) {
35 202
            return $this->typeInstances[$name] = new $this->typeClasses[$name]();
36
        }
37
38 23
        throw DBALException::unknownColumnType($name);
39
    }
40
41
    /**
42
     * Find a name for the given type.
43
     *
44
     * @throws DBALException
45
     */
46 23
    public function lookupName(Type $type) : string
47
    {
48 23
        $name = array_search($type, $this->typeInstances, true);
49
50 23
        if ($name === false) {
51 23
            throw DBALException::typeNotFound(get_class($type));
52
        }
53
54 23
        return $name;
55
    }
56
57
    /**
58
     * Checks if there is a type of the given name.
59
     */
60 3093
    public function has(string $name) : bool
61
    {
62 3093
        return isset($this->typeClasses[$name]);
63
    }
64
65
    /**
66
     * Adds a custom type to the type map.
67
     *
68
     * @param string $name  The name of the type. This should correspond to what getName() returns.
69
     * @param string $class The class name of the custom type.
70
     *
71
     * @throws DBALException
72
     */
73 248
    public function register(string $name, string $class) : void
74
    {
75 248
        if (isset($this->typeClasses[$name])) {
76 23
            throw DBALException::typeExists($name);
77
        }
78
79 248
        $this->typeClasses[$name] = $class;
80 248
    }
81
82
    /**
83
     * Overrides an already defined type to use a different implementation.
84
     *
85
     * @throws DBALException
86
     */
87 23
    public function override(string $name, string $class) : void
88
    {
89 23
        if (! isset($this->typeClasses[$name])) {
90 23
            throw DBALException::typeNotFound($name);
91
        }
92
93 23
        unset($this->typeInstances[$name]);
94
95 23
        $this->typeClasses[$name] = $class;
96 23
    }
97
98
    /**
99
     * Gets the types array map which holds all registered types and the corresponding
100
     * type class
101
     *
102
     * @return string[]
103
     */
104 22615
    public function getTypeClasses() : array
105
    {
106 22615
        return $this->typeClasses;
107
    }
108
}
109