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 |
||
| 15 | trait ConsolesLoaderTrait |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * runConsolesAutoLoader |
||
| 20 | */ |
||
| 21 | public function runConsolesAutoLoader() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * loadConsolesFromContainers |
||
| 29 | */ |
||
| 30 | View Code Duplication | private function loadConsolesFromContainers() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $consoleClasses |
||
| 51 | */ |
||
| 52 | private function loadConsoles(array $consoleClasses = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Incomplete |
||
| 59 | */ |
||
| 60 | private function loadConsolesFromPort() |
||
| 67 | } |
||
| 68 |
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: