Total Complexity | 54 |
Total Lines | 221 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Module |
||
6 | { |
||
7 | protected $module; |
||
8 | |||
9 | protected $data; |
||
10 | |||
11 | protected $name; |
||
12 | |||
13 | public function __construct($moduleName, $moduleData) |
||
14 | { |
||
15 | $this->module = (object) $moduleData; |
||
16 | $this->data = array_filter($moduleData, function($elem){ |
||
17 | return (is_array($elem)) ? false : true; |
||
18 | }); |
||
19 | $this->multipleData = []; |
||
|
|||
20 | $this->multipleData[] = array_filter($moduleData, function($elem){ |
||
21 | return (is_array($elem)) ? true : false; |
||
22 | }); |
||
23 | |||
24 | $this->name = $moduleName; |
||
25 | } |
||
26 | |||
27 | public function getName() |
||
28 | { |
||
29 | return $this->name; |
||
30 | } |
||
31 | |||
32 | public function getMultipleColumns() |
||
33 | { |
||
34 | return $this->multipleData; |
||
35 | } |
||
36 | |||
37 | public function getData() |
||
38 | { |
||
39 | $this->data['sort'] = 'integer'; |
||
40 | $this->data['status'] = 'boolean'; |
||
41 | |||
42 | return $this->data; |
||
43 | } |
||
44 | |||
45 | public function getBackendColumnTitles() |
||
46 | { |
||
47 | $data = ['S.N.']; |
||
48 | foreach ($this->data as $column => $optionString) { |
||
49 | $optionArray = explode('|', $optionString); |
||
50 | if (in_array($optionArray[0], ['string', 'int'])&&in_array($column, ['title', 'firstname', 'lastname', 'name'])) { |
||
51 | $data[] = ucwords($column); |
||
52 | } |
||
53 | } |
||
54 | return array_merge($data, ['Last Updated', 'Status', 'Actions']); |
||
55 | } |
||
56 | |||
57 | public function getNativeColumns() |
||
58 | { |
||
59 | $data = []; |
||
60 | foreach ($this->data as $column => $optionString) { |
||
61 | if (is_array($optionString)) continue; |
||
62 | $optionArray = explode('|', $optionString); |
||
63 | if (in_array($optionArray[0], DataOption::$types)) { |
||
64 | $data[] = $column; |
||
65 | } |
||
66 | } |
||
67 | if($this->getForeignColumns()){ |
||
68 | foreach($this->getForeignColumns() as $relation => $tablename ){ |
||
69 | $columnName = array_values($tablename)[0]; |
||
70 | $data[] = str_singular($columnName).'_id'; |
||
71 | } |
||
72 | } |
||
73 | return $data; |
||
74 | } |
||
75 | |||
76 | public function getNativeData() |
||
77 | { |
||
78 | $data = []; |
||
79 | foreach ($this->data as $column => $optionString) { |
||
80 | $optionArray = explode('|', $optionString); |
||
81 | if (in_array($optionArray[0], DataOption::$types)) { |
||
82 | $data[] = [$column => $optionArray[0]]; |
||
83 | } |
||
84 | } |
||
85 | return $data; |
||
86 | } |
||
87 | |||
88 | public function getWritableColumns() |
||
89 | { |
||
90 | $data = []; |
||
91 | foreach ($this->data as $column => $optionString) { |
||
92 | $optionArray = explode('|', $optionString); |
||
93 | if (in_array($optionArray[0], DataOption::$types)) { |
||
94 | $data[] = [$column => $optionArray[0]]; |
||
95 | } |
||
96 | } |
||
97 | return $data; |
||
98 | } |
||
99 | |||
100 | public function getFileColumns($type = 'all') |
||
101 | { |
||
102 | if (is_array($type)) |
||
103 | $types = $type; |
||
104 | else |
||
105 | $types = ($type == "all") ? DataOption::$fileTypes : [$type]; |
||
106 | |||
107 | $data = []; |
||
108 | foreach ($this->data as $column => $optionString) { |
||
109 | $dataOption = new DataOption($column, $optionString); |
||
110 | if (in_array($dataOption->getType(), $types)) { |
||
111 | $data[] = $column; |
||
112 | } |
||
113 | } |
||
114 | return $data; |
||
115 | } |
||
116 | |||
117 | public function getParentColumns() |
||
127 | } |
||
128 | |||
129 | public function getGalleries() |
||
130 | { |
||
131 | $data = []; |
||
132 | foreach ($this->data as $column => $optionString) { |
||
133 | $dataOption = new DataOption($column, $optionString); |
||
134 | if ($dataOption->getType() == 'gallery') { |
||
135 | $data[] = $column; |
||
136 | } |
||
137 | } |
||
138 | return $data; |
||
139 | } |
||
140 | |||
141 | public function getForeignColumns($type = 'all') |
||
142 | { |
||
143 | if (is_array($type)) |
||
144 | $types = $type; |
||
145 | else |
||
146 | $types = ($type == "all") ? DataOption::$specialTypes : [$type]; |
||
147 | |||
148 | $data = []; |
||
149 | foreach ($this->data as $column => $optionString) { |
||
150 | $dataOption = new DataOption($column, $optionString); |
||
151 | if (in_array($dataOption->getType(), $types)) { |
||
152 | $data[] = [$column => $dataOption->getParentModule()]; |
||
153 | } |
||
154 | } |
||
155 | return $data; |
||
156 | } |
||
157 | |||
158 | public function getForeignData($type = 'all') |
||
159 | { |
||
160 | if (is_array($type)) |
||
161 | $types = $type; |
||
162 | else |
||
163 | $types = ($type == "all") ? DataOption::$specialTypes : [$type]; |
||
164 | |||
165 | $data = []; |
||
166 | foreach ($this->data as $column => $optionString) { |
||
167 | $dataOption = new DataOption($column, $optionString); |
||
168 | if (in_array($dataOption->getType(), $types)) { |
||
169 | $data[] = [ |
||
170 | 'columnName' => $column, |
||
171 | 'parentModule' => $dataOption->getParentModule(), |
||
172 | 'parentModel' => $dataOption->getParentModel() |
||
173 | ]; |
||
174 | } |
||
175 | } |
||
176 | return $data; |
||
177 | } |
||
178 | |||
179 | public function getPivotName($related) |
||
180 | { |
||
181 | $modelArray = [$this->getModelName(), ucfirst(camel_case(str_singular($related)))]; |
||
182 | sort($modelArray); |
||
183 | return implode("", $modelArray); |
||
184 | } |
||
185 | |||
186 | public function getPivotTableName($related) |
||
187 | { |
||
188 | $moduleArray = [str_singular($this->getModelNameLowercase()), str_singular($related)]; |
||
189 | sort($moduleArray); |
||
190 | return implode("_", $moduleArray); |
||
191 | } |
||
192 | |||
193 | public function getModuleName() |
||
194 | { |
||
195 | return $this->name; |
||
196 | } |
||
197 | |||
198 | public function getModuleDisplayName() |
||
199 | { |
||
200 | return ucfirst(str_replace('_', '', $this->name)); |
||
201 | } |
||
202 | |||
203 | public function getDisplayColumn() |
||
204 | { |
||
205 | foreach ($this->data as $column => $optionString) { |
||
206 | $optionArray = explode('|', $optionString); |
||
207 | if (in_array($optionArray[0], ['string', 'int'])&&in_array($column, ['title', 'firstname', 'lastname', 'name'])) { |
||
208 | return $column; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | public function getModelName() |
||
214 | { |
||
215 | return ucfirst(camel_case(str_singular($this->name))); |
||
216 | } |
||
217 | |||
218 | public function getModelNamePlural() |
||
221 | } |
||
222 | |||
223 | public function getModelNameLowercase() |
||
226 | } |
||
227 | } |
||
228 |