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 |
||
20 | final class UserService |
||
21 | { |
||
22 | /** |
||
23 | * @var ConnectionInterface |
||
24 | */ |
||
25 | private $connection; |
||
26 | |||
27 | public function __construct(ConnectionInterface $connection) |
||
31 | |||
32 | /** |
||
33 | * Get the user data for an id. |
||
34 | * |
||
35 | * @throws ApiException |
||
36 | * @throws NotFoundException |
||
37 | */ |
||
38 | public function getUser(string $userId): User |
||
44 | |||
45 | /** |
||
46 | * Get a list of attended venues for a user id. |
||
47 | * |
||
48 | * @throws ApiException |
||
49 | * @throws NotFoundException |
||
50 | * |
||
51 | * @return Setlist[] |
||
52 | */ |
||
53 | View Code Duplication | public function getAttends(string $userId, int $page = 1): array |
|
67 | |||
68 | /** |
||
69 | * Get a list of edited venues for a user id. |
||
70 | * |
||
71 | * @throws ApiException |
||
72 | * @throws NotFoundException |
||
73 | * |
||
74 | * @return Setlist[] |
||
75 | */ |
||
76 | View Code Duplication | public function getEdits(string $userId, int $page = 1): array |
|
90 | } |
||
91 |
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.