Inflector   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 112
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getType() 0 4 1
A invokeMethod() 0 6 1
A invokeMethods() 0 8 2
A setProperty() 0 6 1
A setProperties() 0 8 2
A inflect() 0 19 4
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Inflector;
4
5
use League\Container\ContainerAwareTrait;
6
use League\Container\Argument\ArgumentResolverInterface;
7
use League\Container\Argument\ArgumentResolverTrait;
8
9
class Inflector implements ArgumentResolverInterface, InflectorInterface
10
{
11
    use ArgumentResolverTrait;
12
    use ContainerAwareTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $type;
18
19
    /**
20
     * @var callable
21
     */
22
    protected $callback;
23
24
    /**
25
     * @var array
26
     */
27
    protected $methods = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $properties = [];
33
34
    /**
35
     * Construct.
36
     *
37
     * @param string        $type
38
     * @param callable|null $callback
39
     */
40 27
    public function __construct(string $type, callable $callback = null)
41
    {
42 27
        $this->type     = $type;
43 27
        $this->callback = $callback;
44 27
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 9
    public function getType() : string
50
    {
51 9
        return $this->type;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 9
    public function invokeMethod(string $name, array $args) : InflectorInterface
58
    {
59 9
        $this->methods[$name] = $args;
60
61 9
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 3
    public function invokeMethods(array $methods) : InflectorInterface
68
    {
69 3
        foreach ($methods as $name => $args) {
70 3
            $this->invokeMethod($name, $args);
71
        }
72
73 3
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 9
    public function setProperty(string $property, $value) : InflectorInterface
80
    {
81 9
        $this->properties[$property] = $this->resolveArguments([$value])[0];
82
83 9
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function setProperties(array $properties) : InflectorInterface
90
    {
91 3
        foreach ($properties as $property => $value) {
92 3
            $this->setProperty($property, $value);
93
        }
94
95 3
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 15
    public function inflect($object)
102
    {
103 15
        $properties = $this->resolveArguments(array_values($this->properties));
104 15
        $properties = array_combine(array_keys($this->properties), $properties);
105
106 15
        foreach ($properties as $property => $value) {
107 6
            $object->{$property} = $value;
108
        }
109
110 15
        foreach ($this->methods as $method => $args) {
111 6
            $args = $this->resolveArguments($args);
112
113 6
            call_user_func_array([$object, $method], $args);
114
        }
115
116 15
        if (! is_null($this->callback)) {
117 3
            call_user_func_array($this->callback, [$object]);
118
        }
119 15
    }
120
}
121