| Total Complexity | 45 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 0 |
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.
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 |
||
| 28 | class Row extends SplArrayObject |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Row::$model |
||
| 32 | * |
||
| 33 | * @var \O2System\Spl\Info\SplClassInfo |
||
| 34 | */ |
||
| 35 | private $model; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Row::$record |
||
| 39 | * |
||
| 40 | * @var \O2System\Database\DataObjects\Result\Row\Record |
||
| 41 | */ |
||
| 42 | private $record; |
||
| 43 | |||
| 44 | // ------------------------------------------------------------------------ |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Row::__construct |
||
| 48 | * |
||
| 49 | * @param mixed $row |
||
| 50 | * @param Model $model |
||
| 51 | */ |
||
| 52 | public function __construct($row, Model &$model) |
||
| 53 | { |
||
| 54 | $this->model = new SplClassInfo($model); |
||
| 55 | $this->record = $row->getRecord(); |
||
| 56 | |||
| 57 | $hideColumns = []; |
||
| 58 | |||
| 59 | // Visible Columns |
||
| 60 | if (count($model->visibleColumns)) { |
||
| 61 | $hideColumns = array_diff($row->getColumns(), $model->visibleColumns); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Final rebuild row columns |
||
| 65 | $model->rebuildRow($row); |
||
| 66 | |||
| 67 | // Hide Columns |
||
| 68 | if (count($model->hideColumns)) { |
||
| 69 | $hideColumns = array_merge($model->hideColumns); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($row instanceof Database\DataObjects\Result\Row) { |
||
| 73 | parent::__construct($row->getArrayCopy()); |
||
| 74 | } elseif (is_array($row)) { |
||
| 75 | parent::__construct($row); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Append Columns |
||
| 79 | if (count($model->appendColumns)) { |
||
| 80 | foreach ($model->appendColumns as $appendColumn) { |
||
| 81 | $this->offsetSet($appendColumn, $this->offsetGet($appendColumn)); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | // Unset Columns |
||
| 86 | foreach ($hideColumns as $column) { |
||
| 87 | $this->offsetUnset($column); |
||
| 88 | } |
||
| 89 | |||
| 90 | models($this->model->getClass())->row = $this; |
||
|
|
|||
| 91 | } |
||
| 92 | |||
| 93 | // ------------------------------------------------------------------------ |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Row::offsetGet |
||
| 97 | * |
||
| 98 | * @param string $offset |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function offsetGet($offset) |
||
| 103 | { |
||
| 104 | if ($this->offsetExists($offset)) { |
||
| 105 | return parent::offsetGet($offset); |
||
| 106 | } elseif(strpos($offset, 'record') !== false) { |
||
| 107 | switch ($offset) { |
||
| 108 | case 'record': |
||
| 109 | return $this->record; |
||
| 110 | break; |
||
| 111 | case 'record_status': |
||
| 112 | return $this->record->status; |
||
| 113 | break; |
||
| 114 | case 'record_left': |
||
| 115 | return $this->record->left; |
||
| 116 | break; |
||
| 117 | case 'record_right': |
||
| 118 | return $this->record->right; |
||
| 119 | break; |
||
| 120 | case 'record_depth': |
||
| 121 | return $this->record->depth; |
||
| 122 | break; |
||
| 123 | case 'record_ordering': |
||
| 124 | return $this->record->ordering; |
||
| 125 | break; |
||
| 126 | case 'record_create_user': |
||
| 127 | return $this->record->create->user; |
||
| 128 | break; |
||
| 129 | case 'record_create_timestamp': |
||
| 130 | return $this->record->create->timestamp; |
||
| 131 | break; |
||
| 132 | case 'record_update_user': |
||
| 133 | return $this->record->update->user; |
||
| 134 | break; |
||
| 135 | case 'record_update_timestamp': |
||
| 136 | return $this->record->update->timestamp; |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | } elseif (null !== ($result = $this->__call($offset))) { |
||
| 140 | return $result; |
||
| 141 | } |
||
| 142 | |||
| 143 | return null; |
||
| 144 | } |
||
| 145 | |||
| 146 | // ------------------------------------------------------------------------ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Row::__call |
||
| 150 | * |
||
| 151 | * @param string $method |
||
| 152 | * @param array $args |
||
| 153 | * |
||
| 154 | * @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null |
||
| 155 | */ |
||
| 156 | public function __call($method, $args = []) |
||
| 203 | } |
||
| 204 | |||
| 205 | // ------------------------------------------------------------------------ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Row::delete |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 212 | * @throws \Psr\Cache\InvalidArgumentException |
||
| 213 | */ |
||
| 214 | public function delete() |
||
| 234 | } |
||
| 235 | } |