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 |
||
| 11 | View Code Duplication | class Registry |
|
| 12 | { |
||
| 13 | /** |
||
| 14 | * Actions registry. |
||
| 15 | * |
||
| 16 | * @var ActionInterface[] |
||
| 17 | */ |
||
| 18 | protected $actions = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Add an Action to the registry. |
||
| 22 | * |
||
| 23 | * @param $id |
||
| 24 | * @param ActionInterface $action |
||
| 25 | 1 | * |
|
| 26 | * @throws Exception |
||
| 27 | 1 | */ |
|
| 28 | 1 | public function add($id, ActionInterface $action) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Return an Action from the registry. |
||
| 38 | * |
||
| 39 | * @param string $id |
||
| 40 | 1 | * @return ActionInterface |
|
| 41 | * @throws Exception |
||
| 42 | 1 | */ |
|
| 43 | 1 | public function get($id) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Return true if an Action with the name $id has been registered. |
||
| 54 | * |
||
| 55 | 1 | * @param string $id |
|
| 56 | * @return bool |
||
| 57 | 1 | */ |
|
| 58 | public function has($id) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Return all the registered Actions. |
||
| 65 | 1 | * |
|
| 66 | * @return ActionInterface[] |
||
| 67 | 1 | */ |
|
| 68 | public function all() |
||
| 72 | } |
||
| 73 |