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 |
||
| 14 | abstract class AbstractContext implements ContextInterface |
||
| 15 | { |
||
| 16 | /** @var ContextInterface|null */ |
||
| 17 | private $parent; |
||
| 18 | |||
| 19 | /** @var ContextInterface[] */ |
||
| 20 | private $subContexts = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return ContextInterface|null |
||
| 24 | */ |
||
| 25 | 71 | public function getParent() |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @return ContextInterface |
||
| 32 | */ |
||
| 33 | 63 | public function getTopParent() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param ContextInterface|null $parent |
||
| 44 | * |
||
| 45 | * @return ContextInterface |
||
| 46 | */ |
||
| 47 | 130 | public function setParent(ContextInterface $parent = null) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $name |
||
| 56 | * @param null|string $class |
||
| 57 | * |
||
| 58 | * @return ContextInterface|null |
||
| 59 | */ |
||
| 60 | 134 | public function getSubContext($name, $class = null) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $class |
||
| 78 | * @param bool $autoCreate |
||
| 79 | * |
||
| 80 | * @return ContextInterface|null |
||
| 81 | */ |
||
| 82 | public function getSubContextByClass($class, $autoCreate) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $name |
||
| 89 | * @param object|ContextInterface $subContext |
||
| 90 | * |
||
| 91 | * @return AbstractContext |
||
| 92 | */ |
||
| 93 | 139 | public function addSubContext($name, $subContext) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $name |
||
| 118 | * |
||
| 119 | * @return ContextInterface |
||
| 120 | */ |
||
| 121 | 2 | public function removeSubContext($name) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $name |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 2 | public function containsSubContext($name) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @return ContextInterface |
||
| 145 | */ |
||
| 146 | 1 | public function clearSubContexts() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return \ArrayIterator |
||
| 158 | */ |
||
| 159 | 6 | public function getIterator() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $ownName |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 2 | public function debugPrintTree($ownName = 'root') |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 1 | public function __toString() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $path |
||
| 200 | * |
||
| 201 | * @return ContextInterface |
||
| 202 | */ |
||
| 203 | 3 | public function getPath($path) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $class |
||
| 226 | * |
||
| 227 | * @return ContextInterface |
||
| 228 | */ |
||
| 229 | 129 | protected function createSubContext($class) |
|
| 235 | } |
||
| 236 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: