| Total Complexity | 44 |
| Total Lines | 236 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IteratorDot often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IteratorDot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class IteratorDot extends ValidateDot |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The stored items |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $items = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * elements |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $elements; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create a new Dot instance |
||
| 40 | * |
||
| 41 | * @param mixed $items |
||
| 42 | */ |
||
| 43 | public function __construct($items = []) |
||
| 44 | { |
||
| 45 | $this->elements = $this->getArrayItems($items); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Set a given key / value pair or pairs |
||
| 50 | * if the key doesn't exist already |
||
| 51 | * |
||
| 52 | * @param array|int|string $keys |
||
| 53 | * @param mixed $value |
||
| 54 | */ |
||
| 55 | public function add($keys, $value = null) |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Delete the contents of a given key or keys |
||
| 68 | * |
||
| 69 | * @param array|int|string|null $keys |
||
| 70 | */ |
||
| 71 | public function clear($keys = null) |
||
| 72 | { |
||
| 73 | if (is_null($keys)) { |
||
| 74 | $this->elements = []; |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | $keys = (array)$keys; |
||
| 78 | foreach ($keys as $key) { |
||
| 79 | $this->set($key, []); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Delete the given key or keys |
||
| 85 | * |
||
| 86 | * @param array|int|string $keys |
||
| 87 | */ |
||
| 88 | public function delete($keys) |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Flatten an array with the given character as a key delimiter |
||
| 111 | * |
||
| 112 | * @param string $delimiter |
||
| 113 | * @param array|null $items |
||
| 114 | * @param string $prepend |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function flatten($delimiter = '.', $items = null, $prepend = '') |
||
| 118 | { |
||
| 119 | $flatten = []; |
||
| 120 | if (is_null($items)) { |
||
| 121 | $items = $this->elements; |
||
| 122 | } |
||
| 123 | foreach ($items as $key => $value) { |
||
| 124 | if (is_array($value) && !empty($value)) { |
||
| 125 | $flatten = array_merge( |
||
| 126 | $flatten, |
||
| 127 | $this->flatten($delimiter, $value, $prepend . $key . $delimiter) |
||
| 128 | ); |
||
| 129 | } else { |
||
| 130 | $flatten[$prepend . $key] = $value; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | return $flatten; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the value of a given key |
||
| 138 | * |
||
| 139 | * @param int|string|null $key |
||
| 140 | * @param mixed $default |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public function get($key = null, $default = null) |
||
| 144 | { |
||
| 145 | if (is_null($key)) { |
||
| 146 | return $this->elements; |
||
| 147 | } |
||
| 148 | if ($this->exists($this->elements, $key)) { |
||
| 149 | return $this->elements[$key]; |
||
| 150 | } |
||
| 151 | if (false === strpos($key, '.')) { |
||
| 152 | return $default; |
||
| 153 | } |
||
| 154 | $items = $this->elements; |
||
| 155 | foreach (explode('.', $key) as $segment) { |
||
| 156 | if (!is_array($items) || !$this->exists($items, $segment)) { |
||
| 157 | return $default; |
||
| 158 | } |
||
| 159 | $items = &$items[$segment]; |
||
| 160 | } |
||
| 161 | return $items; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return the given items as an array |
||
| 166 | * |
||
| 167 | * @param mixed $items |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function getArrayItems($items) |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return the value of a given key and |
||
| 182 | * delete the key |
||
| 183 | * |
||
| 184 | * @param int|string|null $key |
||
| 185 | * @param mixed $default |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function pull($key = null, $default = null) |
||
| 189 | { |
||
| 190 | if (is_null($key)) { |
||
| 191 | $value = $this->all(); |
||
| 192 | $this->clear(); |
||
| 193 | return $value; |
||
| 194 | } |
||
| 195 | $value = $this->get($key, $default); |
||
| 196 | $this->delete($key); |
||
| 197 | return $value; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Push a given value to the end of the array |
||
| 202 | * in a given key |
||
| 203 | * |
||
| 204 | * @param mixed $key |
||
| 205 | * @param mixed $value |
||
| 206 | */ |
||
| 207 | public function push($key, $value = null) |
||
| 208 | { |
||
| 209 | if (is_null($value)) { |
||
| 210 | $this->elements[] = $key; |
||
| 211 | return; |
||
| 212 | } |
||
| 213 | $items = $this->get($key); |
||
| 214 | if (is_array($items) || is_null($items)) { |
||
| 215 | $items[] = $value; |
||
| 216 | $this->set($key, $items); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set a given key / value pair or pairs |
||
| 222 | * |
||
| 223 | * @param array|int|string $keys |
||
| 224 | * @param mixed $value |
||
| 225 | */ |
||
| 226 | public function set($keys, $value = null) |
||
| 227 | { |
||
| 228 | if (is_array($keys)) { |
||
| 229 | foreach ($keys as $key => $value) { |
||
| 230 | $this->set($key, $value); |
||
| 231 | } |
||
| 232 | return; |
||
| 233 | } |
||
| 234 | $items = &$this->elements; |
||
| 235 | foreach (explode('.', $keys) as $key) { |
||
| 236 | if (!isset($items[$key]) || !is_array($items[$key])) { |
||
| 237 | $items[$key] = []; |
||
| 238 | } |
||
| 239 | $items = &$items[$key]; |
||
| 240 | } |
||
| 241 | $items = $value; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Return the value of a given key or all the values as JSON |
||
| 246 | * |
||
| 247 | * @param mixed $key |
||
| 248 | * @param int $options |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function toJson($key = null, $options = 0) |
||
| 258 | } |
||
| 259 | |||
| 260 | } |