Total Complexity | 43 |
Total Lines | 191 |
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[md5(serialize($data))] = $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 getFilterableColumns() |
||
120 | { |
||
121 | $data = $this->columns; |
||
122 | $newData = []; |
||
123 | foreach($data as $i=>$item) { |
||
124 | /** @var ColumnModel $item */ |
||
125 | if($item->getFilterable()) { |
||
126 | $newData[] = $item; |
||
127 | } |
||
128 | } |
||
129 | return $newData; |
||
130 | } |
||
131 | |||
132 | public function getDetailColumns() |
||
133 | { |
||
134 | $data = $this->columns; |
||
135 | $newData = []; |
||
136 | foreach($data as $i=>$item) { |
||
137 | /** @var ColumnModel $item */ |
||
138 | if($item->getShowDetail()) { |
||
139 | $newData[] = $item; |
||
140 | } |
||
141 | } |
||
142 | return $newData; |
||
143 | } |
||
144 | |||
145 | public function getAssignmentData() |
||
160 | } |
||
161 | |||
162 | public function removeColumn($label_or_name) |
||
163 | { |
||
164 | $data = $this->getColumns(); |
||
165 | foreach($data as $i=>$d) |
||
166 | { |
||
167 | /** @var ColumnModel $d */ |
||
168 | if($d->getLabel() == $label_or_name || $d->getName() == $label_or_name) { |
||
169 | unset($data[$i]); |
||
170 | } |
||
171 | } |
||
172 | $this->columns = $data; |
||
173 | } |
||
174 | |||
175 | public function getColumnNameOnly() |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param int $index |
||
187 | * @param ColumnModel $value |
||
188 | */ |
||
189 | public function setColumn($index, ColumnModel $value) |
||
190 | { |
||
191 | $this->columns[$index] = $value; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param int $index |
||
196 | * @return ColumnModel |
||
197 | */ |
||
198 | public function getColumn($index) { |
||
200 | } |
||
201 | |||
202 | public function setColumnArray($index, $key, $values) |
||
205 | } |
||
206 | } |