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 |
||
| 13 | class AccessLogged extends AccessMagic { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Starting values of properties |
||
| 17 | * |
||
| 18 | * @var array $_startValues |
||
| 19 | */ |
||
| 20 | protected $_startValues = []; |
||
| 21 | /** |
||
| 22 | * Changed values |
||
| 23 | * |
||
| 24 | * @var array $_changes |
||
| 25 | */ |
||
| 26 | protected $_changes = []; |
||
| 27 | /** |
||
| 28 | * Increment/Decrement results AKA deltas |
||
| 29 | * |
||
| 30 | * @var array $_deltas |
||
| 31 | */ |
||
| 32 | protected $_deltas = []; |
||
| 33 | |||
| 34 | public function __set($name, $value) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Increments field by value |
||
| 50 | * |
||
| 51 | * @param string $name |
||
| 52 | * @param int|float $value Default: 1 |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function inc($name, $value = 1) { |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Decrements field by value |
||
| 64 | * |
||
| 65 | * @param string $name |
||
| 66 | * @param int|float $value Default: 1 |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function dec($name, $value = 1) { |
|
| 75 | |||
| 76 | public function getChanges() { |
||
| 79 | |||
| 80 | public function getDeltas() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Flushes changes |
||
| 86 | * |
||
| 87 | * Makes current values a start one and resets changes/deltas |
||
| 88 | */ |
||
| 89 | public function flush() { |
||
| 94 | |||
| 95 | public function clear() { |
||
| 99 | |||
| 100 | } |
||
| 101 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.