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 | trait ModifiesLabels |
||
| 9 | { |
||
| 10 | |||
| 11 | public function __construct() |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param array $labels |
||
| 18 | * |
||
| 19 | * @return Mail|string |
||
| 20 | * @throws \Exception |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function addLabels( array $labels ) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @param array $labels |
||
| 34 | * |
||
| 35 | * @return Mail|string |
||
| 36 | * @throws \Exception |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function removeLabels( array $labels ) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Adds a single label to the request |
||
| 50 | * |
||
| 51 | * @param $label |
||
| 52 | * |
||
| 53 | * @return Mail |
||
| 54 | */ |
||
| 55 | private function addSingleLabel( $label ) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Removes a single label from the request |
||
| 64 | * |
||
| 65 | * @param $label |
||
| 66 | * |
||
| 67 | * @return Mail |
||
| 68 | */ |
||
| 69 | private function removeSingleLabel( $label ) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Executes the modification |
||
| 78 | * |
||
| 79 | * @return Mail |
||
| 80 | */ |
||
| 81 | private function modify() |
||
| 85 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: