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); |
||
18 | class ObjectEntity extends OperatorResource implements Creatable, Deletable, HasMetadata |
||
19 | { |
||
20 | use MetadataTrait; |
||
21 | |||
22 | const METADATA_PREFIX = 'X-Object-Meta-'; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $containerName; |
||
26 | |||
27 | /** @var string */ |
||
28 | public $name; |
||
29 | |||
30 | /** @var string */ |
||
31 | public $hash; |
||
32 | |||
33 | /** @var string */ |
||
34 | public $contentType; |
||
35 | |||
36 | /** @var int */ |
||
37 | public $contentLength; |
||
38 | |||
39 | /** @var string */ |
||
40 | public $lastModified; |
||
41 | |||
42 | /** @var array */ |
||
43 | public $metadata; |
||
44 | |||
45 | protected $markerKey = 'name'; |
||
46 | protected $aliases = ['bytes' => 'contentLength']; |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function populateFromResponse(ResponseInterface $response): ResourceInterface |
||
59 | |||
60 | /** |
||
61 | * @param ResponseInterface $response |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | View Code Duplication | private function populateHeaders(ResponseInterface $response): self |
|
75 | |||
76 | /** |
||
77 | * Retrieves the public URI for this resource. |
||
78 | * |
||
79 | * @return \GuzzleHttp\Psr7\Uri |
||
80 | */ |
||
81 | public function getPublicUri(): Uri |
||
85 | |||
86 | /** |
||
87 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function create(array $data): Creatable |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function retrieve() |
||
105 | |||
106 | /** |
||
107 | * This call will perform a `GET` HTTP request for the given object and return back its content in the form of a |
||
108 | * Guzzle Stream object. Downloading an object will transfer all of the content for an object, and is therefore |
||
109 | * distinct from fetching its metadata (a `HEAD` request). The body of an object is not fetched by default to |
||
110 | * improve performance when handling large objects. |
||
111 | * |
||
112 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::getObject} |
||
113 | * |
||
114 | * @return StreamInterface |
||
115 | */ |
||
116 | |||
117 | public function download(array $data = []): StreamInterface |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function delete() |
||
135 | |||
136 | /** |
||
137 | * @param array $options {@see \OpenStack\ObjectStore\v1\Api::copyObject} |
||
138 | */ |
||
139 | public function copy(array $options) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | View Code Duplication | public function mergeMetadata(array $metadata) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | View Code Duplication | public function resetMetadata(array $metadata) |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function getMetadata(): array |
||
183 | } |
||
184 |
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.