Completed
Push — master ( 5a86e8...bb2d28 )
by Nate
03:17
created

ConstructorConstructor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 45
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 18 5
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Gson\Internal;
8
9
use Tebru\Gson\InstanceCreator;
10
use Tebru\Gson\Internal\ObjectConstructor\CreateFromInstanceCreator;
11
use Tebru\Gson\Internal\ObjectConstructor\CreateFromReflectionClass;
12
use Tebru\Gson\Internal\ObjectConstructor\CreateWithoutArguments;
13
use Tebru\Gson\PhpType;
14
use Throwable;
15
16
/**
17
 * Class ConstructorConstructor
18
 *
19
 * This class acts as an ObjectConstructor factory.  It takes in a map of instance creators and
20
 * wraps object creation in an [@see ObjectConstructor].  This does expensive operations
21
 * (like reflection) once and allows it to be cached for subsequent calls.
22
 *
23
 * @author Nate Brunette <[email protected]>
24
 */
25
final class ConstructorConstructor
26
{
27
    /**
28
     * An array of [@see InstanceCreator] objects that can be used
29
     * for custom instantiation of a class
30
     *
31
     * @var InstanceCreator[]
32
     */
33
    private $instanceCreators;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param InstanceCreator[] $instanceCreators
39
     */
40 4
    public function __construct(array $instanceCreators = [])
41
    {
42 4
        $this->instanceCreators = $instanceCreators;
43 4
    }
44
45
    /**
46
     * Get the correct [@see ObjectConstructor] to use
47
     *
48
     * @param PhpType $type
49
     * @return ObjectConstructor
50
     */
51 3
    public function get(PhpType $type): ObjectConstructor
52
    {
53 3
        $class = $type->getType();
54 3
        foreach ($this->instanceCreators as $instanceCreatorClass => $creator) {
55 1
            if ($type->isA($instanceCreatorClass) && array_key_exists($class, $this->instanceCreators)) {
56 1
                return new CreateFromInstanceCreator($this->instanceCreators[$class], $type);
57
            }
58
        }
59
60
        try {
61
            // attempt to instantiate a new class without any arguments
62 2
            new $class();
63
64 1
            return new CreateWithoutArguments($class);
65 1
        } catch (Throwable $throwable) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
66 1
            return new CreateFromReflectionClass($class);
67
        }
68
    }
69
}
70