AbstractStructureDefinition   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 260
Duplicated Lines 13.46 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 3
cbo 2
dl 35
loc 260
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocBlock() 0 4 1
A getFunctionDefinitions() 0 4 1
A getInvariants() 26 26 5
A getName() 0 4 1
A getNamespace() 0 4 1
A getPath() 0 4 1
A getUsedStructures() 0 4 1
A getQualifiedName() 9 9 2
A getType() 0 4 1
A hasParents() 0 4 1
A setDocBlock() 0 4 1
A setFunctionDefinitions() 0 4 1
A setName() 0 4 1
A setNamespace() 0 4 1
A setPath() 0 4 1
A setUsedStructures() 0 4 1

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
/**
4
 * \AppserverIo\Doppelgaenger\Entities\Definitions\AbstractStructureDefinition
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Entities\Definitions;
22
23
use AppserverIo\Doppelgaenger\Entities\Lists\FunctionDefinitionList;
24
use AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface;
25
26
/**
27
 * This class acts as a DTO-like (we are not immutable due to protected visibility)
28
 * entity for describing class definitions
29
 *
30
 * @author    Bernhard Wick <[email protected]>
31
 * @copyright 2015 TechDivision GmbH - <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/appserver-io/doppelgaenger
34
 * @link      http://www.appserver.io/
35
 *
36
 * @property string                 $path                File path to the class definition
37
 * @property string                 $namespace           The namespace the class belongs to
38
 * @property array                  $usedStructures      All classes which are referenced by the "use" keyword
39
 * @property string                 $docBlock            The initial class docblock header
40
 * @property string                 $name                Name of the class
41
 * @property array|string           $extends             Parent structures this structure extends from
42
 * @property FunctionDefinitionList $functionDefinitions List of methods
43
 */
44
abstract class AbstractStructureDefinition extends AbstractDefinition implements StructureDefinitionInterface
45
{
46
    /**
47
     * File path to the class definition
48
     *
49
     * @var string $path
50
     */
51
    protected $path;
52
53
    /**
54
     * The namespace the class belongs to
55
     *
56
     * @var string $namespace
57
     */
58
    protected $namespace;
59
60
    /**
61
     * All classes which are referenced by the "use" keyword
62
     *
63
     * @var array $usedStructures
64
     */
65
    protected $usedStructures;
66
67
    /**
68
     * The initial class docblock header
69
     *
70
     * @var string $docBlock
71
     */
72
    protected $docBlock;
73
74
    /**
75
     * Name of the class
76
     *
77
     * @var string $name
78
     */
79
    protected $name;
80
81
    /**
82
     * List of methods this class defines
83
     *
84
     * @var \AppserverIo\Doppelgaenger\Entities\Lists\FunctionDefinitionList $functionDefinitions
85
     */
86
    protected $functionDefinitions;
87
88
    /**
89
     * List of directly defined invariant conditions
90
     *
91
     * @var \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $invariantConditions
92
     */
93
    protected $invariantConditions;
94
95
    /**
96
     * List of lists of any ancestral invariants
97
     *
98
     * @var \AppserverIo\Doppelgaenger\Entities\Lists\TypedListList $ancestralInvariants
99
     */
100
    protected $ancestralInvariants;
101
102
    /**
103
     * Getter method for attribute $docBlock
104
     *
105
     * @return string
106
     */
107
    public function getDocBlock()
108
    {
109
        return $this->docBlock;
110
    }
111
112
    /**
113
     * Getter method for attribute $functionDefinitions
114
     *
115
     * @return null|\AppserverIo\Doppelgaenger\Entities\Lists\FunctionDefinitionList
116
     */
117
    public function getFunctionDefinitions()
118
    {
119
        return $this->functionDefinitions;
120
    }
121
122
    /**
123
     * Will return all invariants. direct and introduced (by ancestral structures) alike.
124
     *
125
     * @param boolean $nonPrivateOnly Make this true if you only want conditions which do not have a private context
126
     *
127
     * @return \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList
128
     */
129 View Code Duplication
    public function getInvariants($nonPrivateOnly = false)
130
    {
131
        // We have to clone it here, otherwise we might have weird side effects, of having the "add()" operation
132
        // persistent on $this->ancestralInvariants
133
        $invariants = clone $this->ancestralInvariants;
134
        $invariants->add($this->invariantConditions);
135
136
        // If we need to we will filter all the non private conditions from the lists
137
        if ($nonPrivateOnly === true) {
138
            // we might want to filter invariants based on their visibility
139
            $invariantListIterator = $invariants->getIterator();
140
            foreach ($invariantListIterator as $invariantList) {
141
                // iterate all invariant lists
142
                $invariantIterator = $invariantList->getIterator();
143
                foreach ($invariantIterator as $key => $invariant) {
144
                    // iterate the actual invariants
145
                    if ($invariant->isPrivateContext()) {
146
                        $invariantList->delete($key);
147
                    }
148
                }
149
            }
150
        }
151
152
        // Return what is left
153
        return $invariants;
154
    }
155
156
    /**
157
     * Getter method for attribute $name
158
     *
159
     * @return string
160
     */
161
    public function getName()
162
    {
163
        return $this->name;
164
    }
165
166
    /**
167
     * Getter method for attribute $namespace
168
     *
169
     * @return string
170
     */
171
    public function getNamespace()
172
    {
173
        return $this->namespace;
174
    }
175
176
    /**
177
     * Getter method for attribute $path
178
     *
179
     * @return string
180
     */
181
    public function getPath()
182
    {
183
        return $this->path;
184
    }
185
186
    /**
187
     * Getter method for attribute $usedStructures
188
     *
189
     * @return array
190
     */
191
    public function getUsedStructures()
192
    {
193
        return $this->usedStructures;
194
    }
195
196
    /**
197
     * Will return the qualified name of a structure
198
     *
199
     * @return string
200
     */
201 View Code Duplication
    public function getQualifiedName()
202
    {
203
        if (empty($this->namespace)) {
204
            return $this->name;
205
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
206
        } else {
207
            return ltrim($this->namespace, '\\') . '\\' . $this->name;
208
        }
209
    }
210
211
    /**
212
     * Will return the type of the definition.
213
     *
214
     * @return string
215
     */
216
    public function getType()
217
    {
218
        return static::TYPE;
219
    }
220
221
    /**
222
     * Does this structure have parent structures.
223
     * We are talking parents here, not implemented interfaces or used traits
224
     *
225
     * @return boolean
226
     */
227
    public function hasParents()
228
    {
229
        return !empty($this->extends);
230
    }
231
232
    /**
233
     * Setter method for attribute $docBlock
234
     *
235
     * @param string $docBlock Doc block of the structure
236
     *
237
     * @return null
238
     */
239
    public function setDocBlock($docBlock)
240
    {
241
        $this->docBlock = $docBlock;
242
    }
243
244
    /**
245
     * Setter method for attribute $functionDefinitions
246
     *
247
     * @param \AppserverIo\Doppelgaenger\Entities\Lists\FunctionDefinitionList $functionDefinitions List of functions
248
     *
249
     * @return null
250
     */
251
    public function setFunctionDefinitions(FunctionDefinitionList $functionDefinitions)
252
    {
253
        $this->functionDefinitions = $functionDefinitions;
254
    }
255
256
    /**
257
     * Setter method for attribute $name
258
     *
259
     * @param string $name Name of the structure
260
     *
261
     * @return null
262
     */
263
    public function setName($name)
264
    {
265
        $this->name = $name;
266
    }
267
268
    /**
269
     * Getter method for attribute $namespace
270
     *
271
     * @param string $namespace The namespace of the structure
272
     *
273
     * @return null
274
     */
275
    public function setNamespace($namespace)
276
    {
277
        $this->namespace = $namespace;
278
    }
279
280
    /**
281
     * Setter method for attribute $path
282
     *
283
     * @param string $path Path the definition's file
284
     *
285
     * @return null
286
     */
287
    public function setPath($path)
288
    {
289
        $this->path = $path;
290
    }
291
292
    /**
293
     * Getter method for attribute $usedStructures
294
     *
295
     * @param array $usedStructures Array of structures referenced using the "use" statement
296
     *
297
     * @return null
298
     */
299
    public function setUsedStructures($usedStructures)
300
    {
301
        $this->usedStructures = $usedStructures;
302
    }
303
}
304