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 classes like ConceptPropertyValue 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 ConceptPropertyValue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class ConceptPropertyValue extends VocabularyDataObject |
||
|
|||
7 | { |
||
8 | /** submembers */ |
||
9 | private $submembers; |
||
10 | /** property type */ |
||
11 | private $type; |
||
12 | /** content language */ |
||
13 | private $clang; |
||
14 | |||
15 | View Code Duplication | public function __construct($model, $vocab, $resource, $prop, $clang = '') |
|
22 | |||
23 | public function __toString() |
||
32 | |||
33 | public function getLang() |
||
37 | |||
38 | public function getLabel($lang = '') |
||
75 | |||
76 | public function getType() |
||
80 | |||
81 | public function getUri() |
||
85 | |||
86 | public function getVocab() |
||
90 | |||
91 | public function getVocabName() |
||
95 | |||
96 | public function addSubMember($member, $lang = '') |
||
102 | |||
103 | public function getSubMembers() |
||
111 | |||
112 | private function sortSubMembers() |
||
119 | |||
120 | public function isExternal() { |
||
124 | |||
125 | public function getNotation() |
||
132 | |||
133 | public function isReified() { |
||
136 | |||
137 | public function getReifiedPropertyValues() { |
||
153 | |||
154 | } |
||
155 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.