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 |
||
10 | abstract class OperatorResource extends AbstractResource implements OperatorInterface |
||
11 | { |
||
12 | use OperatorTrait; |
||
13 | |||
14 | const DEFAULT_MARKER_KEY = 'id'; |
||
15 | |||
16 | /** |
||
17 | * The key that indicates how the API nests resource collections. For example, when |
||
18 | * performing a GET, it could respond with ``{"servers": [{}, {}]}``. In this case, "servers" |
||
19 | * is the resources key, since the array of servers is nested inside. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $resourcesKey; |
||
24 | |||
25 | /** |
||
26 | * Indicates which attribute of the current resource should be used for pagination markers. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $markerKey; |
||
31 | |||
32 | /** |
||
33 | * Will create a new instance of this class with the current HTTP client and API injected in. This |
||
34 | * is useful when enumerating over a collection since multiple copies of the same resource class |
||
35 | * are needed. |
||
36 | * |
||
37 | * @return OperatorResource |
||
38 | */ |
||
39 | public function newInstance(): OperatorResource |
||
43 | |||
44 | /** |
||
45 | * @return \GuzzleHttp\Psr7\Uri:null |
||
46 | */ |
||
47 | protected function getHttpBaseUrl() |
||
51 | |||
52 | /** |
||
53 | * @param array $definition |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function executeWithState(array $definition) |
||
61 | |||
62 | private function getResourcesKey(): string |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | public function enumerate(array $def, array $userVals = [], callable $mapFn = null): \Generator |
||
104 | |||
105 | public function extractMultipleInstances(ResponseInterface $response, string $key = null): array |
||
118 | |||
119 | protected function getService() |
||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | View Code Duplication | public function model(string $class, $data = null): ResourceInterface |
|
148 | } |
||
149 |
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.