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 |
||
| 8 | class MetadataManager |
||
| 9 | { |
||
| 10 | private $V3Edmx = null; |
||
| 11 | private $oldEdmx = null; |
||
| 12 | private $lastError = null; |
||
| 13 | private $serializer = null; |
||
| 14 | |||
| 15 | public function __construct($namespaceName = "Data", $containerName = "DefaultContainer") |
||
| 27 | |||
| 28 | public function getEdmx() |
||
| 32 | |||
| 33 | public function getEdmxXML() |
||
| 37 | |||
| 38 | public function addEntityType($name, $accessType = "Public") |
||
| 39 | { |
||
| 40 | $this->startEdmxTransaction(); |
||
| 41 | $NewEntity = new TEntityTypeType(); |
||
| 42 | $NewEntity->setName($name); |
||
| 43 | |||
| 44 | |||
| 45 | $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType(); |
||
| 46 | $entitySet->setName($this->pluralize(2, $NewEntity->getName())); |
||
| 47 | $namespace = $this->V3Edmx->getDataServices()[0]->getNamespace(); |
||
| 48 | if (0 == strlen(trim($namespace))) { |
||
| 49 | $entityTypeName = $NewEntity->getName(); |
||
| 50 | } else { |
||
| 51 | $entityTypeName = $namespace . "." . $NewEntity->getName(); |
||
| 52 | } |
||
| 53 | $entitySet->setEntityType($entityTypeName); |
||
| 54 | $entitySet->setGetterAccess($accessType); |
||
| 55 | |||
| 56 | $this->V3Edmx->getDataServices()[0]->addToEntityType($NewEntity); |
||
| 57 | $this->V3Edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet); |
||
| 58 | if (!$this->V3Edmx->isok($this->lastError)) { |
||
| 59 | $this->revertEdmxTransaction(); |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | $this->commitEdmxTransaction(); |
||
|
|
|||
| 63 | return $NewEntity; |
||
| 64 | } |
||
| 65 | |||
| 66 | private function startEdmxTransaction() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Pluralizes a word if quantity is not one. |
||
| 73 | * |
||
| 74 | * @param int $quantity Number of items |
||
| 75 | * @param string $singular Singular form of word |
||
| 76 | * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided |
||
| 77 | * @return string Pluralized word if quantity is not one, otherwise singular |
||
| 78 | */ |
||
| 79 | View Code Duplication | public static function pluralize($quantity, $singular, $plural = null) |
|
| 94 | |||
| 95 | private function revertEdmxTransaction() |
||
| 99 | |||
| 100 | private function commitEdmxTransaction() |
||
| 104 | |||
| 105 | public function addPropertyToEntityType($entityType, $name, $type, $defaultValue = null, $isKey = false, $storeGeneratedPattern = false, $summary = null, $longDescription = null) |
||
| 134 | |||
| 135 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
| 139 | |||
| 140 | public function getLastError() |
||
| 144 | } |
||
| 145 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: