Total Complexity | 99 |
Total Lines | 785 |
Duplicated Lines | 0 % |
Changes | 12 | ||
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() |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Get the addon name |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getAddonName() |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return bab_addonInfos |
||
184 | */ |
||
185 | public function getAddon() |
||
188 | } |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Register myself as a functionality. |
||
193 | * |
||
194 | */ |
||
195 | public static function register() |
||
196 | { |
||
197 | $addon = bab_getAddonInfosInstance('libapp'); |
||
198 | |||
199 | $addon->registerFunctionality('App', 'app.php'); |
||
200 | } |
||
201 | |||
202 | |||
203 | /** |
||
204 | * Synchronize sql tables for all classes found in Application object |
||
205 | * using methods 'includeXxxxxClassName' |
||
206 | * sychronize if the two correspond methods are met and the set classname match the prefix from parameter |
||
207 | * do not synchronize if the set method has a VueSet suffix, in this case the table si juste a readonly vue |
||
208 | * |
||
209 | * @param string $prefix tables and classes prefix |
||
210 | * @return bab_synchronizeSql |
||
211 | */ |
||
212 | public function synchronizeSql($prefix) |
||
213 | { |
||
214 | if (!$prefix) { |
||
215 | return null; |
||
216 | } |
||
217 | |||
218 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
219 | $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
||
220 | $sql = 'SET FOREIGN_KEY_CHECKS=0; |
||
221 | '; |
||
222 | |||
223 | foreach (get_class_methods($this) as $method) { |
||
224 | |||
225 | if (substr($method, 0, 7) === 'include' && substr($method, -3) === 'Set') { |
||
226 | $incl = $method; |
||
227 | $classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
||
228 | |||
229 | |||
230 | $classname = $this->$classNameMethod(); |
||
231 | |||
232 | if ($prefix === substr($classname, 0, strlen($prefix)) |
||
233 | && 'Set' === substr($classname, -3) |
||
234 | && 'ViewSet' !== substr($classname, -7)) { |
||
235 | if (method_exists($this, $incl)) { |
||
236 | $this->$incl(); |
||
237 | |||
238 | $call = substr($classname, strlen($prefix)); |
||
239 | |||
240 | if (class_exists($classname)) { |
||
241 | |||
242 | /* @var $set ORM_RecordSet */ |
||
243 | $set = $this->$call(); |
||
244 | if (method_exists($set, 'useLang')) { |
||
245 | // This is necessary if the recordSet constructor uses a setLang(). |
||
246 | // We need to revert to multilang fields before synchronizing. |
||
247 | $set->useLang(false); |
||
|
|||
248 | } |
||
249 | $sql .= $mysqlbackend->setToSql($set) . "\n"; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
257 | $synchronize = new bab_synchronizeSql(); |
||
258 | |||
259 | $synchronize->fromSqlString($sql); |
||
260 | |||
261 | return $synchronize; |
||
262 | } |
||
263 | |||
264 | |||
265 | |||
266 | public function getSynchronizeSql($prefix) |
||
267 | { |
||
268 | if (!$prefix) { |
||
269 | return null; |
||
270 | } |
||
271 | |||
272 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
273 | $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
||
274 | $sql = ''; |
||
275 | |||
276 | foreach (get_class_methods($this) as $method) { |
||
277 | |||
278 | if (substr($method, 0, strlen('include')) === 'include' && substr($method, -strlen('Set')) === 'Set') { |
||
279 | $incl = $method; |
||
280 | $classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
||
281 | |||
282 | $classname = $this->$classNameMethod(); |
||
283 | |||
284 | if ($prefix === substr($classname, 0, strlen($prefix)) |
||
285 | && 'Set' === substr($classname, -3) |
||
286 | && 'ViewSet' !== substr($classname, -6) |
||
287 | ) { |
||
288 | if (method_exists($this, $incl)) { |
||
289 | $this->$incl(); |
||
290 | $call = substr($classname, strlen($prefix)); |
||
291 | |||
292 | if (class_exists($classname) && method_exists($this, $call)) { |
||
293 | $set = $this->$call(); |
||
294 | $sql .= $mysqlbackend->setToSql($set) . "\n"; |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | |||
301 | return $sql; |
||
302 | } |
||
303 | |||
304 | |||
305 | |||
306 | public function includeBase() |
||
307 | { |
||
308 | require_once APP_PHP_PATH . 'base.class.php'; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Includes app_RecordSet class definition. |
||
313 | */ |
||
314 | public function includeRecordSet() |
||
315 | { |
||
316 | require_once APP_SET_PATH . 'record.class.php'; |
||
317 | } |
||
318 | |||
319 | |||
320 | /** |
||
321 | * Includes app_TraceableRecordSet class definition. |
||
322 | */ |
||
323 | public function includeTraceableRecordSet() |
||
324 | { |
||
325 | require_once APP_SET_PATH . 'traceablerecord.class.php'; |
||
326 | } |
||
327 | |||
328 | |||
329 | /** |
||
330 | * |
||
331 | * @return app_AccessManager |
||
332 | */ |
||
333 | public function AccessManager() |
||
334 | { |
||
335 | static $accessManager = null; |
||
336 | if (!isset($accessManager)) { |
||
337 | $accessManager = new app_AccessManager($this); |
||
338 | } |
||
339 | |||
340 | return $accessManager; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * SET DEFINITION |
||
345 | */ |
||
346 | |||
347 | //Log |
||
348 | |||
349 | /** |
||
350 | * Includes LogSet class definition. |
||
351 | */ |
||
352 | public function includeLogSet() |
||
353 | { |
||
354 | require_once APP_SET_PATH . 'log.class.php'; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | public function LogClassName() |
||
361 | { |
||
362 | return 'app_Log'; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @return string |
||
367 | */ |
||
368 | public function LogSetClassName() |
||
369 | { |
||
370 | return $this->LogClassName() . 'Set'; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @return app_LogSet |
||
375 | */ |
||
376 | public function LogSet() |
||
377 | { |
||
378 | $this->includeLogSet(); |
||
379 | $className = $this->LogSetClassName(); |
||
380 | $set = new $className($this); |
||
381 | return $set; |
||
382 | } |
||
383 | |||
384 | |||
385 | //Link |
||
386 | |||
387 | /** |
||
388 | * Includes LinkSet class definition. |
||
389 | */ |
||
390 | public function includeLinkSet() |
||
391 | { |
||
392 | require_once APP_SET_PATH . 'link.class.php'; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function LinkClassName() |
||
399 | { |
||
400 | return 'app_Link'; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return string |
||
405 | */ |
||
406 | public function LinkSetClassName() |
||
407 | { |
||
408 | return $this->LinkClassName() . 'Set'; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @return app_LinkSet |
||
413 | */ |
||
414 | public function LinkSet() |
||
415 | { |
||
416 | $this->includeLinkSet(); |
||
417 | $className = $this->LinkSetClassName(); |
||
418 | $set = new $className($this); |
||
419 | return $set; |
||
420 | } |
||
421 | |||
422 | |||
423 | /** |
||
424 | * |
||
425 | * @param string $recordClassname |
||
426 | * |
||
427 | * @return ORM_RecordSet |
||
428 | */ |
||
429 | public function getRecordSetByRef($recordClassname) |
||
430 | { |
||
431 | $classSet = $recordClassname . 'Set'; |
||
432 | $set = $this->$classSet(); |
||
433 | return $set; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Returns the app_Record corresponding to the specified |
||
438 | * reference $ref. |
||
439 | * |
||
440 | * @param string $ref A reference string (e.g. Contact:12) |
||
441 | * @return app_Record or null if no corresponding record is found. |
||
442 | */ |
||
443 | public function getRecordByRef($ref) |
||
444 | { |
||
445 | $refParts = explode(':', $ref); |
||
446 | if (count($refParts) !== 2) { |
||
447 | return null; |
||
448 | } |
||
449 | list($recordClassname, $id) = $refParts; |
||
450 | $set = $this->getRecordSetByRef($recordClassname); |
||
451 | if (isset($set)) { |
||
452 | return $set->get($id); |
||
453 | } |
||
454 | return null; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Returns the reference corresponding to the specified |
||
459 | * app_Record $record (e.g. Contact:12 or Deal:125) |
||
460 | * |
||
461 | * @param app_Record $record |
||
462 | * @return string |
||
463 | */ |
||
464 | public function getRecordRef(app_Record $record) |
||
465 | { |
||
466 | $fullClassName = get_class($record); |
||
467 | list(, $className) = explode('_', $fullClassName); |
||
468 | return $className . ':' . $record->id; |
||
469 | } |
||
470 | |||
471 | |||
472 | /** |
||
473 | * |
||
474 | * @return Func_Translate_Gettext |
||
475 | */ |
||
476 | private static function getTranslator() |
||
477 | { |
||
478 | static $translator = null; |
||
479 | if (!isset($translator)) { |
||
480 | $translator = bab_functionality::get('Translate/Gettext'); |
||
481 | $translator->setAddonName('libapp'); |
||
482 | } |
||
483 | |||
484 | return $translator; |
||
485 | } |
||
486 | |||
487 | |||
488 | /** |
||
489 | * Specifies the language to use for translation. |
||
490 | * |
||
491 | * If $language is null, the language is reset to |
||
492 | * the current logged user language. |
||
493 | * |
||
494 | * @param string|null $language |
||
495 | */ |
||
496 | public function setTranslateLanguage($language) |
||
497 | { |
||
498 | $this->language = $language; |
||
499 | } |
||
500 | |||
501 | |||
502 | |||
503 | /** |
||
504 | * Translates the string. |
||
505 | * |
||
506 | * @param string $str Text to translate or singular form |
||
507 | * @param string $str_plurals Plurals form string |
||
508 | * @param int $number Number of items for plurals |
||
509 | * @return string |
||
510 | */ |
||
511 | public function translate($str, $str_plurals = null, $number = null) |
||
512 | { |
||
513 | $translator = self::getTranslator(); |
||
514 | $translation = $translator->translate($str, $str_plurals, $number); |
||
515 | |||
516 | return $translation; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param string $str |
||
521 | * @param string $str_plurals |
||
522 | * @param int $number |
||
523 | * @return string |
||
524 | */ |
||
525 | public function translatable($str, $str_plurals = null, $number = null) |
||
526 | { |
||
527 | return $str; |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Translates all the string in an array and returns a new array. |
||
532 | * |
||
533 | * @param array $arr |
||
534 | * @return array |
||
535 | */ |
||
536 | public function translateArray($arr) |
||
544 | } |
||
545 | |||
546 | |||
547 | |||
548 | /** |
||
549 | * Returns a link for writting an email to the specified email address. |
||
550 | * |
||
551 | * @param string $addr |
||
552 | * @param string $subject |
||
553 | * @param string $body |
||
554 | * |
||
555 | * @return string |
||
556 | */ |
||
557 | public function mailTo($addr, $subject = null, $body = null) |
||
558 | { |
||
559 | $mailTo = 'mailto:' . $addr; |
||
560 | $parameters = array(); |
||
561 | if (isset($subject)) { |
||
562 | $parameters[] = 'subject=' . $subject; |
||
563 | } |
||
564 | if (isset($body)) { |
||
565 | $parameters[] = 'body=' . $body; |
||
566 | } |
||
567 | if (!empty($parameters)) { |
||
568 | $mailTo .= '?' . implode('&', $parameters); |
||
569 | } |
||
570 | |||
571 | return $mailTo; |
||
572 | } |
||
573 | |||
574 | |||
575 | |||
576 | /** |
||
577 | * Format a number for display |
||
578 | * |
||
579 | * @param float|string|null $number Numeric value with decimal |
||
580 | * @return string |
||
581 | */ |
||
582 | public function numberFormat($number, $decimals = 2) |
||
583 | { |
||
584 | if (is_null($number)) { |
||
585 | return '#,##'; |
||
586 | } |
||
587 | |||
588 | $number = number_format(floatval($number), $decimals, ',', ' '); |
||
589 | return str_replace(' ', bab_nbsp(), $number); |
||
590 | } |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Format a number with an optional unit. |
||
595 | * |
||
596 | * If the value is >= 1000 the value is shortened and the corresponding prexif (k or M) is used. |
||
597 | * |
||
598 | * @param float|string|null $number |
||
599 | * @param string $unitSymbol (For example $, m2, Wh) |
||
600 | * @param int $decimals |
||
601 | * @return string|mixed |
||
602 | */ |
||
603 | public function shortFormatWithUnit($number, $unitSymbol = '', $decimals = 2) |
||
604 | { |
||
605 | if (is_null($number)) { |
||
606 | return '#,##'; |
||
607 | } |
||
608 | |||
609 | $prefix = ''; |
||
610 | if ($number >= 1000000) { |
||
611 | $number /= 1000000; |
||
612 | $prefix = 'M'; |
||
613 | } elseif ($number >= 1000) { |
||
614 | $number /= 1000; |
||
615 | $prefix = 'k'; |
||
616 | } |
||
617 | |||
618 | $number = number_format($number, $decimals, ',', ' '); |
||
619 | return str_replace(' ', bab_nbsp(), $number . ' ' . $prefix . $unitSymbol); |
||
620 | } |
||
621 | |||
622 | |||
623 | /** |
||
624 | * Reformat a phone number in the specified format. |
||
625 | * |
||
626 | * @param string $phone The phone number to be formatted |
||
627 | * @param int $format The format the phone number should be formatted into |
||
628 | * |
||
629 | * @return string The formatted phone number |
||
630 | */ |
||
631 | public function phoneNumberFormat($phone, $format = null) |
||
632 | { |
||
633 | $PhoneNumber = bab_Functionality::get('PhoneNumber'); |
||
634 | if ($PhoneNumber === false) { |
||
635 | return $phone; |
||
636 | } |
||
637 | |||
638 | if (!isset($format)) { |
||
639 | $format = bab_registry::get('/' . $this->addonName . '/numberFormat', $PhoneNumber->getDefaultFormat()); |
||
640 | } |
||
641 | $phoneNumberUtil = $PhoneNumber->PhoneNumberUtil(); |
||
642 | |||
643 | try { |
||
644 | $phoneNumber = $phoneNumberUtil->parse($phone, 'FR'); |
||
645 | $phone = $phoneNumberUtil->format($phoneNumber, $format); |
||
646 | } catch (\libphonenumber\NumberParseException $e) { |
||
647 | } |
||
648 | |||
649 | return $phone; |
||
650 | } |
||
651 | |||
652 | |||
653 | |||
654 | |||
655 | /** |
||
656 | * Includes Controller class definition. |
||
657 | */ |
||
658 | public function includeController() |
||
659 | { |
||
660 | require_once APP_PHP_PATH . '/controller.class.php'; |
||
661 | } |
||
662 | |||
663 | |||
664 | /** |
||
665 | * Includes RecordController class definition. |
||
666 | */ |
||
667 | public function includeRecordController() |
||
668 | { |
||
669 | require_once APP_CTRL_PATH . '/record.ctrl.php'; |
||
670 | } |
||
671 | |||
672 | |||
673 | /** |
||
674 | * Instanciates the controller. |
||
675 | * |
||
676 | * @return app_Controller |
||
677 | */ |
||
678 | public function Controller() |
||
679 | { |
||
680 | $this->includeController(); |
||
681 | return bab_getInstance($this->classPrefix.'Controller')->setApp($this); |
||
682 | } |
||
683 | |||
684 | |||
685 | /** |
||
686 | * Instanciates a controller class. |
||
687 | * |
||
688 | * @return bab_Controller |
||
689 | */ |
||
690 | public function ControllerProxy($className, $proxy = true) |
||
691 | { |
||
692 | $this->includeController(); |
||
693 | |||
694 | if ($proxy) { |
||
695 | return app_Controller::getProxyInstance($this, $className); |
||
696 | } |
||
697 | |||
698 | return new $className($this); |
||
699 | } |
||
700 | |||
701 | |||
702 | |||
703 | /** |
||
704 | * Include class app_Ui |
||
705 | * |
||
706 | */ |
||
707 | public function includeUi() |
||
710 | } |
||
711 | |||
712 | |||
713 | /** |
||
714 | * The app_Ui object propose an access to all ui files and ui objects (widgets) |
||
715 | * |
||
716 | * @return app_Ui |
||
717 | */ |
||
718 | public function Ui() |
||
719 | { |
||
720 | $this->includeUi(); |
||
721 | return bab_getInstance($this->classPrefix . 'Ui');//->setApp($this); |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * Get upload path |
||
726 | * if the method return null, no upload functionality |
||
727 | * |
||
728 | * @return bab_Path |
||
729 | */ |
||
730 | public function getUploadPath() |
||
731 | { |
||
732 | require_once $GLOBALS['babInstallPath'].'utilit/path.class.php'; |
||
733 | return new bab_Path(bab_getAddonInfosInstance($this->getAddonName())->getUploadPath()); |
||
734 | } |
||
735 | |||
736 | |||
737 | /** |
||
738 | * |
||
739 | * @param string $name |
||
740 | * @param mixed $arguments |
||
741 | * @return mixed |
||
742 | */ |
||
743 | public function __call($name, $arguments) |
||
781 | } |
||
782 | } |
||
783 | |||
784 | |||
785 | /** |
||
786 | * Test if this App implementation handles the specified object class. |
||
787 | * |
||
788 | * The default is to consider that an object Xxxx implemented if the includeXxxxSet() method is |
||
789 | * declared public on this App. |
||
790 | * |
||
791 | * @param string $objectClassName App object name (eg. 'Contact' or 'CatalogItem') |
||
792 | * @return bool |
||
793 | */ |
||
794 | public function __isset($objectClassName) |
||
820 | } |
||
821 | } |
||
822 | |||
823 | if (app_App()) { |
||
824 | spl_autoload_register(array(app_App(), 'loadObject')); |
||
825 | } |
||
826 |