Complex classes like Row 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Row, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Row extends BinderObject implements DumpToArrayInterface |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * \DOMNode represents a Row |
||
| 15 | * @var \DOMElement |
||
| 16 | */ |
||
| 17 | private $node = null; |
||
| 18 | private $row = null; |
||
| 19 | private $originalRow = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Row constructor |
||
| 23 | * |
||
| 24 | * @param array() |
||
| 25 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
| 26 | */ |
||
| 27 | 35 | public function __construct($instance = null) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Add a string field to row |
||
| 43 | * @param string $name |
||
| 44 | * @param string $value |
||
| 45 | */ |
||
| 46 | 31 | public function addField($name, $value) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $name - Field name |
||
| 60 | * @return string |
||
| 61 | * @desc et the string value from a field name |
||
| 62 | */ |
||
| 63 | 15 | public function get($name) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get array from a single field |
||
| 79 | * |
||
| 80 | * @param string $fieldName |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 3 | public function getAsArray($fieldName) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Return all Field Names from current Row |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | 1 | public function getFieldNames() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Set a string value to existing field name |
||
| 109 | * @param string $name |
||
| 110 | * @param string $value |
||
| 111 | */ |
||
| 112 | 9 | public function set($name, $value) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Remove specified field name from row. |
||
| 124 | * |
||
| 125 | * @param string $fieldName |
||
| 126 | */ |
||
| 127 | 2 | public function removeField($fieldName) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Remove specified field name with specified value name from row. |
||
| 137 | * |
||
| 138 | * @param string $fieldName |
||
| 139 | * @param $value |
||
| 140 | */ |
||
| 141 | 1 | public function removeValue($fieldName, $value) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Update a specific field and specific value with new value |
||
| 163 | * |
||
| 164 | * @param String $fieldName |
||
| 165 | * @param String $oldvalue |
||
| 166 | * @param String $newvalue |
||
| 167 | */ |
||
| 168 | 1 | public function replaceValue($fieldName, $oldvalue, $newvalue) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the \DOMElement row objet |
||
| 188 | * |
||
| 189 | * @return \DOMElement |
||
| 190 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
| 191 | */ |
||
| 192 | 3 | public function getAsDom() |
|
| 211 | |||
| 212 | 18 | public function toArray() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getAsJSON() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 2 | public function getAsRaw() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 1 | public function hasChanges() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * |
||
| 251 | */ |
||
| 252 | 35 | public function acceptChanges() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * |
||
| 259 | */ |
||
| 260 | 1 | public function rejectChanges() |
|
| 264 | |||
| 265 | 31 | protected function informChanges() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Override Specific implementation of setPropValue to Row |
||
| 272 | * |
||
| 273 | * @param Row $obj |
||
| 274 | * @param string $propName |
||
| 275 | * @param string $value |
||
| 276 | */ |
||
| 277 | 4 | protected function setPropValue($obj, $propName, $value) |
|
| 281 | } |
||
| 282 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..