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 declare(strict_types=1); |
||
15 | class TogglClient |
||
16 | { |
||
17 | const VERSION = 'v8'; |
||
18 | |||
19 | /** |
||
20 | * @var Client; |
||
21 | */ |
||
22 | private $client; |
||
23 | |||
24 | /** |
||
25 | * @var SerializerInterface |
||
26 | */ |
||
27 | private $serializer; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $api_key; |
||
33 | |||
34 | /** |
||
35 | * TogglClient constructor. |
||
36 | * @param Client $client |
||
37 | * @param SerializerInterface $serializer |
||
38 | * @param $api_key |
||
39 | */ |
||
40 | 2 | public function __construct(GuzzleClient $client, SerializerInterface $serializer, $api_key) |
|
46 | |||
47 | /** |
||
48 | * @return array|Workspace[] |
||
49 | */ |
||
50 | 1 | public function getWorkspaces() |
|
58 | |||
59 | /** |
||
60 | * @return array|Client[] |
||
61 | */ |
||
62 | View Code Duplication | public function getClientsForWorkspace($workspaceId) |
|
70 | |||
71 | /** |
||
72 | * @return array|Project[] |
||
73 | */ |
||
74 | View Code Duplication | public function getProjectsForWorkspace($workspaceId) |
|
82 | } |
||
83 |
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.