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 |
||
| 19 | class DotArray implements \ArrayAccess |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Storage array |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $array = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array $array |
||
| 30 | */ |
||
| 31 | 53 | public function __construct($array = []) |
|
| 32 | { |
||
| 33 | 53 | foreach ($array as $key => $value) { |
|
| 34 | 26 | $this->offsetSet($key, $value); |
|
| 35 | 53 | } |
|
| 36 | 53 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Validate key |
||
| 40 | * |
||
| 41 | * @param string $key |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 39 | public static function validateKey($key) |
|
| 45 | { |
||
| 46 | 39 | return (bool) preg_match('/^(\w|\.)+$/', $key); |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Explode key by separator character (a dot character) |
||
| 51 | * |
||
| 52 | * @param string $key |
||
| 53 | * @return array |
||
| 54 | * @throws \RuntimeException |
||
| 55 | */ |
||
| 56 | 39 | protected function explodeKey($key) |
|
| 57 | { |
||
| 58 | 39 | if (!self::validateKey($key)) { |
|
| 59 | 1 | throw new \RuntimeException("Key `$key` is invalid"); |
|
| 60 | } |
||
| 61 | |||
| 62 | 38 | return explode('.', $key); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Check has key |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | 25 | public function hasKey($key) |
|
| 72 | { |
||
| 73 | 25 | $parts = $this->explodeKey($key); |
|
| 74 | 25 | $count = count($parts) - 1; |
|
| 75 | 25 | $cKey = array_pop($parts); |
|
| 76 | |||
| 77 | 25 | if (0 == $count) { |
|
| 78 | 24 | $array = $this->array; |
|
| 79 | 24 | } else { |
|
| 80 | 1 | $pKey = implode('.', $parts); |
|
| 81 | 1 | $array = $this->offsetGet($pKey); |
|
| 82 | } |
||
| 83 | |||
| 84 | 25 | return is_array($array) && array_key_exists($cKey, $array); |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get all value as array |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 28 | public function toArray() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Check exist key . Like isset(), a value of null is considered not set. |
||
| 99 | * isset($array['abc.xyz']) same isset($array['abc']['xyz']) |
||
| 100 | * |
||
| 101 | * @param string $key |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | 17 | public function offsetExists($key) |
|
| 105 | { |
||
| 106 | 17 | return null !== $this->offsetGet($key); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get an array value |
||
| 111 | * $array['abc.xyz'] same $array['abc']['xyz'] |
||
| 112 | * |
||
| 113 | * @param string $key |
||
| 114 | * @return mixed NULL will be returned if the key is not found. |
||
| 115 | */ |
||
| 116 | 25 | public function offsetGet($key) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Set an array value |
||
| 133 | * $array['abc.xyz'] = 'value' same $array['abc']['xyz'] = 'value' |
||
| 134 | * |
||
| 135 | * @param string $key |
||
| 136 | * @param mixed $value |
||
| 137 | */ |
||
| 138 | 37 | public function offsetSet($key, $value) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Unset an array value |
||
| 160 | * using unset($array['abc.xyz']) to unset($array['abc']['xyz']) |
||
| 161 | * |
||
| 162 | * @param string $key |
||
| 163 | */ |
||
| 164 | 2 | public function offsetUnset($key) |
|
| 177 | } |
||
| 178 |