Total Complexity | 96 |
Total Lines | 766 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like Func_App 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 Func_App, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Func_App extends bab_Functionality |
||
36 | { |
||
37 | public $addonPrefix; |
||
38 | public $classPrefix; |
||
39 | public $addonName; |
||
40 | public $controllerTg; |
||
41 | public $phpPath; |
||
42 | public $recordSetPath; |
||
43 | public $ctrlPath; |
||
44 | public $uiPath; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $language = null; |
||
51 | |||
52 | |||
53 | public function __construct() |
||
54 | { |
||
55 | $this->addonName = 'libapp'; |
||
56 | $this->addonPrefix = 'app'; |
||
57 | |||
58 | $this->classPrefix = $this->addonPrefix . '_'; |
||
59 | $this->controllerTg = 'addon/' . $this->addonName . '/main'; |
||
60 | |||
61 | $addon = bab_getAddonInfosInstance($this->addonName); |
||
62 | $this->phpPath = $addon->getPhpPath(); |
||
63 | $this->recordSetPath = $this->phpPath; |
||
64 | $this->ctrlPath = $this->phpPath; |
||
65 | $this->uiPath = $this->phpPath . 'ui/'; |
||
66 | |||
67 | $babDB = bab_getDB(); |
||
68 | |||
69 | $LibOrm = bab_Functionality::get('LibOrm'); |
||
70 | /*@var $LibOrm Func_LibOrm */ |
||
71 | |||
72 | $LibOrm->initMysql(); |
||
73 | ORM_RecordSet::setBackend(new ORM_MySqlBackend($babDB)); |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @param string $class |
||
79 | * @return boolean |
||
80 | */ |
||
81 | public function loadObject($class) |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the expected pathname for the file containing the definition of the record set class. |
||
105 | * |
||
106 | * @param string $class |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getRecordSetPathname($class) |
||
110 | { |
||
111 | // $App->MyRecordSet() -> myrecord.class.php |
||
112 | $file = strtolower(substr($class, strlen($this->classPrefix), -3)) . '.class.php'; |
||
113 | return $this->recordSetPath . $file; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns the expected pathname for the file containing the definition of the record class. |
||
118 | * |
||
119 | * @param string $class |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getRecordPathname($class) |
||
123 | { |
||
124 | // $App->MyRecord() -> myrecord.class.php |
||
125 | $file = strtolower(substr($class, strlen($this->classPrefix))) . '.class.php'; |
||
126 | return $this->recordSetPath . $file; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getUiPath() |
||
133 | { |
||
134 | return APP_UI_PATH; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getSetPath() |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getCtrlPath() |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getPhpPath() |
||
157 | { |
||
158 | return APP_PHP_PATH; |
||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | * |
||
165 | */ |
||
166 | public function getDescription() |
||
167 | { |
||
168 | return 'Application framework.'; |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Get the addon name |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getAddonName() |
||
177 | { |
||
178 | return $this->addonName; |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return bab_addonInfos |
||
184 | */ |
||
185 | public function getAddon() |
||
186 | { |
||
187 | return bab_getAddonInfosInstance($this->getAddonName()); |
||
188 | } |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Register myself as a functionality. |
||
193 | * |
||
194 | */ |
||
195 | public static function register() |
||
196 | { |
||
197 | require_once $GLOBALS['babInstallPath'].'utilit/functionalityincl.php'; |
||
198 | $functionalities = new bab_functionalities(); |
||
|
|||
199 | |||
200 | $addon = bab_getAddonInfosInstance('libapp'); |
||
201 | |||
202 | $addon->registerFunctionality('App', 'app.php'); |
||
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Synchronize sql tables for all classes found in Application object |
||
208 | * using methods 'includeXxxxxClassName' |
||
209 | * sychronize if the two correspond methods are met and the set classname match the prefix from parameter |
||
210 | * do not synchronize if the set method has a VueSet suffix, in this case the table si juste a readonly vue |
||
211 | * |
||
212 | * @param string $prefix tables and classes prefix |
||
213 | * @return bab_synchronizeSql |
||
214 | */ |
||
215 | public function synchronizeSql($prefix) |
||
216 | { |
||
217 | if (!$prefix) { |
||
218 | return null; |
||
219 | } |
||
220 | |||
221 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
222 | $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
||
223 | $sql = 'SET FOREIGN_KEY_CHECKS=0; |
||
224 | '; |
||
225 | |||
226 | foreach (get_class_methods($this) as $method) { |
||
227 | |||
228 | if (substr($method, 0, 7) === 'include' && substr($method, -3) === 'Set') { |
||
229 | $incl = $method; |
||
230 | $classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
||
231 | |||
232 | |||
233 | $classname = $this->$classNameMethod(); |
||
234 | |||
235 | if ($prefix === substr($classname, 0, strlen($prefix)) |
||
236 | && 'Set' === substr($classname, -3) |
||
237 | && 'ViewSet' !== substr($classname, -7)) { |
||
238 | if (method_exists($this, $incl)) { |
||
239 | $this->$incl(); |
||
240 | |||
241 | $call = substr($classname, strlen($prefix)); |
||
242 | |||
243 | if (class_exists($classname)) { |
||
244 | |||
245 | /* @var $set ORM_RecordSet */ |
||
246 | $set = $this->$call(); |
||
247 | if (method_exists($set, 'useLang')) { |
||
248 | // This is necessary if the recordSet constructor uses a setLang(). |
||
249 | // We need to revert to multilang fields before synchronizing. |
||
250 | $set->useLang(false); |
||
251 | } |
||
252 | $sql .= $mysqlbackend->setToSql($set) . "\n"; |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
260 | $synchronize = new bab_synchronizeSql(); |
||
261 | |||
262 | $synchronize->fromSqlString($sql); |
||
263 | |||
264 | return $synchronize; |
||
265 | } |
||
266 | |||
267 | |||
268 | |||
269 | public function getSynchronizeSql($prefix) |
||
270 | { |
||
271 | if (!$prefix) { |
||
272 | return null; |
||
273 | } |
||
274 | |||
275 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
276 | $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
||
277 | $sql = ''; |
||
278 | |||
279 | foreach (get_class_methods($this) as $method) { |
||
280 | |||
281 | if (substr($method, 0, strlen('include')) === 'include' && substr($method, -strlen('Set')) === 'Set') { |
||
282 | $incl = $method; |
||
283 | $classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
||
284 | |||
285 | $classname = $this->$classNameMethod(); |
||
286 | |||
287 | if ($prefix === substr($classname, 0, strlen($prefix)) |
||
288 | && 'Set' === substr($classname, -3) |
||
289 | && 'ViewSet' !== substr($classname, -6) |
||
290 | ) { |
||
291 | if (method_exists($this, $incl)) { |
||
292 | $this->$incl(); |
||
293 | $call = substr($classname, strlen($prefix)); |
||
294 | |||
295 | if (class_exists($classname) && method_exists($this, $call)) { |
||
296 | $set = $this->$call(); |
||
297 | $sql .= $mysqlbackend->setToSql($set) . "\n"; |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return $sql; |
||
305 | } |
||
306 | |||
307 | |||
308 | |||
309 | public function includeBase() |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Includes app_RecordSet class definition. |
||
316 | */ |
||
317 | public function includeRecordSet() |
||
320 | } |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Includes app_TraceableRecordSet class definition. |
||
325 | */ |
||
326 | public function includeTraceableRecordSet() |
||
329 | } |
||
330 | |||
331 | |||
332 | |||
333 | /** |
||
334 | * SET DEFINITION |
||
335 | */ |
||
336 | |||
337 | //Log |
||
338 | |||
339 | /** |
||
340 | * Includes LogSet class definition. |
||
341 | */ |
||
342 | public function includeLogSet() |
||
343 | { |
||
344 | require_once APP_SET_PATH . 'log.class.php'; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return string |
||
349 | */ |
||
350 | public function LogClassName() |
||
351 | { |
||
352 | return 'app_Log'; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function LogSetClassName() |
||
359 | { |
||
360 | return $this->LogClassName() . 'Set'; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @return app_LogSet |
||
365 | */ |
||
366 | public function LogSet() |
||
367 | { |
||
368 | $this->includeLogSet(); |
||
369 | $className = $this->LogSetClassName(); |
||
370 | $set = new $className($this); |
||
371 | return $set; |
||
372 | } |
||
373 | |||
374 | |||
375 | //Link |
||
376 | |||
377 | /** |
||
378 | * Includes LinkSet class definition. |
||
379 | */ |
||
380 | public function includeLinkSet() |
||
381 | { |
||
382 | require_once APP_SET_PATH . 'link.class.php'; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | public function LinkClassName() |
||
389 | { |
||
390 | return 'app_Link'; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @return string |
||
395 | */ |
||
396 | public function LinkSetClassName() |
||
397 | { |
||
398 | return $this->LinkClassName() . 'Set'; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @return app_LinkSet |
||
403 | */ |
||
404 | public function LinkSet() |
||
405 | { |
||
406 | $this->includeLinkSet(); |
||
407 | $className = $this->LinkSetClassName(); |
||
408 | $set = new $className($this); |
||
409 | return $set; |
||
410 | } |
||
411 | |||
412 | |||
413 | |||
414 | |||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * Returns the app_Record corresponding to the specified |
||
420 | * reference $ref. |
||
421 | * |
||
422 | * @param string $ref A reference string (e.g. Contact:12) |
||
423 | * @return app_Record or null if no corresponding record is found. |
||
424 | */ |
||
425 | public function getRecordByRef($ref) |
||
426 | { |
||
427 | $refParts = explode(':', $ref); |
||
428 | if (count($refParts) !== 2) { |
||
429 | return null; |
||
430 | } |
||
431 | list($classname, $id) = $refParts; |
||
432 | $classSet = $classname . 'Set'; |
||
433 | $set = $this->$classSet(); |
||
434 | if (isset($set)) { |
||
435 | return $set->get($id); |
||
436 | } |
||
437 | return null; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Returns the reference corresponding to the specified |
||
442 | * app_Record $record (e.g. Contact:12 or Deal:125) |
||
443 | * |
||
444 | * @param app_Record $record |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getRecordRef(app_Record $record) |
||
448 | { |
||
449 | $fullClassName = get_class($record); |
||
450 | list(, $className) = explode('_', $fullClassName); |
||
451 | return $className . ':' . $record->id; |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Specifies the language to use for translation. |
||
457 | * |
||
458 | * If $language is null, the language is reset to |
||
459 | * the current logged user language. |
||
460 | * |
||
461 | * @param string|null $language |
||
462 | */ |
||
463 | public function setTranslateLanguage($language) |
||
464 | { |
||
465 | $this->language = $language; |
||
466 | } |
||
467 | |||
468 | |||
469 | |||
470 | /** |
||
471 | * Translates the string. |
||
472 | * |
||
473 | * @param string $str |
||
474 | * @return string |
||
475 | */ |
||
476 | public function translate($str, $str_plurals = null, $number = null) |
||
477 | { |
||
478 | require_once APP_PHP_PATH . 'functions.php'; |
||
479 | $translation = $str; |
||
480 | if ($translate = bab_functionality::get('Translate/Gettext')) { |
||
481 | /* @var $translate Func_Translate_Gettext */ |
||
482 | $translate->setAddonName('libapp'); |
||
483 | $translation = $translate->translate($str, $str_plurals, $number); |
||
484 | } |
||
485 | |||
486 | return $translation; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * @param string $str |
||
491 | * @param string $str_plurals |
||
492 | * @param int $number |
||
493 | * @return string |
||
494 | */ |
||
495 | public function translatable($str, $str_plurals = null, $number = null) |
||
496 | { |
||
497 | return $str; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Translates all the string in an array and returns a new array. |
||
502 | * |
||
503 | * @param array $arr |
||
504 | * @return array |
||
505 | */ |
||
506 | public function translateArray($arr) |
||
514 | } |
||
515 | |||
516 | |||
517 | |||
518 | /** |
||
519 | * Returns a link for writting an email to the specified email address. |
||
520 | * |
||
521 | * @param string $addr |
||
522 | * @param string $subject |
||
523 | * @param string $body |
||
524 | * |
||
525 | * @return string |
||
526 | */ |
||
527 | public function mailTo($addr, $subject = null, $body = null) |
||
528 | { |
||
529 | $mailTo = 'mailto:' . $addr; |
||
530 | $parameters = array(); |
||
531 | if (isset($subject)) { |
||
532 | $parameters[] = 'subject=' . $subject; |
||
533 | } |
||
534 | if (isset($body)) { |
||
535 | $parameters[] = 'body=' . $body; |
||
536 | } |
||
537 | if (!empty($parameters)) { |
||
538 | $mailTo .= '?' . implode('&', $parameters); |
||
539 | } |
||
540 | |||
541 | return $mailTo; |
||
542 | } |
||
543 | |||
544 | |||
545 | |||
546 | /** |
||
547 | * Format a number for display |
||
548 | * |
||
549 | * @param float|string|null $number Numeric value with decimal |
||
550 | * @return string |
||
551 | */ |
||
552 | public function numberFormat($number, $decimals = 2) |
||
553 | { |
||
554 | if (is_null($number)) { |
||
555 | return '#,##'; |
||
556 | } |
||
557 | |||
558 | $number = number_format(floatval($number), $decimals, ',', ' '); |
||
559 | return str_replace(' ', bab_nbsp(), $number); |
||
560 | } |
||
561 | |||
562 | |||
563 | /** |
||
564 | * Format a number with an optional unit. |
||
565 | * |
||
566 | * If the value is >= 1000 the value is shortened and the corresponding prexif (k or M) is used. |
||
567 | * |
||
568 | * @param float|string|null $number |
||
569 | * @param string $unitSymbol (For example $, m2, Wh) |
||
570 | * @param int $decimals |
||
571 | * @return string|mixed |
||
572 | */ |
||
573 | public function shortFormatWithUnit($number, $unitSymbol = '', $decimals = 2) |
||
574 | { |
||
575 | if (is_null($number)) { |
||
576 | return '#,##'; |
||
577 | } |
||
578 | |||
579 | $prefix = ''; |
||
580 | if ($number >= 1000000) { |
||
581 | $number /= 1000000; |
||
582 | $prefix = 'M'; |
||
583 | } elseif ($number >= 1000) { |
||
584 | $number /= 1000; |
||
585 | $prefix = 'k'; |
||
586 | } |
||
587 | |||
588 | $number = number_format($number, $decimals, ',', ' '); |
||
589 | return str_replace(' ', bab_nbsp(), $number . ' ' . $prefix . $unitSymbol); |
||
590 | } |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Reformat a phone number in the specified format. |
||
595 | * |
||
596 | * @param string $phone The phone number to be formatted |
||
597 | * @param int $format The format the phone number should be formatted into |
||
598 | * |
||
599 | * @return string The formatted phone number |
||
600 | */ |
||
601 | public function phoneNumberFormat($phone, $format = null) |
||
602 | { |
||
603 | $PhoneNumber = bab_Functionality::get('PhoneNumber'); |
||
604 | if ($PhoneNumber === false) { |
||
605 | return $phone; |
||
606 | } |
||
607 | |||
608 | if (!isset($format)) { |
||
609 | $format = bab_registry::get('/' . $this->addonName . '/numberFormat', $PhoneNumber->getDefaultFormat()); |
||
610 | } |
||
611 | $phoneNumberUtil = $PhoneNumber->PhoneNumberUtil(); |
||
612 | |||
613 | try { |
||
614 | $phoneNumber = $phoneNumberUtil->parse($phone, 'FR'); |
||
615 | $phone = $phoneNumberUtil->format($phoneNumber, $format); |
||
616 | } catch (\libphonenumber\NumberParseException $e) { |
||
617 | } |
||
618 | |||
619 | return $phone; |
||
620 | } |
||
621 | |||
622 | |||
623 | |||
624 | |||
625 | /** |
||
626 | * Includes Controller class definition. |
||
627 | */ |
||
628 | public function includeController() |
||
629 | { |
||
630 | require_once APP_PHP_PATH . '/controller.class.php'; |
||
631 | } |
||
632 | |||
633 | |||
634 | /** |
||
635 | * Includes RecordController class definition. |
||
636 | */ |
||
637 | public function includeRecordController() |
||
638 | { |
||
639 | require_once APP_CTRL_PATH . '/record.ctrl.php'; |
||
640 | } |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Instanciates the controller. |
||
645 | * |
||
646 | * @return app_Controller |
||
647 | */ |
||
648 | public function Controller() |
||
649 | { |
||
650 | $this->includeController(); |
||
651 | return bab_getInstance($this->classPrefix.'Controller')->setApp($this); |
||
652 | } |
||
653 | |||
654 | |||
655 | /** |
||
656 | * Instanciates a controller class. |
||
657 | * |
||
658 | * @return bab_Controller |
||
659 | */ |
||
660 | public function ControllerProxy($className, $proxy = true) |
||
661 | { |
||
662 | $this->includeController(); |
||
663 | |||
664 | if ($proxy) { |
||
665 | return app_Controller::getProxyInstance($this, $className); |
||
666 | } |
||
667 | |||
668 | return new $className($this); |
||
669 | } |
||
670 | |||
671 | |||
672 | |||
673 | /** |
||
674 | * Include class app_Ui |
||
675 | * |
||
676 | */ |
||
677 | public function includeUi() |
||
680 | } |
||
681 | |||
682 | |||
683 | /** |
||
684 | * The app_Ui object propose an access to all ui files and ui objects (widgets) |
||
685 | * |
||
686 | * @return app_Ui |
||
687 | */ |
||
688 | public function Ui() |
||
689 | { |
||
690 | $this->includeUi(); |
||
691 | return bab_getInstance($this->classPrefix . 'Ui');//->setApp($this); |
||
692 | } |
||
693 | |||
694 | |||
695 | |||
696 | protected function includeAccess() |
||
697 | { |
||
698 | require_once WORKSPACE_PHP_PATH . '/access.class.php'; |
||
699 | } |
||
700 | |||
701 | |||
702 | |||
703 | |||
704 | |||
705 | /** |
||
706 | * Get upload path |
||
707 | * if the method return null, no upload functionality |
||
708 | * |
||
709 | * @return bab_Path |
||
710 | */ |
||
711 | public function getUploadPath() |
||
712 | { |
||
713 | require_once $GLOBALS['babInstallPath'].'utilit/path.class.php'; |
||
714 | return new bab_Path(bab_getAddonInfosInstance($this->getAddonName())->getUploadPath()); |
||
715 | } |
||
716 | |||
717 | |||
718 | /** |
||
719 | * |
||
720 | * @param string $name |
||
721 | * @param mixed $arguments |
||
722 | * @return mixed |
||
723 | */ |
||
724 | public function __call($name, $arguments) |
||
762 | } |
||
763 | } |
||
764 | |||
765 | |||
766 | /** |
||
767 | * Test if this App implementation handles the specified object class. |
||
768 | * |
||
769 | * The default is to consider that an object Xxxx implemented if the includeXxxxSet() method is |
||
770 | * declared public on this App. |
||
771 | * |
||
772 | * @param string $objectClassName App object name (eg. 'Contact' or 'CatalogItem') |
||
773 | * @return bool |
||
774 | */ |
||
775 | public function __isset($objectClassName) |
||
801 | } |
||
802 | } |
||
803 | |||
804 | spl_autoload_register(array(app_App(), 'loadObject')); |
||
805 | |||
806 |