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); |
||
| 20 | class Image extends OperatorResource implements Creatable, Listable, Retrievable, Deletable |
||
| 21 | { |
||
| 22 | /** @var string */ |
||
| 23 | public $status; |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | public $name; |
||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | public $tags; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | public $containerFormat; |
||
| 33 | |||
| 34 | /** @var \DateTimeImmutable */ |
||
| 35 | public $createdAt; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | public $diskFormat; |
||
| 39 | |||
| 40 | /** @var \DateTimeImmutable */ |
||
| 41 | public $updatedAt; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | public $visibility; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | public $minDisk; |
||
| 48 | |||
| 49 | /** @var bool */ |
||
| 50 | public $protected; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | public $id; |
||
| 54 | |||
| 55 | /** @var \GuzzleHttp\Psr7\Uri */ |
||
| 56 | public $fileUri; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | public $checksum; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | public $ownerId; |
||
| 63 | |||
| 64 | /** @var int */ |
||
| 65 | public $size; |
||
| 66 | |||
| 67 | /** @var int */ |
||
| 68 | public $minRam; |
||
| 69 | |||
| 70 | /** @var \GuzzleHttp\Psr7\Uri */ |
||
| 71 | public $schemaUri; |
||
| 72 | |||
| 73 | /** @var int */ |
||
| 74 | public $virtualSize; |
||
| 75 | |||
| 76 | private $jsonSchema; |
||
| 77 | |||
| 78 | protected $aliases = [ |
||
| 79 | 'container_format' => 'containerFormat', |
||
| 80 | 'disk_format' => 'diskFormat', |
||
| 81 | 'min_disk' => 'minDisk', |
||
| 82 | 'owner' => 'ownerId', |
||
| 83 | 'min_ram' => 'minRam', |
||
| 84 | 'virtual_size' => 'virtualSize', |
||
| 85 | ]; |
||
| 86 | |||
| 87 | 7 | /** |
|
| 88 | * @inheritdoc |
||
| 89 | 6 | */ |
|
| 90 | View Code Duplication | protected function getAliases(): array |
|
| 91 | 6 | { |
|
| 92 | return parent::getAliases() + [ |
||
| 93 | 6 | 'created_at' => new Alias('createdAt', \DateTimeImmutable::class), |
|
| 94 | 5 | 'updated_at' => new Alias('updatedAt', \DateTimeImmutable::class), |
|
| 95 | 5 | 'fileUri' => new Alias('fileUri', \GuzzleHttp\Psr7\Uri::class), |
|
| 96 | 'schemaUri' => new Alias('schemaUri', \GuzzleHttp\Psr7\Uri::class) |
||
| 97 | 6 | ]; |
|
| 98 | 5 | } |
|
| 99 | 5 | ||
| 100 | 7 | public function populateFromArray(array $data): self |
|
| 116 | 2 | ||
| 117 | 2 | public function create(array $data): Creatable |
|
| 122 | |||
| 123 | public function retrieve() |
||
| 128 | |||
| 129 | 2 | private function getSchema(): Schema |
|
| 138 | 2 | ||
| 139 | 1 | public function update(array $data) |
|
| 177 | 1 | ||
| 178 | 1 | public function delete() |
|
| 182 | |||
| 183 | 1 | public function deactivate() |
|
| 187 | 1 | ||
| 188 | public function reactivate() |
||
| 192 | 1 | ||
| 193 | public function uploadData(StreamInterface $stream) |
||
| 201 | |||
| 202 | public function downloadData(): StreamInterface |
||
| 207 | |||
| 208 | public function addMember($memberId): Member |
||
| 212 | |||
| 213 | public function listMembers(): \Generator |
||
| 217 | |||
| 218 | public function getMember($memberId): Member |
||
| 222 | } |
||
| 223 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: