Instantiator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setReflectionClass() 0 4 1
A getReflectionClass() 0 7 2
A countConstructorArgs() 0 7 2
A isInstantiable() 0 4 1
A isInstantiableWithoutArgs() 0 4 2
A instantiate() 0 12 3
1
<?php
2
/**
3
 * This program is free software. It comes without any warranty, to
4
 * the extent permitted by applicable law. You can redistribute it
5
 * and/or modify it under the terms of the Do What The Fuck You Want
6
 * To Public License, Version 2, as published by Sam Hocevar. See
7
 * http://www.wtfpl.net/ for more details.
8
 */
9
10
declare(strict_types = 1);
11
12
namespace hanneskod\classtools\Instantiator;
13
14
use hanneskod\classtools\Exception\LogicException;
15
16
/**
17
 * Instantiate reflected class
18
 *
19
 * @author Hannes Forsgård <[email protected]>
20
 */
21
class Instantiator
22
{
23
    /**
24
     * @var \ReflectionClass
25
     */
26
    private $class;
27
28
    /**
29
     * Set class to instantiate
30
     */
31
    public function setReflectionClass(\ReflectionClass $class): void
32
    {
33
        $this->class = $class;
34
    }
35
36
    /**
37
     * Get loaded reflection class
38
     *
39
     * @throws LogicException  If reflected class is not loaded
40
     */
41
    public function getReflectionClass(): \ReflectionClass
42
    {
43
        if (!isset($this->class)) {
44
            throw new LogicException("Reflected class not loaded");
45
        }
46
        return $this->class;
47
    }
48
49
    /**
50
     * Get number of required constructor parameters
51
     */
52
    public function countConstructorArgs(): int
53
    {
54
        if ($constructor = $this->class->getConstructor()) {
55
            return $constructor->getNumberOfRequiredParameters();
56
        }
57
        return 0;
58
    }
59
60
    /**
61
     * Check if class is instantiable
62
     */
63
    public function isInstantiable(): bool
64
    {
65
        return $this->getReflectionClass()->isInstantiable();
66
    }
67
68
    /**
69
     * Check if class is instantiable without constructor parameters
70
     */
71
    public function isInstantiableWithoutArgs(): bool
72
    {
73
        return $this->isInstantiable() && !$this->countConstructorArgs();
74
    }
75
76
    /**
77
     * Create instance
78
     *
79
     * @param  array          $args Optional constructor arguments
80
     * @return mixed          Instance of reflected class
81
     * @throws LogicException If reflected class is not instantiable
82
     */
83
    public function instantiate(array $args = array())
84
    {
85
        if (!$this->isInstantiable()) {
86
            throw new LogicException("Reflected class is not instantiable");
87
        }
88
89
        if (count($args) < $this->countConstructorArgs()) {
90
            throw new LogicException("Unable to instantiate, too few constructor arguments");
91
        }
92
93
        return $this->getReflectionClass()->newInstanceArgs($args);
94
    }
95
}
96