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); |
||
| 10 | |||
| 11 | use Assert\Assert; |
||
| 12 | use Ds\Map; |
||
| 13 | use RuntimeException; |
||
| 14 | |||
| 15 | trait TypedMapTrait |
||
| 16 | { |
||
| 17 | private Map $compositeMap; |
||
|
|
|||
| 18 | |||
| 19 | /** @var string[] */ |
||
| 20 | private array $validTypes = []; |
||
| 21 | |||
| 22 | 2 | /** @param string[] $validTypes */ |
|
| 23 | private function init(iterable $objects, array $validTypes): void |
||
| 24 | 2 | { |
|
| 25 | if (isset($this->compositeVector)) { |
||
| 26 | throw new RuntimeException('Cannot reinitialize map'); |
||
| 27 | } |
||
| 28 | |||
| 29 | Assert::thatAll($validTypes, 'Invalid map types')->string()->notEmpty(); |
||
| 30 | 6 | $this->validTypes = $validTypes; |
|
| 31 | $this->compositeMap = new Map; |
||
| 32 | 6 | ||
| 33 | foreach ($objects as $key => $object) { |
||
| 34 | $this->assertValidKey($key); |
||
| 35 | $this->assertValidType($object); |
||
| 36 | $this->compositeMap->put($key, clone $object); |
||
| 37 | } |
||
| 38 | 2 | } |
|
| 39 | |||
| 40 | 2 | public function keys(): array |
|
| 41 | 1 | { |
|
| 42 | 1 | $this->assertInitialized(); |
|
| 43 | 1 | return $this->compositeMap->keys()->toArray(); |
|
| 44 | } |
||
| 45 | |||
| 46 | 3 | public function has(string $key): bool |
|
| 47 | { |
||
| 48 | 3 | $this->assertInitialized(); |
|
| 49 | $this->assertValidKey($key); |
||
| 50 | return $this->compositeMap->hasKey($key); |
||
| 51 | 1 | } |
|
| 52 | |||
| 53 | 1 | public function get(string $key, $default = null): ?object |
|
| 54 | { |
||
| 55 | $this->assertInitialized(); |
||
| 56 | 1 | $this->assertValidKey($key); |
|
| 57 | if (func_num_args() === 1) { |
||
| 58 | 1 | Assert::that($this->has($key))->true("Key '$key' not found and no default provided"); |
|
| 59 | return clone (object)$this->compositeMap->get($key); |
||
| 60 | } else { |
||
| 61 | 1 | if (!is_null($default)) { |
|
| 62 | $this->assertValidType($default); |
||
| 63 | 1 | } |
|
| 64 | $object = $this->compositeMap->get($key, $default); |
||
| 65 | return is_null($object) ? null : clone $object; |
||
| 66 | 1 | } |
|
| 67 | } |
||
| 68 | 1 | ||
| 69 | public function with(string $key, object $object): self |
||
| 70 | { |
||
| 71 | $this->assertInitialized(); |
||
| 72 | $this->assertValidKey($key); |
||
| 73 | $this->assertValidType($object); |
||
| 74 | 3 | $copy = clone $this; |
|
| 75 | $copy->compositeMap->put($key, clone $object); |
||
| 76 | 3 | return $copy; |
|
| 77 | } |
||
| 78 | |||
| 79 | 18 | public function without(string $key): self |
|
| 80 | { |
||
| 81 | 18 | $this->assertInitialized(); |
|
| 82 | 18 | Assert::that($this->has($key))->true("Key '$key' not found"); |
|
| 83 | 16 | $copy = clone $this; |
|
| 84 | 14 | $copy->compositeMap->remove($key); |
|
| 85 | return $copy; |
||
| 86 | 16 | } |
|
| 87 | 16 | ||
| 88 | /** |
||
| 89 | 16 | * Note that this does not do a strict equality check because all objects are immutable so it's |
|
| 90 | * unlikely that you will request a reference to an internal object. If you require more specific |
||
| 91 | 16 | * matching use search(), filter(), unwrap object, or iterate. |
|
| 92 | 2 | */ |
|
| 93 | 2 | public function find(object $object) |
|
| 94 | 2 | { |
|
| 95 | 2 | $this->assertInitialized(); |
|
| 96 | $this->assertValidType($object); |
||
| 97 | return array_search($object, $this->compositeMap->toArray(), false); |
||
| 98 | 14 | } |
|
| 99 | |||
| 100 | 14 | public function first(): object |
|
| 101 | { |
||
| 102 | 14 | $this->assertInitialized(); |
|
| 103 | 14 | /** @psalm-suppress MissingPropertyType */ |
|
| 104 | 14 | return clone $this->compositeMap->first()->value; |
|
| 105 | 14 | } |
|
| 106 | 14 | ||
| 107 | public function last(): object |
||
| 108 | { |
||
| 109 | 14 | $this->assertInitialized(); |
|
| 110 | 1 | /** @psalm-suppress MissingPropertyType */ |
|
| 111 | 1 | return clone $this->compositeMap->last()->value; |
|
| 112 | 1 | } |
|
| 113 | 1 | ||
| 114 | 1 | public function isEmpty(): bool |
|
| 115 | { |
||
| 116 | $this->assertInitialized(); |
||
| 117 | 14 | return $this->compositeMap->isEmpty(); |
|
| 118 | } |
||
| 119 | 1 | ||
| 120 | /** @param static $map */ |
||
| 121 | 1 | public function merge($map): self |
|
| 122 | 1 | { |
|
| 123 | $this->assertInitialized(); |
||
| 124 | $this->assertValidMap($map); |
||
| 273 |