PHPClass   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 285
Duplicated Lines 4.56 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
wmc 46
lcom 3
cbo 1
dl 13
loc 285
ccs 76
cts 96
cp 0.7917
rs 8.3999
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromFQCN() 0 8 2
B isSimpleType() 0 15 6
A getPhpType() 0 10 3
A isNativeType() 13 13 2
A __construct() 0 5 1
A getName() 0 4 1
A setName() 0 5 1
A getNamespace() 0 4 1
A setNamespace() 0 5 1
A getDoc() 0 4 1
A setDoc() 0 5 1
A __toString() 0 4 1
A getFullName() 0 4 1
A getChecks() 0 4 2
A addCheck() 0 5 1
A getProperties() 0 4 1
A hasProperty() 0 4 1
B hasPropertyInHierarchy() 0 10 5
B getPropertyInHierarchy() 0 10 5
A getPropertiesInHierarchy() 0 10 3
A getProperty() 0 4 1
A addProperty() 0 5 1
A getExtends() 0 4 1
A setExtends() 0 5 1
A getAbstract() 0 4 1
A setAbstract() 0 5 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PHPClass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PHPClass, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace GoetasWebservices\Xsd\XsdToPhp\Php\Structure;
3
4
class PHPClass
5
{
6
7
    protected $name;
8
9
    protected $namespace;
10
11
    protected $doc;
12
13 16
    public static function createFromFQCN($className)
14
    {
15 16
        if (($pos = strrpos($className, '\\')) !== false) {
16
            return new self(substr($className, $pos + 1), substr($className, 0, $pos));
17
        } else {
18 16
            return new self($className);
19
        }
20
    }
21
22
    /**
23
     * @param bool $onlyParent
24
     * @return PHPProperty
25
     */
26 8
    public function isSimpleType($onlyParent = false)
27
    {
28 8
        if ($onlyParent) {
29
            $e = $this->getExtends();
30
            if ($e) {
31
                if ($e->hasProperty('__value')) {
32
                    return $e->getProperty('__value');
33
                }
34
            }
35
        } else {
36 8
            if ($this->hasPropertyInHierarchy('__value') && count($this->getPropertiesInHierarchy()) === 1) {
37 2
                return $this->getPropertyInHierarchy("__value");
38
            }
39
        }
40 7
    }
41
42 8
    public function getPhpType()
43
    {
44 8
        if (!$this->getNamespace()) {
45 8
            if ($this->isNativeType()) {
46 8
                return $this->getName();
47
            }
48
            return "\\" . $this->getName();
49
        }
50 1
        return "\\" . $this->getFullName();
51
    }
52
53 8 View Code Duplication
    public function isNativeType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55 8
        return !$this->getNamespace() && in_array($this->getName(), [
56 8
            'string',
57 8
            'int',
58 8
            'float',
59 8
            'integer',
60 8
            'boolean',
61 8
            'array',
62 8
            'mixed',
63
            'callable'
64 8
        ]);
65
    }
66
67
68 30
    public function __construct($name = null, $namespace = null)
69
    {
70 30
        $this->name = $name;
71 30
        $this->namespace = $namespace;
72 30
    }
73
74 24
    public function getName()
75
    {
76 24
        return $this->name;
77
    }
78
79 30
    public function setName($name)
80
    {
81 30
        $this->name = $name;
82 30
        return $this;
83
    }
84
85 24
    public function getNamespace()
86
    {
87 24
        return $this->namespace;
88
    }
89
90 30
    public function setNamespace($namespace)
91
    {
92 30
        $this->namespace = $namespace;
93 30
        return $this;
94
    }
95
96 8
    public function getDoc()
97
    {
98 8
        return $this->doc;
99
    }
100
101 30
    public function setDoc($doc)
102
    {
103 30
        $this->doc = $doc;
104 30
        return $this;
105
    }
106
107
    public function __toString()
108
    {
109
        return $this->getFullName();
110
    }
111
112 30
    public function getFullName()
113
    {
114 30
        return "{$this->namespace}\\{$this->name}";
115
    }
116
117
    protected $checks = array();
118
119
    /**
120
     *
121
     * @var PHPConstant[]
122
     */
123
    protected $constants = array();
124
125
    /**
126
     *
127
     * @var PHPProperty[]
128
     */
129
    protected $properties = array();
130
131
    /**
132
     *
133
     * @param
134
     *            $property
135
     * @return array
136
     */
137
    public function getChecks($property)
138
    {
139
        return isset($this->checks[$property]) ? $this->checks[$property] : array();
140
    }
141
142
    /**
143
     *
144
     * @param
145
     *            $property
146
     * @param
147
     *            $check
148
     * @param
149
     *            $value
150
     * @return $this
151
     */
152 1
    public function addCheck($property, $check, $value)
153
    {
154 1
        $this->checks[$property][$check][] = $value;
155 1
        return $this;
156
    }
157
158
    /**
159
     *
160
     * @return PHPProperty[]
161
     */
162 19
    public function getProperties()
163
    {
164 19
        return $this->properties;
165
    }
166
167
    /**
168
     *
169
     * @param string $name
170
     * @return boolean
171
     */
172 8
    public function hasProperty($name)
173
    {
174 8
        return isset($this->properties[$name]);
175
    }
176
177
    /**
178
     *
179
     * @param string $name
180
     * @return bool
181
     */
182 8
    public function hasPropertyInHierarchy($name)
183
    {
184 8
        if ($this->hasProperty($name)) {
185 2
            return true;
186
        }
187 7
        if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) {
188
            return true;
189
        }
190 7
        return false;
191
    }
192
193
    /**
194
     *
195
     * @param string $name
196
     * @return PHPProperty
197
     */
198 2
    public function getPropertyInHierarchy($name)
199
    {
200 2
        if ($this->hasProperty($name)) {
201 2
            return $this->getProperty($name);
202
        }
203
        if (($this instanceof PHPClass) && $this->getExtends() && $this->getExtends()->hasPropertyInHierarchy($name)) {
204
            return $this->getExtends()->getPropertyInHierarchy($name);
205
        }
206
        return null;
207
    }
208
209
    /**
210
     *
211
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
212
     * @return PHPProperty
213
     */
214 2
    public function getPropertiesInHierarchy()
215
    {
216 2
        $ps = $this->getProperties();
217
218 2
        if (($this instanceof PHPClass) && $this->getExtends()) {
219
            $ps = array_merge($ps, $this->getExtends()->getPropertiesInHierarchy());
220
        }
221
222 2
        return $ps;
223
    }
224
225
    /**
226
     *
227
     * @param string $name
228
     * @return PHPProperty
229
     */
230 8
    public function getProperty($name)
231
    {
232 8
        return $this->properties[$name];
233
    }
234
235
    /**
236
     *
237
     * @param PHPProperty $property
238
     * @return $this
239
     */
240 30
    public function addProperty(PHPProperty $property)
241
    {
242 30
        $this->properties[$property->getName()] = $property;
243 30
        return $this;
244
    }
245
246
    /**
247
     *
248
     * @var boolean
249
     */
250
    protected $abstract;
251
252
    /**
253
     *
254
     * @var PHPClass
255
     */
256
    protected $extends;
257
258
    /**
259
     *
260
     * @return PHPClass
261
     */
262 19
    public function getExtends()
263
    {
264 19
        return $this->extends;
265
    }
266
267
    /**
268
     *
269
     * @param PHPClass $extends
270
     * @return PHPClass
271
     */
272 17
    public function setExtends(PHPClass $extends)
273
    {
274 17
        $this->extends = $extends;
275 17
        return $this;
276
    }
277
278
    public function getAbstract()
279
    {
280
        return $this->abstract;
281
    }
282
283 25
    public function setAbstract($abstract)
284
    {
285 25
        $this->abstract = (boolean)$abstract;
286 25
        return $this;
287
    }
288
}
289