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 namespace Modules\Dashboard\Repositories\Cache; |
||
6 | class CacheWidgetDecorator extends BaseCacheDecorator implements WidgetRepository |
||
7 | { |
||
8 | public function __construct(WidgetRepository $widgets) |
||
14 | |||
15 | /** |
||
16 | * Find the saved state of widgets for the given user id |
||
17 | * @param int $userId |
||
18 | * @return string |
||
19 | */ |
||
20 | View Code Duplication | public function findForUser($userId) |
|
30 | |||
31 | /** |
||
32 | * Update or create the given widgets for given user |
||
33 | * @param array $widgets |
||
34 | * @return void |
||
35 | */ |
||
36 | View Code Duplication | public function updateOrCreateForUser($widgets, $userId) |
|
46 | } |
||
47 |
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.