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 |
||
17 | class TSchemaType extends IsOK |
||
18 | { |
||
19 | use GSchemaBodyElementsTrait, TSimpleIdentifierTrait, XSDTopLevelTrait, TNamespaceNameTrait { |
||
20 | TSimpleIdentifierTrait::isNCName insteadof TNamespaceNameTrait; |
||
21 | TSimpleIdentifierTrait::matchesRegexPattern insteadof TNamespaceNameTrait; |
||
22 | TSimpleIdentifierTrait::isName insteadof TNamespaceNameTrait; |
||
23 | } |
||
24 | /** |
||
25 | * @property string $namespace |
||
26 | */ |
||
27 | private $namespace = null; |
||
28 | |||
29 | /** |
||
30 | * @property string $namespaceUri |
||
31 | */ |
||
32 | private $namespaceUri = null; |
||
33 | |||
34 | /** |
||
35 | * @property string $alias |
||
36 | */ |
||
37 | private $alias = null; |
||
38 | |||
39 | /** |
||
40 | * Gets as namespaceUri |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function getNamespaceUri() |
||
48 | |||
49 | /** |
||
50 | * Sets a new namespaceUri |
||
51 | * |
||
52 | * @param string $namespaceUri |
||
53 | * @return self |
||
54 | */ |
||
55 | public function setNamespaceUri($namespaceUri) |
||
64 | |||
65 | /** |
||
66 | * Gets as alias |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getAlias() |
||
74 | |||
75 | /** |
||
76 | * Sets a new alias |
||
77 | * |
||
78 | * @param string $alias |
||
79 | * @return self |
||
80 | */ |
||
81 | public function setAlias($alias) |
||
90 | |||
91 | public function isOK(&$msg = null) |
||
115 | |||
116 | public function isStructureOK(&$msg = null) |
||
117 | { |
||
118 | $entityTypeNames = []; |
||
119 | $associationNames = []; |
||
120 | $navigationProperties = []; |
||
121 | $associationSets = []; |
||
122 | $namespaceLen = strlen($this->namespace); |
||
123 | if (0 != $namespaceLen) { |
||
124 | $namespaceLen++; |
||
125 | } |
||
126 | |||
127 | foreach ($this->getEntityType() as $entityType) { |
||
128 | $entityTypeNames[] = $entityType->getName(); |
||
129 | foreach ($entityType->getNavigationProperty() as $navigationProperty) { |
||
130 | $navigationProperties[ |
||
131 | substr($navigationProperty->getRelationship(), $namespaceLen) |
||
132 | ][] = $navigationProperty; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | foreach ($this->association as $association) { |
||
137 | $associationNames[$association->getName()] = $association->getEnd(); |
||
138 | } |
||
139 | foreach ($this->getEntityContainer()[0]->getAssociationSet() as $associationSet) { |
||
140 | $associationSets[substr($associationSet->getAssociation(), $namespaceLen)] = $associationSet->getEnd(); |
||
141 | } |
||
142 | |||
143 | |||
144 | $entitySets = $this->getEntityContainer()[0]->getEntitySet(); |
||
145 | foreach ($entitySets as $eset) { |
||
146 | $eSetType = $eset->getEntityType(); |
||
147 | /*if (substr($eSetType, 0, strlen($this->getNamespace())) != $this->getNamespace()) { |
||
148 | $msg = "Types for Entity Sets should have the namespace at the beginning " . __CLASS__; |
||
149 | die($this->getNamespace()); |
||
150 | return false; |
||
151 | }*/ |
||
152 | $eSetType = str_replace($this->getNamespace() . ".", "", $eSetType); |
||
153 | if (!in_array($eSetType, $entityTypeNames)) { |
||
154 | $msg = "entitySet Types should have a matching type name in entity Types"; |
||
155 | return false; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // Check Associations to associationSets |
||
160 | if (count($associationSets) != count($associationNames)) { |
||
161 | $msg = "we have " . count($associationSets) . "association sets and " . count($associationNames) |
||
162 | . " associations, they should be the same"; |
||
163 | } |
||
164 | if (count($associationNames) * 2 < count($navigationProperties)) { |
||
165 | $msg = "we have too many navigation properties. should have no more then double the" |
||
166 | ." number of associations."; |
||
167 | } |
||
168 | |||
169 | foreach ($associationNames as $associationName => $associationEnds) { |
||
170 | if (!array_key_exists($associationName, $associationSets)) { |
||
171 | $msg = "association " . $associationName . " exists without matching associationSet"; |
||
172 | return false; |
||
173 | } |
||
174 | |||
175 | if (!array_key_exists($associationName, $navigationProperties)) { |
||
176 | $msg = "association " . $associationName . " exists without matching Natvigation Property"; |
||
177 | return false; |
||
178 | } |
||
179 | $roles = [$associationEnds[0]->getRole(), $associationEnds[1]->getRole()]; |
||
180 | View Code Duplication | if (!in_array($associationSets[$associationName][0]->getRole(), $roles)) { |
|
181 | $msg = "association Set role " . $associationSets[$associationName][0]->getRole() |
||
182 | . "lacks a matching property in the attached association"; |
||
183 | return false; |
||
184 | } |
||
185 | View Code Duplication | if (!in_array($associationSets[$associationName][1]->getRole(), $roles)) { |
|
186 | $msg = "association Set role " . $associationSets[$associationName][1]->getRole() |
||
187 | . "lacks a matching property in the attached association"; |
||
188 | return false; |
||
189 | } |
||
190 | } |
||
191 | return true; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Gets as namespace |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getNamespace() |
||
203 | |||
204 | /** |
||
205 | * Sets a new namespace |
||
206 | * |
||
207 | * @param string $namespace |
||
208 | * @return self |
||
209 | */ |
||
210 | public function setNamespace($namespace) |
||
219 | |||
220 | protected function getRand() |
||
224 | } |
||
225 |
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.