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 |
||
17 | class Container extends AbstractResource implements Creatable, Deletable, Retrievable, Listable, HasMetadata |
||
18 | { |
||
19 | use MetadataTrait; |
||
20 | |||
21 | const METADATA_PREFIX = 'X-Container-Meta-'; |
||
22 | |||
23 | /** @var int */ |
||
24 | public $objectCount; |
||
25 | |||
26 | /** @var int */ |
||
27 | public $bytesUsed; |
||
28 | |||
29 | /** @var string */ |
||
30 | public $name; |
||
31 | |||
32 | /** @var array */ |
||
33 | public $metadata; |
||
34 | |||
35 | protected $markerKey = 'name'; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 4 | public function populateFromResponse(ResponseInterface $response) |
|
48 | |||
49 | /** |
||
50 | * Retrieves a collection of object resources in the form of a generator. |
||
51 | * |
||
52 | * @param array $options {@see \OpenStack\ObjectStore\v1\Api::getContainer} |
||
53 | * @param callable|null $mapFn Allows a function to be mapped over each element. |
||
54 | * |
||
55 | * @return \Generator |
||
56 | */ |
||
57 | 1 | public function listObjects(array $options = [], callable $mapFn = null) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 1 | public function retrieve() |
|
71 | |||
72 | /** |
||
73 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer} |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | 2 | public function create(array $data) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 1 | public function delete() |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 1 | public function mergeMetadata(array $metadata) |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 1 | View Code Duplication | public function resetMetadata(array $metadata) |
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 2 | public function getMetadata() |
|
133 | |||
134 | /** |
||
135 | * Retrieves an Object and populates its `name` and `containerName` properties according to the name provided and |
||
136 | * the name of this container. A HTTP call will not be executed by default - you need to call |
||
137 | * {@see Object::retrieve} or {@see Object::download} on the returned Object object to do that. |
||
138 | * |
||
139 | * @param string $name The name of the object |
||
140 | * |
||
141 | * @return Object |
||
142 | */ |
||
143 | 4 | public function getObject($name) |
|
147 | |||
148 | /** |
||
149 | * Identifies whether an object exists in this container. |
||
150 | * |
||
151 | * @param string $name The name of the object. |
||
152 | * |
||
153 | * @return bool TRUE if the object exists, FALSE if it does not. |
||
154 | * |
||
155 | * @throws BadResponseError For any other HTTP error which does not have a 404 Not Found status. |
||
156 | * @throws \Exception For any other type of fatal error. |
||
157 | */ |
||
158 | 3 | public function objectExists($name) |
|
159 | { |
||
160 | try { |
||
161 | 3 | $this->getObject($name)->retrieve(); |
|
162 | 1 | return true; |
|
163 | 2 | } catch (BadResponseError $e) { |
|
164 | 2 | if ($e->getResponse()->getStatusCode() === 404) { |
|
165 | 1 | return false; |
|
166 | } |
||
167 | 1 | throw $e; |
|
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Creates a single object according to the values provided. |
||
173 | * |
||
174 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
||
175 | * |
||
176 | * @return Object |
||
177 | */ |
||
178 | 1 | public function createObject(array $data) |
|
182 | } |
||
183 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.