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 MetadataManager 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 MetadataManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class MetadataManager |
||
| 22 | { |
||
| 23 | private $V3Edmx = null; |
||
| 24 | private $oldEdmx = null; |
||
|
|
|||
| 25 | private $lastError = null; |
||
| 26 | private $serializer = null; |
||
| 27 | |||
| 28 | public function __construct($namespaceName = "Data", $containerName = "DefaultContainer") |
||
| 29 | { |
||
| 30 | $this->V3Edmx = new Edmx($namespaceName, $containerName); |
||
| 31 | if (!$this->V3Edmx->isOK($msg)) { |
||
| 32 | throw new \Exception($msg); |
||
| 33 | } |
||
| 34 | $this->initSerialiser(); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function getEdmx() |
||
| 38 | { |
||
| 39 | $msg = null; |
||
| 40 | assert($this->V3Edmx->isOk($msg), $msg); |
||
| 41 | return $this->V3Edmx; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getEdmxXML() |
||
| 48 | |||
| 49 | public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null) |
||
| 81 | |||
| 82 | private function startEdmxTransaction() |
||
| 83 | { |
||
| 84 | //$this->oldEdmx = serialize($this->V3Edmx); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Pluralizes a word if quantity is not one. |
||
| 89 | * |
||
| 90 | * @param int $quantity Number of items |
||
| 91 | * @param string $singular Singular form of word |
||
| 92 | * @param string $plural Plural form of word; function will attempt to deduce plural |
||
| 93 | * form from singular if not provided |
||
| 94 | * @return string Pluralized word if quantity is not one, otherwise singular |
||
| 95 | */ |
||
| 96 | public static function pluralize($quantity, $singular, $plural = null) |
||
| 97 | { |
||
| 98 | if ($quantity == 1 || !strlen($singular)) { |
||
| 99 | return $singular; |
||
| 100 | } |
||
| 101 | if ($plural !== null) { |
||
| 102 | return $plural; |
||
| 103 | } |
||
| 104 | |||
| 105 | $last_letter = strtolower($singular[strlen($singular) - 1]); |
||
| 106 | switch ($last_letter) { |
||
| 107 | case 'y': |
||
| 108 | return substr($singular, 0, -1) . 'ies'; |
||
| 109 | case 's': |
||
| 110 | return $singular . 'es'; |
||
| 111 | default: |
||
| 112 | return $singular . 's'; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | private function revertEdmxTransaction() |
||
| 117 | { |
||
| 118 | //$this->V3Edmx = unserialize($this->oldEdmx); |
||
| 119 | } |
||
| 120 | |||
| 121 | private function commitEdmxTransaction() |
||
| 122 | { |
||
| 123 | //$this->oldEdmx = null; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function addPropertyToEntityType( |
||
| 165 | |||
| 166 | public function addNavigationPropertyToEntityType( |
||
| 167 | TEntityTypeType $principalType, |
||
| 168 | $principalMultiplicity, |
||
| 169 | $principalProperty, |
||
| 170 | TEntityTypeType $dependentType, |
||
| 171 | $dependentMultiplicity, |
||
| 172 | $dependentProperty = "", |
||
| 173 | array $principalConstraintProperty = null, |
||
| 174 | array $dependentConstraintProperty = null, |
||
| 175 | $principalGetterAccess = "Public", |
||
| 176 | $principalSetterAccess = "Public", |
||
| 177 | $dependentGetterAccess = "Public", |
||
| 178 | $dependentSetterAccess = "Public", |
||
| 179 | $principalSummery = null, |
||
| 180 | $principalLongDescription = null, |
||
| 181 | $dependentSummery = null, |
||
| 182 | $dependentLongDescription = null |
||
| 183 | ) { |
||
| 184 | $this->startEdmxTransaction(); |
||
| 185 | $principalEntitySetName = Str::plural($principalType->getName(), 2); |
||
| 186 | $dependentEntitySetName = Str::plural($dependentType->getName(), 2); |
||
| 187 | $relationName = $principalType->getName() . "_" . $principalProperty . "_" |
||
| 188 | . $dependentType->getName() . "_" . $dependentProperty; |
||
| 189 | $relationName = trim($relationName, "_"); |
||
| 190 | |||
| 191 | $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
||
| 192 | if (0 == strlen(trim($namespace))) { |
||
| 193 | $relationFQName = $relationName; |
||
| 194 | } else { |
||
| 195 | $relationFQName = $namespace . "." . $relationName; |
||
| 196 | } |
||
| 197 | |||
| 198 | $principalNavigationProperty = new TNavigationPropertyType(); |
||
| 199 | $principalNavigationProperty->setName($principalProperty); |
||
| 200 | $principalNavigationProperty->setToRole(trim($dependentEntitySetName . "_" . $dependentProperty, "_")); |
||
| 201 | $principalNavigationProperty->setFromRole($principalEntitySetName . "_" . $principalProperty); |
||
| 202 | $principalNavigationProperty->setRelationship($relationFQName); |
||
| 203 | $principalNavigationProperty->setGetterAccess($principalGetterAccess); |
||
| 204 | $principalNavigationProperty->setSetterAccess($principalSetterAccess); |
||
| 205 | if (null != $principalSummery || null != $principalLongDescription) { |
||
| 206 | $principalDocumentation = new TDocumentationType(); |
||
| 207 | $principalDocumentation->setSummary($principalSummery); |
||
| 208 | $principalDocumentation->setLongDescription($principalLongDescription); |
||
| 209 | $principalNavigationProperty->setDocumentation($principalDocumentation); |
||
| 210 | } |
||
| 211 | $principalType->addToNavigationProperty($principalNavigationProperty); |
||
| 212 | $dependentNavigationProperty = null; |
||
| 213 | if (!empty($dependentProperty)) { |
||
| 214 | $dependentNavigationProperty = new TNavigationPropertyType(); |
||
| 215 | $dependentNavigationProperty->setName($dependentProperty); |
||
| 216 | $dependentNavigationProperty->setToRole($principalEntitySetName . "_" . $principalProperty); |
||
| 217 | $dependentNavigationProperty->setFromRole($dependentEntitySetName . "_" . $dependentProperty); |
||
| 218 | $dependentNavigationProperty->setRelationship($relationFQName); |
||
| 219 | $dependentNavigationProperty->setGetterAccess($dependentGetterAccess); |
||
| 220 | $dependentNavigationProperty->setSetterAccess($dependentSetterAccess); |
||
| 221 | if (null != $dependentSummery || null != $dependentLongDescription) { |
||
| 222 | $dependentDocumentation = new TDocumentationType(); |
||
| 223 | $dependentDocumentation->setSummary($dependentSummery); |
||
| 224 | $dependentDocumentation->setLongDescription($dependentLongDescription); |
||
| 225 | $dependentNavigationProperty->setDocumentation($dependentDocumentation); |
||
| 226 | } |
||
| 227 | $dependentType->addToNavigationProperty($dependentNavigationProperty); |
||
| 228 | } |
||
| 229 | |||
| 230 | $assocation = $this->createAssocationFromNavigationProperty( |
||
| 231 | $principalType, |
||
| 232 | $dependentType, |
||
| 233 | $principalNavigationProperty, |
||
| 234 | $dependentNavigationProperty, |
||
| 235 | $principalMultiplicity, |
||
| 236 | $dependentMultiplicity, |
||
| 237 | $principalConstraintProperty, |
||
| 238 | $dependentConstraintProperty |
||
| 239 | ); |
||
| 240 | |||
| 241 | $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToAssociation($assocation); |
||
| 242 | |||
| 243 | $associationSet = $this->createAssocationSetForAssocation( |
||
| 244 | $assocation, |
||
| 245 | $principalEntitySetName, |
||
| 246 | $dependentEntitySetName |
||
| 247 | ); |
||
| 248 | |||
| 249 | $this->V3Edmx->getDataServiceType()->getSchema()[0] |
||
| 250 | ->getEntityContainer()[0]->addToAssociationSet($associationSet); |
||
| 251 | |||
| 252 | if (!$this->V3Edmx->isok($this->lastError)) { |
||
| 253 | $this->revertEdmxTransaction(); |
||
| 254 | return false; |
||
| 255 | } |
||
| 256 | $this->commitEdmxTransaction(); |
||
| 257 | return [$principalNavigationProperty, $dependentNavigationProperty]; |
||
| 258 | } |
||
| 259 | |||
| 260 | protected function createAssocationFromNavigationProperty( |
||
| 261 | TEntityTypeType $principalType, |
||
| 262 | TEntityTypeType $dependentType, |
||
| 263 | TNavigationPropertyType $principalNavigationProperty, |
||
| 264 | TNavigationPropertyType $dependentNavigationProperty = null, |
||
| 265 | $principalMultiplicity, |
||
| 266 | $dependentMultiplicity, |
||
| 267 | array $principalConstraintProperty = null, |
||
| 268 | array $dependentConstraintProperty = null |
||
| 269 | ) { |
||
| 270 | if (null != $dependentNavigationProperty) { |
||
| 271 | if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) { |
||
| 272 | $msg = "if you have both a dependent property and a principal property," |
||
| 273 | ." they should both have the same relationship"; |
||
| 274 | throw new \Exception($msg); |
||
| 275 | } |
||
| 276 | if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() || |
||
| 277 | $dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole() |
||
| 278 | ) { |
||
| 279 | throw new \Exception("The from roles and two roles from matching properties should match"); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
||
| 283 | |||
| 284 | if (0 == strlen(trim($namespace))) { |
||
| 285 | $principalTypeFQName = $principalType->getName(); |
||
| 286 | $dependentTypeFQName = $dependentType->getName(); |
||
| 287 | } else { |
||
| 288 | $principalTypeFQName = $namespace . "." . $principalType->getName(); |
||
| 289 | $dependentTypeFQName = $namespace . "." . $dependentType->getName(); |
||
| 290 | } |
||
| 291 | $association = new TAssociationType(); |
||
| 292 | $relationship = $principalNavigationProperty->getRelationship(); |
||
| 293 | if (strpos($relationship, '.') !== false) { |
||
| 294 | $relationship = substr($relationship, strpos($relationship, '.') + 1); |
||
| 295 | } |
||
| 296 | |||
| 297 | $association->setName($relationship); |
||
| 298 | $principalEnd = new TAssociationEndType(); |
||
| 299 | $principalEnd->setType($principalTypeFQName); |
||
| 300 | $principalEnd->setRole($principalNavigationProperty->getFromRole()); |
||
| 301 | $principalEnd->setMultiplicity($principalMultiplicity); |
||
| 302 | $association->addToEnd($principalEnd); |
||
| 303 | $dependentEnd = new TAssociationEndType(); |
||
| 304 | $dependentEnd->setType($dependentTypeFQName); |
||
| 305 | $dependentEnd->setMultiplicity($dependentMultiplicity); |
||
| 306 | $association->addToEnd($dependentEnd); |
||
| 307 | |||
| 308 | if (null != $dependentNavigationProperty) { |
||
| 309 | $dependentEnd->setRole($dependentNavigationProperty->getFromRole()); |
||
| 310 | } else { |
||
| 311 | $dependentEnd->setRole($principalNavigationProperty->getToRole()); |
||
| 312 | } |
||
| 313 | |||
| 314 | $principalReferralConstraint = null; |
||
| 315 | $dependentReferralConstraint = null; |
||
| 316 | |||
| 317 | View Code Duplication | if (null != $principalConstraintProperty && 0 < count($principalConstraintProperty)) { |
|
| 318 | $principalReferralConstraint = new TReferentialConstraintRoleElementType(); |
||
| 319 | $principalReferralConstraint->setRole($principalNavigationProperty->getFromRole()); |
||
| 320 | foreach ($principalConstraintProperty as $propertyRef) { |
||
| 321 | $TpropertyRef = new TPropertyRefType(); |
||
| 322 | $TpropertyRef->setName($propertyRef); |
||
| 323 | $principalReferralConstraint->addToPropertyRef($TpropertyRef); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | View Code Duplication | if (null != $dependentConstraintProperty && 0 < count($dependentConstraintProperty)) { |
|
| 327 | $dependentReferralConstraint = new TReferentialConstraintRoleElementType(); |
||
| 328 | $dependentReferralConstraint->setRole($dependentNavigationProperty->getFromRole()); |
||
| 329 | foreach ($dependentConstraintProperty as $propertyRef) { |
||
| 330 | $TpropertyRef = new TPropertyRefType(); |
||
| 331 | $TpropertyRef->setName($propertyRef); |
||
| 332 | $dependentReferralConstraint->addToPropertyRef($TpropertyRef); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | if (null != $dependentReferralConstraint || null != $principalReferralConstraint) { |
||
| 337 | $constraint = new TConstraintType(); |
||
| 338 | $constraint->setPrincipal($principalReferralConstraint); |
||
| 339 | $constraint->setDependent($dependentReferralConstraint); |
||
| 340 | $association->setReferentialConstraint($constraint); |
||
| 341 | } |
||
| 342 | return $association; |
||
| 343 | } |
||
| 344 | |||
| 345 | protected function createAssocationSetForAssocation( |
||
| 370 | |||
| 371 | public function getLastError() |
||
| 375 | |||
| 376 | private function initSerialiser() |
||
| 377 | { |
||
| 378 | $ymlDir = __DIR__ . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata"; |
||
| 379 | $this->serializer = |
||
| 380 | SerializerBuilder::create() |
||
| 381 | ->addMetadataDir($ymlDir) |
||
| 382 | ->build(); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function __sleep() |
||
| 391 | |||
| 392 | public function __wakeup() |
||
| 396 | } |
||
| 397 |
This check marks private properties in classes that are never used. Those properties can be removed.