| Total Complexity | 40 |
| Total Lines | 237 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Data 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 Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Data implements IDataProvider |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Data array. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $data = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Returns the data array. |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public function getData() |
||
| 40 | { |
||
| 41 | return $this->data; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Clears a the entire data or only the given key. |
||
| 46 | * |
||
| 47 | * @param array|string $name clears only one value if you give a name, multiple values if |
||
| 48 | * you give an array of names, or the entire data if left null |
||
| 49 | */ |
||
| 50 | public function clear($name = null) |
||
| 51 | { |
||
| 52 | if ($name === null) { |
||
| 53 | $this->data = array(); |
||
| 54 | } elseif (is_array($name)) { |
||
| 55 | foreach ($name as $index) { |
||
| 56 | unset($this->data[$index]); |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | unset($this->data[$name]); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Overwrites the entire data with the given array. |
||
| 65 | * |
||
| 66 | * @param array $data the new data array to use |
||
| 67 | */ |
||
| 68 | public function setData(array $data) |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * merges the given array(s) with the current data with array_merge. |
||
| 75 | * |
||
| 76 | * @param array $data the array to merge |
||
| 77 | */ |
||
| 78 | public function mergeData(array $data) |
||
| 79 | { |
||
| 80 | $args = func_get_args(); |
||
| 81 | foreach ($args as $key => $v) { |
||
| 82 | if (is_array($v)) { |
||
| 83 | $this->data = array_merge($this->data, $v); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Assigns a value or an array of values to the data object. |
||
| 90 | * |
||
| 91 | * @param array|string $name an associative array of multiple (index=>value) or a string |
||
| 92 | * that is the index to use, i.e. a value assigned to "foo" will be |
||
| 93 | * accessible in the template through {$foo} |
||
| 94 | * @param mixed $val the value to assign, or null if $name was an array |
||
| 95 | */ |
||
| 96 | public function assign($name, $val = null) |
||
| 97 | { |
||
| 98 | if (is_array($name)) { |
||
| 99 | reset($name); |
||
| 100 | foreach ($name as $k => $v){ |
||
| 101 | $this->data[$k] = $v; |
||
| 102 | } |
||
| 103 | } else { |
||
| 104 | $this->data[$name] = $val; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Allows to assign variables using the object syntax. |
||
| 110 | * |
||
| 111 | * @param string $name the variable name |
||
| 112 | * @param string $value the value to assign to it |
||
| 113 | */ |
||
| 114 | public function __set($name, $value) |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Assigns a value by reference to the data object. |
||
| 121 | * |
||
| 122 | * @param string $name the index to use, i.e. a value assigned to "foo" will be |
||
| 123 | * accessible in the template through {$foo} |
||
| 124 | * @param mixed $val the value to assign by reference |
||
| 125 | */ |
||
| 126 | public function assignByRef($name, &$val) |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Appends values or an array of values to the data object. |
||
| 133 | * |
||
| 134 | * @param array|string $name an associative array of multiple (index=>value) or a string |
||
| 135 | * that is the index to use, i.e. a value assigned to "foo" will be |
||
| 136 | * accessible in the template through {$foo} |
||
| 137 | * @param mixed $val the value to assign, or null if $name was an array |
||
| 138 | * @param bool $merge true to merge data or false to append, defaults to false |
||
| 139 | */ |
||
| 140 | public function append($name, $val = null, $merge = false) |
||
| 141 | { |
||
| 142 | if (is_array($name)) { |
||
| 143 | foreach ($name as $key => $val) { |
||
| 144 | if (isset($this->data[$key]) && !is_array($this->data[$key])) { |
||
| 145 | settype($this->data[$key], 'array'); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($merge === true && is_array($val)) { |
||
| 149 | $this->data[$key] = $val + $this->data[$key]; |
||
| 150 | } else { |
||
| 151 | $this->data[$key][] = $val; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } elseif ($val !== null) { |
||
| 155 | if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
||
| 156 | settype($this->data[$name], 'array'); |
||
| 157 | } elseif (!isset($this->data[$name])) { |
||
| 158 | $this->data[$name] = array(); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($merge === true && is_array($val)) { |
||
| 162 | $this->data[$name] = $val + $this->data[$name]; |
||
| 163 | } else { |
||
| 164 | $this->data[$name][] = $val; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Appends a value by reference to the data object. |
||
| 171 | * |
||
| 172 | * @param string $name the index to use, i.e. a value assigned to "foo" will be |
||
| 173 | * accessible in the template through {$foo} |
||
| 174 | * @param mixed $val the value to append by reference |
||
| 175 | * @param bool $merge true to merge data or false to append, defaults to false |
||
| 176 | */ |
||
| 177 | public function appendByRef($name, &$val, $merge = false) |
||
| 178 | { |
||
| 179 | if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
||
| 180 | settype($this->data[$name], 'array'); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($merge === true && is_array($val)) { |
||
| 184 | foreach ($val as $key => &$value) { |
||
| 185 | $this->data[$name][$key] = &$value; |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | $this->data[$name][] = &$val; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns true if the variable has been assigned already, false otherwise. |
||
| 194 | * |
||
| 195 | * @param string $name the variable name |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isAssigned($name) |
||
| 200 | { |
||
| 201 | return isset($this->data[$name]); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Supports calls to isset($dwoo->var). |
||
| 206 | * |
||
| 207 | * @param string $name the variable name |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function __isset($name) |
||
| 212 | { |
||
| 213 | return isset($this->data[$name]); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Unassigns/removes a variable. |
||
| 218 | * |
||
| 219 | * @param string $name the variable name |
||
| 220 | */ |
||
| 221 | public function unassign($name) |
||
| 222 | { |
||
| 223 | unset($this->data[$name]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Supports unsetting variables using the object syntax. |
||
| 228 | * |
||
| 229 | * @param string $name the variable name |
||
| 230 | */ |
||
| 231 | public function __unset($name) |
||
| 232 | { |
||
| 233 | unset($this->data[$name]); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns a variable if it was assigned. |
||
| 238 | * |
||
| 239 | * @param string $name the variable name |
||
| 240 | * |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | public function get($name) |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Allows to read variables using the object syntax. |
||
| 250 | * |
||
| 251 | * @param string $name the variable name |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | * @throws Exception |
||
| 255 | */ |
||
| 256 | public function __get($name) |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 |