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 Library extends LazyResource |
|
|
|||
12 | { |
||
13 | /** @var string */ |
||
14 | public $code; |
||
15 | |||
16 | /** @var Locations */ |
||
17 | public $locations; |
||
18 | |||
19 | /** |
||
20 | * Library constructor. |
||
21 | * |
||
22 | * @param Client $client |
||
23 | * @param string $code |
||
24 | */ |
||
25 | public function __construct(Client $client, $code) |
||
31 | |||
32 | /** |
||
33 | * Check if we have the full representation of our data object. |
||
34 | * For libraries, it seems like we get the same data in the list |
||
35 | * response as in the single-item response, so we just check some |
||
36 | * random element. |
||
37 | * |
||
38 | * @param \stdClass $data |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | protected function isInitialized($data) |
||
46 | |||
47 | /** |
||
48 | * Generate the base URL for this resource. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | protected function urlBase() |
||
56 | } |
||
57 |
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.