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") |
||
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 addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
109 | |||
110 | private function getLastError() |
||
114 | } |
||
115 |
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: