Total Complexity | 40 |
Total Lines | 178 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ColumnSingleton 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 ColumnSingleton, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ColumnSingleton |
||
15 | { |
||
16 | |||
17 | private $columns; |
||
18 | private $joins; |
||
19 | |||
20 | public function newColumns() { |
||
21 | $this->columns = []; |
||
22 | } |
||
23 | |||
24 | public function addJoin($data) |
||
25 | { |
||
26 | $this->joins[] = $data; |
||
27 | } |
||
28 | |||
29 | public function getJoin() |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function getColumns() |
||
40 | } |
||
41 | |||
42 | public function valueAssignment($data_row = null) { |
||
43 | foreach ($this->getColumns() as $index=>$column) { |
||
44 | |||
45 | if (! $column->getName()) { |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | /** @var ColumnModel $column */ |
||
50 | if($data_row) { |
||
51 | $value = (isset($data_row->{$column->getField()}))?$data_row->{ $column->getField() }:null; |
||
52 | }else{ |
||
53 | $value = request($column->getName()); |
||
54 | |||
55 | if(!$value && $column->getDefaultValue()) { |
||
56 | $value = $column->getDefaultValue(); |
||
57 | } |
||
58 | |||
59 | $value = getTypeHook($column->getType())->assignment($value, $column); |
||
60 | } |
||
61 | |||
62 | $column->setValue($value); |
||
63 | $this->setColumn($index, $column); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function getIndexColumns() |
||
68 | { |
||
69 | $data = $this->columns; |
||
70 | $newData = []; |
||
71 | foreach($data as $i=>$item) { |
||
72 | /** @var ColumnModel $item */ |
||
73 | if($item->getShowIndex()) { |
||
74 | $newData[] = $item; |
||
75 | } |
||
76 | } |
||
77 | return $newData; |
||
78 | } |
||
79 | |||
80 | public function getAddEditColumns() |
||
81 | { |
||
82 | $data = $this->columns; |
||
83 | $newData = []; |
||
84 | foreach($data as $i=>$item) { |
||
85 | /** @var ColumnModel $item */ |
||
86 | if($item->getShowAdd() || $item->getShowEdit()) { |
||
87 | $newData[] = $item; |
||
88 | } |
||
89 | } |
||
90 | return $newData; |
||
91 | } |
||
92 | |||
93 | public function getEditColumns() |
||
104 | } |
||
105 | |||
106 | public function getAddColumns() |
||
117 | } |
||
118 | |||
119 | public function getDetailColumns() |
||
130 | } |
||
131 | |||
132 | public function getAssignmentData() |
||
147 | } |
||
148 | |||
149 | public function removeColumn($label_or_name) |
||
150 | { |
||
151 | $data = $this->getColumns(); |
||
152 | foreach($data as $i=>$d) |
||
153 | { |
||
154 | /** @var ColumnModel $d */ |
||
155 | if($d->getLabel() == $label_or_name || $d->getName() == $label_or_name) { |
||
156 | unset($data[$i]); |
||
157 | } |
||
158 | } |
||
159 | $this->columns = $data; |
||
160 | } |
||
161 | |||
162 | public function getColumnNameOnly() |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param int $index |
||
174 | * @param ColumnModel $value |
||
175 | */ |
||
176 | public function setColumn($index, ColumnModel $value) |
||
177 | { |
||
178 | $this->columns[$index] = $value; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param int $index |
||
183 | * @return ColumnModel |
||
184 | */ |
||
185 | public function getColumn($index) { |
||
187 | } |
||
188 | |||
189 | public function setColumnArray($index, $key, $values) |
||
192 | } |
||
193 | } |