Total Complexity | 43 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like CallableObjectOptions 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 CallableObjectOptions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CallableObjectOptions |
||
25 | { |
||
26 | /** |
||
27 | * Check if the js code for this object must be generated |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $bExcluded = false; |
||
32 | |||
33 | /** |
||
34 | * The character to use as separator in javascript class names |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $sSeparator = '.'; |
||
39 | |||
40 | /** |
||
41 | * A list of methods of the user registered callable object the library must not export to javascript |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $aProtectedMethods = []; |
||
46 | |||
47 | /** |
||
48 | * A list of methods to call before processing the request |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $aBeforeMethods = []; |
||
53 | |||
54 | /** |
||
55 | * A list of methods to call after processing the request |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $aAfterMethods = []; |
||
60 | |||
61 | /** |
||
62 | * The javascript class options |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | private $aJsOptions = []; |
||
67 | |||
68 | /** |
||
69 | * The DI options |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $aDiOptions = []; |
||
74 | |||
75 | /** |
||
76 | * The constructor |
||
77 | * |
||
78 | * @param array $aOptions |
||
79 | * @param array $aAnnotations |
||
80 | */ |
||
81 | public function __construct(array $aOptions, array $aAnnotations) |
||
82 | { |
||
83 | [$bExcluded, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations; |
||
84 | $this->bExcluded = $bExcluded || (bool)($aOptions['excluded'] ?? false); |
||
85 | if($this->bExcluded) |
||
86 | { |
||
87 | return; |
||
88 | } |
||
89 | |||
90 | $sSeparator = $aOptions['separator']; |
||
91 | if($sSeparator === '_' || $sSeparator === '.') |
||
92 | { |
||
93 | $this->sSeparator = $sSeparator; |
||
94 | } |
||
95 | $this->addProtectedMethods($aOptions['protected']); |
||
96 | $this->addProtectedMethods($aAnnotationProtected); |
||
97 | |||
98 | foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
||
99 | { |
||
100 | $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list. |
||
101 | foreach($aFunctionNames as $sFunctionName) |
||
102 | { |
||
103 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
||
104 | } |
||
105 | } |
||
106 | foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions) |
||
107 | { |
||
108 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param mixed $xMethods |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | private function addProtectedMethods($xMethods) |
||
118 | { |
||
119 | if(!is_array($xMethods)) |
||
120 | { |
||
121 | $this->aProtectedMethods[trim((string)$xMethods)] = true; |
||
122 | return; |
||
123 | } |
||
124 | foreach($xMethods as $sMethod) |
||
125 | { |
||
126 | $this->aProtectedMethods[trim((string)$sMethod)] = true; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Check if the js code for this object must be generated |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function excluded(): bool |
||
136 | { |
||
137 | return $this->bExcluded; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function separator(): string |
||
144 | { |
||
145 | return $this->sSeparator; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string $sMethodName |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isProtectedMethod(string $sMethodName): bool |
||
154 | { |
||
155 | return isset($this->aProtectedMethods[$sMethodName]) || isset($this->aProtectedMethods['*']); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | public function beforeMethods(): array |
||
162 | { |
||
163 | return $this->aBeforeMethods; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return array |
||
168 | */ |
||
169 | public function afterMethods(): array |
||
170 | { |
||
171 | return $this->aAfterMethods; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return array |
||
176 | */ |
||
177 | public function diOptions(): array |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | */ |
||
185 | public function jsOptions(): array |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Set hook methods |
||
192 | * |
||
193 | * @param array $aHookMethods The array of hook methods |
||
194 | * @param string|array $xValue The value of the configuration option |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | private function setHookMethods(array &$aHookMethods, $xValue) |
||
199 | { |
||
200 | foreach($xValue as $sCalledMethod => $xMethodToCall) |
||
201 | { |
||
202 | if(is_array($xMethodToCall)) |
||
203 | { |
||
204 | $aHookMethods[$sCalledMethod] = $xMethodToCall; |
||
205 | } |
||
206 | elseif(is_string($xMethodToCall)) |
||
207 | { |
||
208 | $aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param array $aDiOptions |
||
215 | */ |
||
216 | private function addDiOption(array $aDiOptions) |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Set configuration options / call options for each method |
||
223 | * |
||
224 | * @param string $sName The name of the configuration option |
||
225 | * @param string|array $xValue The value of the configuration option |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | private function addOption(string $sName, $xValue) |
||
230 | { |
||
231 | switch($sName) |
||
232 | { |
||
233 | // Set the methods to call before processing the request |
||
234 | case '__before': |
||
235 | $this->setHookMethods($this->aBeforeMethods, $xValue); |
||
236 | break; |
||
237 | // Set the methods to call after processing the request |
||
238 | case '__after': |
||
239 | $this->setHookMethods($this->aAfterMethods, $xValue); |
||
240 | break; |
||
241 | // Set the attributes to inject in the callable object |
||
242 | case '__di': |
||
243 | $this->addDiOption($xValue); |
||
244 | break; |
||
245 | default: |
||
246 | break; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $sFunctionName |
||
252 | * @param string $sOptionName |
||
253 | * @param mixed $xOptionValue |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
258 | { |
||
259 | if(is_string($xOptionValue)) |
||
260 | { |
||
261 | $xOptionValue = [$xOptionValue]; |
||
262 | } |
||
263 | if(!is_array($xOptionValue)) |
||
264 | { |
||
265 | return; // Do not save. |
||
266 | } |
||
267 | $aOptions = $this->aJsOptions[$sFunctionName][$sOptionName] ?? []; |
||
268 | $this->aJsOptions[$sFunctionName][$sOptionName] = array_merge($aOptions, $xOptionValue); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param string $sFunctionName |
||
273 | * @param string $sOptionName |
||
274 | * @param mixed $xOptionValue |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | private function _setJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
279 | { |
||
280 | $this->aJsOptions[$sFunctionName][$sOptionName] = $xOptionValue; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $sFunctionName |
||
285 | * @param string $sOptionName |
||
286 | * @param mixed $xOptionValue |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
||
291 | { |
||
292 | switch($sOptionName) |
||
293 | { |
||
294 | case 'excluded': |
||
295 | if((bool)$xOptionValue) |
||
296 | { |
||
297 | $this->addProtectedMethods($sFunctionName); |
||
298 | } |
||
299 | break; |
||
300 | // For databags, all the value are merged in a single array. |
||
301 | case 'bags': |
||
302 | $this->_addJsArrayOption($sFunctionName, $sOptionName, $xOptionValue); |
||
303 | return; |
||
304 | // For all the other options, including callback, only the last value is kept. |
||
305 | case 'callback': |
||
306 | default: |
||
307 | $this->_setJsOption($sFunctionName, $sOptionName, $xOptionValue); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param string $sFunctionName |
||
313 | * @param array $aFunctionOptions |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions) |
||
318 | { |
||
319 | foreach($aFunctionOptions as $sOptionName => $xOptionValue) |
||
320 | { |
||
321 | substr($sOptionName, 0, 2) === '__' ? |
||
322 | // Options for PHP classes. They start with "__". |
||
323 | $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) : |
||
324 | // Options for javascript code. |
||
325 | $this->addJsOption($sFunctionName, $sOptionName, $xOptionValue); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param string $sMethodName |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getMethodOptions(string $sMethodName): array |
||
348 | } |
||
349 | } |
||
350 |