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); |
||
| 19 | class Container extends OperatorResource implements Creatable, Deletable, Retrievable, Listable, HasMetadata |
||
| 20 | { |
||
| 21 | use MetadataTrait; |
||
| 22 | |||
| 23 | const METADATA_PREFIX = 'X-Container-Meta-'; |
||
| 24 | |||
| 25 | /** @var int */ |
||
| 26 | public $objectCount; |
||
| 27 | |||
| 28 | /** @var int */ |
||
| 29 | public $bytesUsed; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | public $name; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | public $metadata; |
||
| 36 | |||
| 37 | protected $markerKey = 'name'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 5 | public function populateFromResponse(ResponseInterface $response): self |
|
| 43 | { |
||
| 44 | 5 | parent::populateFromResponse($response); |
|
| 45 | |||
| 46 | 5 | $this->objectCount = $response->getHeaderLine('X-Container-Object-Count'); |
|
|
|
|||
| 47 | 5 | $this->bytesUsed = $response->getHeaderLine('X-Container-Bytes-Used'); |
|
| 48 | 5 | $this->metadata = $this->parseMetadata($response); |
|
| 49 | 5 | ||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Retrieves a collection of object resources in the form of a generator. |
||
| 55 | * |
||
| 56 | * @param array $options {@see \OpenStack\ObjectStore\v1\Api::getContainer} |
||
| 57 | * @param callable|null $mapFn Allows a function to be mapped over each element. |
||
| 58 | * |
||
| 59 | 1 | * @return \Generator |
|
| 60 | */ |
||
| 61 | 1 | public function listObjects(array $options = [], callable $mapFn = null): \Generator |
|
| 62 | 1 | { |
|
| 63 | $options = array_merge($options, ['name' => $this->name, 'format' => 'json']); |
||
| 64 | return $this->model(Object::class)->enumerate($this->api->getContainer(), $options, $mapFn); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | 1 | * {@inheritdoc} |
|
| 69 | */ |
||
| 70 | 1 | public function retrieve() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer} |
||
| 78 | * |
||
| 79 | 3 | * @return $this |
|
| 80 | */ |
||
| 81 | 3 | public function create(array $data): Creatable |
|
| 90 | |||
| 91 | /** |
||
| 92 | 1 | * {@inheritdoc} |
|
| 93 | */ |
||
| 94 | 1 | public function delete() |
|
| 98 | |||
| 99 | /** |
||
| 100 | 1 | * {@inheritdoc} |
|
| 101 | */ |
||
| 102 | 1 | public function mergeMetadata(array $metadata) |
|
| 107 | |||
| 108 | /** |
||
| 109 | 1 | * {@inheritdoc} |
|
| 110 | */ |
||
| 111 | View Code Duplication | public function resetMetadata(array $metadata) |
|
| 128 | |||
| 129 | /** |
||
| 130 | 2 | * {@inheritdoc} |
|
| 131 | */ |
||
| 132 | 2 | public function getMetadata(): array |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieves an Object and populates its `name` and `containerName` properties according to the name provided and |
||
| 140 | * the name of this container. A HTTP call will not be executed by default - you need to call |
||
| 141 | * {@see Object::retrieve} or {@see Object::download} on the returned Object object to do that. |
||
| 142 | * |
||
| 143 | * @param string $name The name of the object |
||
| 144 | * |
||
| 145 | 4 | * @return Object |
|
| 146 | */ |
||
| 147 | 4 | public function getObject($name): Object |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Identifies whether an object exists in this container. |
||
| 154 | * |
||
| 155 | * @param string $name The name of the object. |
||
| 156 | * |
||
| 157 | * @return bool TRUE if the object exists, FALSE if it does not. |
||
| 158 | * |
||
| 159 | * @throws BadResponseError For any other HTTP error which does not have a 404 Not Found status. |
||
| 160 | 3 | * @throws \Exception For any other type of fatal error. |
|
| 161 | */ |
||
| 162 | View Code Duplication | public function objectExists(string $name): bool |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Creates a single object according to the values provided. |
||
| 177 | * |
||
| 178 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
||
| 179 | * |
||
| 180 | 2 | * @return Object |
|
| 181 | */ |
||
| 182 | 2 | public function createObject(array $data): Object |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Creates a Dynamic Large Object by chunking a file into smaller segments and uploading them into a holding |
||
| 189 | * container. When this completes, a manifest file is uploaded which references the prefix of the segments, |
||
| 190 | * allowing concatenation when a request is executed against the manifest. |
||
| 191 | * |
||
| 192 | * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
||
| 193 | * @param int $data['segmentSize'] The size in Bytes of each segment |
||
| 194 | * @param string $data['segmentContainer'] The container to which each segment will be uploaded |
||
| 195 | * @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default |
||
| 196 | * is used: name/timestamp/filesize |
||
| 197 | * |
||
| 198 | 1 | * @return Object |
|
| 199 | */ |
||
| 200 | public function createLargeObject(array $data): Object |
||
| 237 | } |
||
| 238 |
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.