ResolverFactory   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 197
Duplicated Lines 10.66 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 21
loc 197
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 31 5
A setResolverPrefix() 0 10 2
A resolverPrefix() 0 4 1
A setResolverSuffix() 0 10 2
A resolverSuffix() 0 4 1
A setResolverCapitals() 0 5 1
A resolverCapitals() 0 4 1
A setResolverReplacements() 0 5 1
A resolverReplacements() 0 4 1
A resolve() 5 31 4
A isResolvable() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Charcoal\Factory;
4
5
use InvalidArgumentException;
6
7
use Charcoal\Factory\AbstractFactory;
8
9
/**
10
 * The Resolver Factory resolves the **class name** by different configurably
11
 * methods applied to the **type**.
12
 */
13
class ResolverFactory extends AbstractFactory
14
{
15
    /**
16
     * @var string $resolverPrefix
17
     */
18
    private $resolverPrefix = '';
19
20
    /**
21
     * @var string $resolverSuffix
22
     */
23
    private $resolverSuffix = '';
24
25
    /**
26
     * @var array $resolverCapitals
27
     */
28
    private $resolverCapitals;
29
30
    /**
31
     * @var array $resolverReplacements
32
     */
33
    private $resolverReplacements;
34
35
    /**
36
     * @param array $data Factory arguments.
37
     */
38
    public function __construct(array $data = null)
39
    {
40
        parent::__construct($data);
41
42
        if (!isset($data['resolver_prefix'])) {
43
            $data['resolver_prefix'] = '';
44
        }
45
        if (!isset($data['resolverSuffix'])) {
46
            $data['resolver_suffix'] = '';
47
        }
48 View Code Duplication
        if (!isset($data['resolver_capitals'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $data['resolver_capitals'] = [
50
                '-',
51
                '\\',
52
                '/',
53
                '.',
54
                '_'
55
            ];
56
        }
57 View Code Duplication
        if (!isset($data['resolver_replacements'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            $data['resolver_replacements'] = [
59
                '-'=>'',
60
                '/'=>'\\',
61
                '.'=>'_'
62
            ];
63
        }
64
        $this->setResolverPrefix($data['resolver_prefix']);
65
        $this->setResolverSuffix($data['resolver_suffix']);
66
        $this->setResolverCapitals($data['resolver_capitals']);
67
        $this->setResolverReplacements($data['resolver_replacements']);
68
    }
69
70
    /**
71
     * @param string $prefix The resolver prefix string.
72
     * @throws InvalidArgumentException If the prefix argument is not a string.
73
     * @return ResolverFactory Chainable
74
     */
75
    public function setResolverPrefix($prefix)
76
    {
77
        if (!is_string($prefix)) {
78
            throw new InvalidArgumentException(
79
                'Prefix must be a string'
80
            );
81
        }
82
        $this->resolverPrefix = $prefix;
83
        return $this;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function resolverPrefix()
90
    {
91
        return $this->resolverPrefix;
92
    }
93
94
    /**
95
     * @param string $suffix The resolver suffix string.
96
     * @throws InvalidArgumentException If the suffix argument is not a string.
97
     * @return ResolverFactory Chainable
98
     */
99
    public function setResolverSuffix($suffix)
100
    {
101
        if (!is_string($suffix)) {
102
            throw new InvalidArgumentException(
103
                'Prefix must be a string'
104
            );
105
        }
106
        $this->resolverSuffix = $suffix;
107
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function resolverSuffix()
114
    {
115
        return $this->resolverSuffix;
116
    }
117
118
    /**
119
     * @param array $capitals The array of letter to "calitalize-next" (uppercase next letter in the string).
120
     * @return ResolverFactory Chainable
121
     */
122
    public function setResolverCapitals(array $capitals)
123
    {
124
        $this->resolverCapitals = $capitals;
125
        return $this;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function resolverCapitals()
132
    {
133
        return $this->resolverCapitals;
134
    }
135
136
    /**
137
     * @param array $replacements The array (key=>value) of replacements.
138
     * @return ResolverFactory Chainable
139
     */
140
    public function setResolverReplacements(array $replacements)
141
    {
142
        $this->resolverReplacements = $replacements;
143
        return $this;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function resolverReplacements()
150
    {
151
        return $this->resolverReplacements;
152
    }
153
154
    /**
155
     * Resolve the class name from the requested type.
156
     *
157
     * @param string $type The "type" of object to resolve (the object ident).
158
     * @throws InvalidArgumentException If the type parameter is not a string.
159
     * @return string The resolved class name (FQN).
160
     */
161
    public function resolve($type)
162
    {
163
        if (!is_string($type)) {
164
            throw new InvalidArgumentException(
165
                'Can not resolve class ident: type must be a string'
166
            );
167
        }
168
169
        $capitalize_next = function(&$i) {
170
            $i = ucfirst($i);
171
        };
172
173
        $capitals = $this->resolverCapitals();
174 View Code Duplication
        foreach ($capitals as $cap) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
            $expl = explode($cap, $type);
176
            array_walk($expl, $capitalize_next);
177
            $type = implode($cap, $expl);
178
        }
179
180
        $replacements = $this->resolverReplacements();
181
        foreach ($replacements as $rep => $target) {
182
            $type = str_replace($rep, $target, $type);
183
        }
184
185
        $class = '\\'.trim($type, '\\');
186
187
        // Add prefix + suffix, if applicable
188
        $class = $this->resolverPrefix().$class.$this->resolverSuffix();
189
190
        return $class;
191
    }
192
193
    /**
194
     * @param string $type The "type" of object to resolve (the object ident).
195
     * @throws InvalidArgumentException If the type parameter is not a string.
196
     * @return boolean
197
     */
198
    public function isResolvable($type)
199
    {
200
        if (!is_string($type)) {
201
            throw new InvalidArgumentException(
202
                'Can not check resolvable: type must be a string'
203
            );
204
        }
205
206
        $class_name = $this->resolve($type);
207
        return class_exists($class_name);
208
    }
209
}
210