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 |
||
| 32 | trait DelegatorWritableTrait |
||
| 33 | { |
||
| 34 | use WritableTrait, DelegatorTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Override `isWritable()` in the WritableTrait. |
||
| 38 | * |
||
| 39 | * Delegator's writability is base on its registries |
||
| 40 | * |
||
| 41 | * {@inheritDoc} |
||
| 42 | */ |
||
| 43 | public function isWritable()/*# : bool */ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Override `setWritable()` in the WritableTrait |
||
| 57 | * |
||
| 58 | * {@inheritDoc} |
||
| 59 | */ |
||
| 60 | public function setWritable($writable)/*# : bool */ |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set writable to FALSE in all registries |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | * @access protected |
||
| 78 | */ |
||
| 79 | View Code Duplication | protected function setRegistryWritableFalse()/*# : bool */ |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Set writable to TRUE at first matching registry |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | * @access protected |
||
| 95 | */ |
||
| 96 | View Code Duplication | protected function setRegistryWritableTrue()/*# : bool */ |
|
| 106 | } |
||
| 107 |
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.