Total Complexity | 40 |
Total Lines | 287 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 1 | Features | 1 |
Complex classes like StubParser 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 StubParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class StubParser |
||
23 | { |
||
24 | |||
25 | public $texts = []; |
||
26 | private $inputName; |
||
27 | private $parsedModel; |
||
28 | |||
29 | private $fields; |
||
30 | private $inputs; |
||
31 | private $validationRules; |
||
32 | private $hasAuth; |
||
33 | private $store; |
||
34 | |||
35 | public function __construct($inputName, $parsedModel) |
||
36 | { |
||
37 | $this->inputName = $inputName; |
||
38 | $this->parsedModel = $parsedModel; |
||
39 | } |
||
40 | |||
41 | public function setValidationRules($rules) |
||
42 | { |
||
43 | $this->validationRules = $rules; |
||
44 | } |
||
45 | |||
46 | public function setStore($store) |
||
47 | { |
||
48 | $this->store = $store; |
||
49 | } |
||
50 | |||
51 | public function setAuthType(bool $hasAuth){ |
||
52 | $this->hasAuth = $hasAuth; |
||
53 | } |
||
54 | |||
55 | public function setFields(array $fields){ |
||
56 | $this->fields = $fields; |
||
57 | } |
||
58 | |||
59 | public function setInputs(array $inputs){ |
||
60 | $this->inputs = $inputs; |
||
61 | } |
||
62 | |||
63 | public function replaceModel($stub) |
||
64 | { |
||
65 | $modelNamespace = $this->parsedModel; |
||
66 | $modelName = $this->getModelName($modelNamespace); |
||
67 | |||
68 | $array = [ |
||
69 | '{{ modelName }}' => ucfirst($modelName), |
||
70 | '{{ modelNamespace }}' => $modelNamespace, |
||
71 | '{{ uploadFile }}' => $this->uploadCodeParser(), |
||
72 | '{{ model }}' => strtolower($modelName), |
||
73 | '{{ properties }}' => $this->parseProperties(), |
||
74 | '{{ rules }}' => $this->parseValidationRules(), |
||
75 | '{{ fields }}' => $this->parseActionInComponent(), |
||
76 | '{{ setProperties }}' => $this->parseSetPropertiesValue(), |
||
77 | ]; |
||
78 | |||
79 | return str_replace(array_keys($array), array_values($array), $stub); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Make Locale files |
||
84 | */ |
||
85 | public function setLocaleTexts() |
||
86 | { |
||
87 | $this->texts[ucfirst($this->inputName)] = ucfirst($this->inputName); |
||
88 | $this->texts[ucfirst(Str::plural($this->inputName))] = ucfirst(Str::plural($this->inputName)); |
||
89 | $files = File::glob(resource_path('lang').'/*_panel.json'); |
||
90 | |||
91 | foreach ($files as $file) { |
||
92 | $decodedFile = json_decode(File::get($file), 1); |
||
93 | $texts = $this->texts; |
||
94 | foreach ($texts as $key => $text) { |
||
95 | if (array_key_exists($key, $decodedFile)){ |
||
96 | unset($texts[$text]); |
||
97 | } |
||
98 | } |
||
99 | $array = array_merge($decodedFile, $texts); |
||
100 | File::put($file, json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get model name from namespace |
||
106 | */ |
||
107 | public function getModelName($modelNamespace) |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Parse properties in Livewire component |
||
116 | */ |
||
117 | public function parseProperties() |
||
118 | { |
||
119 | $fields = array_keys($this->inputs); |
||
120 | $str = ''; |
||
121 | |||
122 | if(in_array($this->inputName, $fields)){ |
||
123 | $this->error("Model name must not equal to column names, fix it and rerun command with -f flag"); |
||
|
|||
124 | die; |
||
125 | } |
||
126 | |||
127 | foreach ($fields as $field) { |
||
128 | $str .= 'public $'.$field.";".$this->makeTab(1); |
||
129 | } |
||
130 | |||
131 | return $str; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Parse Uploading Code |
||
136 | */ |
||
137 | public function uploadCodeParser() |
||
138 | { |
||
139 | $filesInput = array_keys($this->inputs, 'file'); |
||
140 | $str = ''; |
||
141 | foreach ($filesInput as $file) { |
||
142 | // We get store path which has been defined in crud's config file |
||
143 | $storePath = $this->store[$file] ?? "{$file}"; |
||
144 | |||
145 | // We get property value then store file in $storePath |
||
146 | // A PHP Code for upload as a string will be created here |
||
147 | $str .= $this->makeTab(2).'if($this->getPropertyValue(\''.$file.'\') and is_object($this->'.$file.')) {'.$this->makeTab(3); |
||
148 | $str .= '$this->'.$file.' = $this->getPropertyValue(\''.$file.'\')->store(\''.$storePath.'\');'.$this->makeTab(2); |
||
149 | $str .= '}'.PHP_EOL; |
||
150 | } |
||
151 | |||
152 | return $str; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * parse values for mount method in Livewire |
||
157 | */ |
||
158 | public function parseSetPropertiesValue() |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Parse Validation rules |
||
172 | */ |
||
173 | public function parseValidationRules() |
||
174 | { |
||
175 | $str = ''; |
||
176 | |||
177 | foreach ($this->validationRules as $key => $rule) { |
||
178 | $str .= "'$key' => '$rule',".$this->makeTab(2, $key != array_key_last($this->validationRules)); |
||
179 | } |
||
180 | |||
181 | return $str; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Create an array of properties in Livewire component for actions |
||
186 | */ |
||
187 | public function parseActionInComponent() |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Create Blade from stub |
||
205 | */ |
||
206 | public function parseBlade($stub){ |
||
207 | $modelName = $this->getModelName($this->parsedModel); |
||
208 | |||
209 | $array = [ |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Parse <td> tags for data |
||
225 | */ |
||
226 | public function parseDataInBlade() |
||
227 | { |
||
228 | $fields = $this->fields; |
||
229 | $str = ''; |
||
230 | $modelName = strtolower($this->getModelName($this->parsedModel)); |
||
231 | foreach ($fields as $key => $field) { |
||
232 | $normalizedField = $this->normalizeField($field); |
||
233 | $str .= $normalizedField->setModel($modelName)->setKey($key)->renderData(); |
||
234 | |||
235 | if (array_key_first($fields) != $key){ |
||
236 | $str .= $this->makeTab(1, array_key_last($fields) != $key); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | return $str; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Parse <td> tags for head |
||
245 | */ |
||
246 | public function parseTitlesInBlade() |
||
247 | { |
||
248 | $fields = $this->fields; |
||
249 | $str = ''; |
||
250 | |||
251 | $modelName = strtolower($this->getModelName($this->parsedModel)); |
||
252 | |||
253 | foreach ($fields as $key => $field) { |
||
254 | $normalizedField = $this->normalizeField($field); |
||
255 | $str .= $normalizedField->setModel($modelName)->setKey($key)->renderTitle(); |
||
256 | |||
257 | if (array_key_first($fields) != $key){ |
||
258 | $str .= $this->makeTab(6, array_key_last($fields) != $key); |
||
259 | } |
||
260 | |||
261 | $this->texts[$field->getTitle()] = $field->getTitle(); |
||
262 | } |
||
263 | |||
264 | return $str; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Create inputs HTML |
||
269 | */ |
||
270 | public function parseInputsInBlade() |
||
271 | { |
||
272 | $str = ''; |
||
273 | foreach ($this->inputs as $name => $type) { |
||
274 | $title = ucfirst($name); |
||
275 | $this->texts[$title] = ucfirst($name); |
||
276 | $class = $this->getInputClassNamespace($type); |
||
277 | $str .= (new $class($name, $this->inputName))->render(); |
||
278 | } |
||
279 | |||
280 | return $str; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Tab Maker (Each tabs mean 4 space) |
||
285 | */ |
||
286 | public function makeTab($count, $newLine = true){ |
||
287 | $count = $count * 4; |
||
288 | $tabs = str_repeat(' ', $count); |
||
289 | |||
290 | return $newLine ? "\n".$tabs : $tabs; |
||
291 | } |
||
292 | |||
293 | public function getInputClassNamespace($type) |
||
298 | } |
||
299 | |||
300 | private function normalizeField($field) |
||
301 | { |
||
309 | } |
||
310 | |||
312 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.