Passed
Pull Request — master ( #1 )
by Robin
03:25
created

Func_App::synchronizeComponentsSql()   C

Complexity

Conditions 11
Paths 161

Size

Total Lines 54
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
c 0
b 0
f 0
nc 161
nop 1
dl 0
loc 54
rs 6.8083

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//-------------------------------------------------------------------------
3
// OVIDENTIA http://www.ovidentia.org
4
// Ovidentia is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2, or (at your option)
7
// any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
// USA.
18
//-------------------------------------------------------------------------
19
/**
20
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
21
 * @copyright Copyright (c) 2018 by CANTICO ({@link http://www.cantico.fr})
22
 */
23
24
require_once dirname(__FILE__) . '/define.php';
25
require_once dirname(__FILE__) . '/functions.php';
26
require_once dirname(__FILE__) . '/component.class.php';
27
28
29
30
/**
31
 * Provides extensible functionalities to manage an application.
32
 *
33
 * @method app_CustomFieldSet       CustomFieldSet()
34
 * @method app_CustomSectionSet     CustomSectionSet()
35
 */
36
class Func_App extends bab_Functionality
37
{
38
    public $addonPrefix;
39
    public $classPrefix;
40
    public $addonName;
41
    public $controllerTg;
42
    public $phpPath;
43
    public $recordSetPath;
44
    public $ctrlPath;
45
    public $uiPath;
46
    
47
    private $components;
48
    private $currentComponent;
49
50
51
    /**
52
     * @var string
53
     */
54
    private $language = null;
55
56
57
    public function __construct()
58
    {
59
        $this->addonName = 'libapp';
60
        $this->addonPrefix = 'app';
61
62
        $this->classPrefix = $this->addonPrefix . '_';
63
        $this->controllerTg = 'addon/' . $this->addonName . '/main';
64
65
        $addon = bab_getAddonInfosInstance($this->addonName);
66
        $this->phpPath = $addon->getPhpPath();
67
        $this->recordSetPath = $this->phpPath;
68
        $this->ctrlPath = $this->phpPath;
69
        $this->uiPath = $this->phpPath . 'ui/';
70
71
        $babDB = bab_getDB();
72
73
        $LibOrm = bab_Functionality::get('LibOrm');
74
        /*@var $LibOrm Func_LibOrm */
75
76
        $LibOrm->initMysql();
77
        ORM_RecordSet::setBackend(new ORM_MySqlBackend($babDB));
78
    }
79
    
80
    public function createComponent($set, $ctrl, $ui)
81
    {
82
        return new app_Component($this, $set, $ctrl, $ui);
83
    }
84
    
85
    public function addComponent($componentName, app_Component $component)
86
    {
87
        $this->components[$componentName] = $component;
88
    }
89
    
90
    /**
91
     * @return app_Component
92
     */
93
    public function getComponentByName($componentName)
94
    {
95
        if(isset($this->components[$componentName])){
96
            return $this->components[$componentName];
97
        }
98
        return null;
99
    }
100
    
101
    public function setCurrentComponent(app_Component $component = null)
102
    {
103
        $this->currentComponent = $component;
104
        return $this;
105
    }
106
    
107
    /**
108
     * @return app_Component
109
     */
110
    public function getCurrentComponent()
111
    {
112
        return $this->currentComponent;
113
    }
114
    
115
    public function __get($componentName)
116
    {
117
        return $this->components[$componentName];
118
    }
119
120
    /**
121
     * @return app_Component[]
122
     */
123
    public function getComponents()
124
    {
125
        return $this->components;
126
    }
127
    
128
    public function checkComponentsDependencies()
129
    {
130
        $components = $this->getComponents();
131
        
132
        $messages = array();
133
        
134
        foreach ($components as $componentName => $component){
135
            $dependencies = $component->checkDependencies();
136
            foreach($dependencies['requiredComponents'] as $requiredComponent){
137
                $messages['requiredComponents'][$componentName][] = $requiredComponent;
138
            }
139
            foreach($dependencies['optionalComponents'] as $optionalComponent){
140
                $messages['optionalComponents'][$componentName][] = $optionalComponent;
141
            }
142
        }
143
        
144
        return $messages;
145
    }
146
147
    /**
148
     * @param string $class
149
     * @return boolean
150
     */
151
    public function loadObject($class)
152
    {
153
        $classPrefix = $this->classPrefix;
154
155
        if (substr($class, 0, strlen($classPrefix)) === $classPrefix) {
156
            if (substr($class, -3) === 'Set') {
157
                $pathname = $this->getRecordSetPathname($class);
158
                if (file_exists($pathname)) {
159
                    require $pathname;
160
                    return true;
161
                }
162
            } else {
163
                $pathname = $this->getRecordPathname($class);
164
                if (file_exists($pathname)) {
165
                    require $pathname;
166
                    return true;
167
                }
168
            }
169
        }
170
        return false;
171
    }
172
173
    /**
174
     * Returns the expected pathname for the file containing the definition of the record set class.
175
     *
176
     * @param string $class
177
     * @return string
178
     */
179
    public function getRecordSetPathname($class)
180
    {
181
        // $App->MyRecordSet() -> myrecord.class.php
182
        $file = strtolower(substr($class, strlen($this->classPrefix), -3)) . '.class.php';
183
        return $this->recordSetPath . $file;
184
    }
185
186
    /**
187
     *  Returns the expected pathname for the file containing the definition of the record class.
188
     *
189
     * @param string $class
190
     * @return string
191
     */
192
    public function getRecordPathname($class)
193
    {
194
        // $App->MyRecord() -> myrecord.class.php
195
        $file = strtolower(substr($class, strlen($this->classPrefix))) . '.class.php';
196
        return $this->recordSetPath . $file;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getUiPath()
203
    {
204
        return APP_UI_PATH;
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getSetPath()
211
    {
212
        return APP_SET_PATH;
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    public function getCtrlPath()
219
    {
220
        return APP_CTRL_PATH;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getPhpPath()
227
    {
228
        return APP_PHP_PATH;
229
    }
230
231
232
    /**
233
     * @return string
234
     *
235
     */
236
    public function getDescription()
237
    {
238
        return 'Application framework.';
239
    }
240
241
242
    /**
243
     * Get the addon name
244
     * @return string
245
     */
246
    public function getAddonName()
247
    {
248
        return $this->addonName;
249
    }
250
251
252
    /**
253
     * @return bab_addonInfos
254
     */
255
    public function getAddon()
256
    {
257
        return bab_getAddonInfosInstance($this->getAddonName());
258
    }
259
260
261
    /**
262
     * Register myself as a functionality.
263
     *
264
     */
265
    public static function register()
266
    {
267
        $addon = bab_getAddonInfosInstance('libapp');
268
269
        $addon->registerFunctionality('App', 'app.php');
270
    }
271
272
273
    /**
274
     * Synchronize sql tables for all classes found in Application object
275
     * using methods  'includeXxxxxClassName'
276
     * sychronize if the two correspond methods are met and the set classname match the prefix from parameter
277
     * do not synchronize if the set method has a VueSet suffix, in this case the table si juste a readonly vue
278
     *
279
     * @param	string	$prefix		tables and classes prefix
280
     * @return bab_synchronizeSql
281
     */
282
    public function synchronizeSql($prefix)
283
    {
284
        if (!$prefix) {
285
            return null;
286
        }
287
288
        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
289
        $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']);
290
        $sql = 'SET FOREIGN_KEY_CHECKS=0;
291
            ';
292
293
        foreach (get_class_methods($this) as $method) {
294
295
            if (substr($method, 0, 7) === 'include' && substr($method, -3) === 'Set') {
296
                $incl = $method;
297
                $classNameMethod = substr($method, strlen('include')) . 'ClassName';
298
299
300
                $classname = $this->$classNameMethod();
301
302
                if ($prefix === substr($classname, 0, strlen($prefix))
303
                  && 'Set' === substr($classname, -3)
304
                  && 'ViewSet' !== substr($classname, -7)) {
305
                    if (method_exists($this, $incl)) {
306
                        $this->$incl();
307
308
                        $call = substr($classname, strlen($prefix));
309
310
                        if (class_exists($classname)) {
311
312
                            /* @var $set ORM_RecordSet */
313
                            $set = $this->$call();
314
                            if (method_exists($set, 'useLang')) {
315
                                // This is necessary if the recordSet constructor uses a setLang().
316
                                // We need to revert to multilang fields before synchronizing.
317
                                $set->useLang(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $language of ORM_RecordSet::useLang(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

317
                                $set->useLang(/** @scrutinizer ignore-type */ false);
Loading history...
318
                            }
319
                            $sql .= $mysqlbackend->setToSql($set) . "\n";
320
                        }
321
                    }
322
                }
323
            }
324
        }
325
326
        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
327
        $synchronize = new bab_synchronizeSql();
328
329
        $synchronize->fromSqlString($sql);
330
331
        return $synchronize;
332
    }
333
    
334
    public function synchronizeComponentsSql($prefix)
335
    {
336
        if (!$prefix) {
337
            return null;
338
        }
339
        
340
        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
341
        $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']);
342
        $sql = 'SET FOREIGN_KEY_CHECKS=0;
343
            ';
344
345
        foreach ($this->components as $component){
346
            /* @var $component app_Component */
347
            try{
348
                /* @var $set ORM_RecordSet */
349
                $set = $component->recordSet();
350
                if (method_exists($set, 'useLang')) {
351
                    // This is necessary if the recordSet constructor uses a setLang().
352
                    // We need to revert to multilang fields before synchronizing.
353
                    $set->useLang(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $language of ORM_RecordSet::useLang(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

353
                    $set->useLang(/** @scrutinizer ignore-type */ false);
Loading history...
354
                }
355
                $sql .= $mysqlbackend->setToSql($set) . "\n";
356
                
357
                $component->onUpdate();
358
            }
359
            catch (Exception $e){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
360
                
361
            }
362
        }
363
        
364
        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
365
        $synchronize = new bab_synchronizeSql();
366
        
367
        $synchronize->fromSqlString($sql);
368
        
369
        $dependencies = $this->checkComponentsDependencies();
370
        foreach ($dependencies['requiredComponents'] as $componentName => $requiredComponents){
371
            foreach ($requiredComponents as $requiredComponent){
372
                $hasComponent = $this->getComponentByName($requiredComponent);
373
                if(!$hasComponent){
374
                    bab_installWindow::message(sprintf($this->translate('The component %s is required for the component %s to work. You will encounter fatal errors'), $requiredComponent, $componentName));
375
                }
376
            }
377
        }
378
        foreach ($dependencies['optionalComponents'] as $componentName => $optionalComponents){
379
            foreach ($optionalComponents as $optionalComponent){
380
                $hasComponent = $this->getComponentByName($optionalComponent);
381
                if(!$hasComponent){
382
                    bab_installWindow::message(sprintf($this->translate('The optional component %s has not been added. The component %s may have reduced functionalities'), $optionalComponent, $componentName));
383
                }
384
            }
385
        }
386
        
387
        return $synchronize;
388
    }
389
390
391
392
    public function getSynchronizeSql($prefix)
393
    {
394
        if (!$prefix) {
395
            return null;
396
        }
397
398
        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
399
        $mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']);
400
        $sql = '';
401
402
        foreach (get_class_methods($this) as $method) {
403
404
            if (substr($method, 0, strlen('include')) === 'include' && substr($method, -strlen('Set')) === 'Set') {
405
                $incl = $method;
406
                $classNameMethod = substr($method, strlen('include')) . 'ClassName';
407
408
                $classname = $this->$classNameMethod();
409
410
                if ($prefix === substr($classname, 0, strlen($prefix))
411
                    && 'Set' === substr($classname, -3)
412
                    && 'ViewSet' !== substr($classname, -6)
413
                    ) {
414
                        if (method_exists($this, $incl)) {
415
                            $this->$incl();
416
                            $call = substr($classname, strlen($prefix));
417
418
                            if (class_exists($classname) && method_exists($this, $call)) {
419
                                $set = $this->$call();
420
                                $sql .= $mysqlbackend->setToSql($set) . "\n";
421
                            }
422
                        }
423
                    }
424
            }
425
        }
426
427
        return $sql;
428
    }
429
430
431
432
    public function includeBase()
433
    {
434
        require_once APP_PHP_PATH . 'base.class.php';
435
    }
436
437
    /**
438
     * Includes app_RecordSet class definition.
439
     */
440
    public function includeRecordSet()
441
    {
442
        require_once APP_SET_PATH . 'record.class.php';
443
    }
444
445
446
    /**
447
     * Includes app_TraceableRecordSet class definition.
448
     */
449
    public function includeTraceableRecordSet()
450
    {
451
        require_once APP_SET_PATH . 'traceablerecord.class.php';
452
    }
453
454
455
    /**
456
     *
457
     * @return app_AccessManager
458
     */
459
    public function AccessManager()
460
    {
461
        static $accessManager = null;
462
        if (!isset($accessManager)) {
463
            require_once APP_SET_PATH . 'accessmanager.class.php';
464
            $accessManager = new app_AccessManager($this);
465
        }
466
467
        return $accessManager;
468
    }
469
470
    /**
471
     * SET DEFINITION
472
     */
473
474
    //Log
475
476
    /**
477
     * Includes LogSet class definition.
478
     */
479
    public function includeLogSet()
480
    {
481
        require_once APP_SET_PATH . 'log.class.php';
482
    }
483
484
    /**
485
     * @return string
486
     */
487
    public function LogClassName()
488
    {
489
        return 'app_Log';
490
    }
491
492
    /**
493
     * @return string
494
     */
495
    public function LogSetClassName()
496
    {
497
        return $this->LogClassName() . 'Set';
498
    }
499
500
    /**
501
     * @return app_LogSet
502
     */
503
    public function LogSet()
504
    {
505
        $this->includeLogSet();
506
        $className = $this->LogSetClassName();
507
        $set = new $className($this);
508
        return $set;
509
    }
510
511
512
    //Link
513
514
    /**
515
     * Includes LinkSet class definition.
516
     */
517
    public function includeLinkSet()
518
    {
519
        require_once APP_SET_PATH . 'link.class.php';
520
    }
521
522
    /**
523
     * @return string
524
     */
525
    public function LinkClassName()
526
    {
527
        return 'app_Link';
528
    }
529
530
    /**
531
     * @return string
532
     */
533
    public function LinkSetClassName()
534
    {
535
        return $this->LinkClassName() . 'Set';
536
    }
537
538
    /**
539
     * @return app_LinkSet
540
     */
541
    public function LinkSet()
542
    {
543
        $this->includeLinkSet();
544
        $className = $this->LinkSetClassName();
545
        $set = new $className($this);
546
        return $set;
547
    }
548
549
550
    /**
551
     *
552
     * @param string $recordClassname
553
     *
554
     * @return ORM_RecordSet
555
     */
556
    public function getRecordSetByRef($recordClassname)
557
    {
558
        $component = $this->getComponentByName(str_replace($this->classPrefix, '', $recordClassname));
559
        if(isset($component)){
560
            return $component->recordSet();
561
        }
562
        $classSet = $recordClassname . 'Set';
563
        $set = $this->$classSet();
564
        return $set;
565
    }
566
567
    /**
568
     * Returns the app_Record corresponding to the specified
569
     * reference $ref.
570
     *
571
     * @param string 	$ref	A reference string (e.g. Contact:12)
572
     * @return app_Record	or null if no corresponding record is found.
573
     */
574
    public function getRecordByRef($ref)
575
    {
576
        $refParts = explode(':', $ref);
577
        if (count($refParts) !== 2) {
578
            return null;
579
        }
580
        list($recordClassname, $id) = $refParts;
581
        $set = $this->getRecordSetByRef($recordClassname);
582
        if (isset($set)) {
583
            return $set->get($id);
584
        }
585
        return null;
586
    }
587
588
    /**
589
     * Returns the reference corresponding to the specified
590
     * app_Record $record (e.g. Contact:12 or Deal:125)
591
     *
592
     * @param app_Record	$record
593
     * @return string
594
     */
595
    public function getRecordRef(app_Record $record)
596
    {
597
        $fullClassName = get_class($record);
598
        list(, $className) = explode('_', $fullClassName);
599
        return $className . ':' . $record->id;
600
    }
601
602
603
    /**
604
     *
605
     * @return Func_Translate_Gettext
0 ignored issues
show
Bug introduced by
The type Func_Translate_Gettext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
606
     */
607
    private static function getTranslator()
608
    {
609
        static $translator = null;
610
        if (!isset($translator)) {
611
            $translator = bab_functionality::get('Translate/Gettext', false);
612
            $translator->setAddonName('libapp');
0 ignored issues
show
Bug introduced by
The method setAddonName() does not exist on bab_functionality. It seems like you code against a sub-type of bab_functionality such as Func_App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

612
            $translator->/** @scrutinizer ignore-call */ 
613
                         setAddonName('libapp');
Loading history...
613
        }
614
615
        return $translator;
616
    }
617
618
619
    /**
620
     * Specifies the language to use for translation.
621
     *
622
     * If $language is null, the language is reset to
623
     * the current logged user language.
624
     *
625
     * @param string|null $language
626
     */
627
    public function setTranslateLanguage($language)
628
    {
629
        $this->language = $language;
630
    }
631
632
633
634
    /**
635
     * Translates the string.
636
     *
637
     * @param string $str           Text to translate or singular form
638
     * @param string $str_plurals   Plurals form string
639
     * @param int $number           Number of items for plurals
640
     * @return string
641
     */
642
    public function translate($str, $str_plurals = null, $number = null)
643
    {
644
        $translator = self::getTranslator();
645
        $translation = $translator->translate($str, $str_plurals, $number);
646
647
        return $translation;
648
    }
649
650
    /**
651
     * @param string    $str
652
     * @param string    $str_plurals
653
     * @param int       $number
654
     * @return string
655
     */
656
    public function translatable($str, $str_plurals = null, $number = null)
657
    {
658
        return $str;
659
    }
660
661
    /**
662
     * Translates all the string in an array and returns a new array.
663
     *
664
     * @param array $arr
665
     * @return array
666
     */
667
    public function translateArray($arr)
668
    {
669
        $newarr = $arr;
670
671
        foreach ($newarr as &$str) {
672
            $str = $this->translate($str);
673
        }
674
        return $newarr;
675
    }
676
677
678
679
    /**
680
     * Returns a link for writting an email to the specified email address.
681
     *
682
     * @param string $addr
683
     * @param string $subject
684
     * @param string $body
685
     *
686
     * @return string
687
     */
688
    public function mailTo($addr, $subject = null, $body = null)
689
    {
690
        $mailTo = 'mailto:' . $addr;
691
        $parameters = array();
692
        if (isset($subject)) {
693
            $parameters[] = 'subject=' . $subject;
694
        }
695
        if (isset($body)) {
696
            $parameters[] = 'body=' . $body;
697
        }
698
        if (!empty($parameters)) {
699
            $mailTo .= '?' . implode('&', $parameters);
700
        }
701
702
        return $mailTo;
703
    }
704
705
706
707
    /**
708
     * Format a number for display
709
     *
710
     * @param   float|string|null   $number     Numeric value with decimal
711
     * @return string
712
     */
713
    public function numberFormat($number, $decimals = 2)
714
    {
715
        if (is_null($number)) {
716
            return '#,##';
717
        }
718
719
        $number = number_format(floatval($number), $decimals, ',', ' ');
720
        return str_replace(' ', bab_nbsp(), $number);
721
    }
722
723
724
    /**
725
     * Format a number with an optional unit.
726
     *
727
     * If the value is >= 1000 the value is shortened and the corresponding prexif (k or M) is used.
728
     *
729
     * @param float|string|null $number
730
     * @param string $unitSymbol    (For example $, m2, Wh)
731
     * @param int $decimals
732
     * @return string|mixed
733
     */
734
    public function shortFormatWithUnit($number, $unitSymbol = '', $decimals = 2)
735
    {
736
        if (is_null($number)) {
737
            return '#,##';
738
        }
739
740
        $prefix = '';
741
        if ($number >= 1000000) {
742
            $number /= 1000000;
743
            $prefix = 'M';
744
        } elseif ($number >= 1000) {
745
            $number /= 1000;
746
            $prefix = 'k';
747
        }
748
749
        $number = number_format($number, $decimals, ',', ' ');
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $number of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

749
        $number = number_format(/** @scrutinizer ignore-type */ $number, $decimals, ',', ' ');
Loading history...
750
        return str_replace(' ', bab_nbsp(), $number . ' ' . $prefix . $unitSymbol);
751
    }
752
753
754
    /**
755
     * Reformat a phone number in the specified format.
756
     *
757
     * @param string    $phone      The phone number to be formatted
758
     * @param int       $format     The format the phone number should be formatted into
759
     *
760
     * @return string               The formatted phone number
761
     */
762
    public function phoneNumberFormat($phone, $format = null)
763
    {
764
        $PhoneNumber = bab_Functionality::get('PhoneNumber');
765
        if ($PhoneNumber === false) {
0 ignored issues
show
introduced by
The condition $PhoneNumber === false is always false.
Loading history...
766
            return $phone;
767
        }
768
769
        if (!isset($format)) {
770
            $format = bab_registry::get('/' . $this->addonName . '/numberFormat', $PhoneNumber->getDefaultFormat());
0 ignored issues
show
Bug introduced by
The method getDefaultFormat() does not exist on bab_functionality. It seems like you code against a sub-type of bab_functionality such as Func_App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

770
            $format = bab_registry::get('/' . $this->addonName . '/numberFormat', $PhoneNumber->/** @scrutinizer ignore-call */ getDefaultFormat());
Loading history...
771
        }
772
        $phoneNumberUtil = $PhoneNumber->PhoneNumberUtil();
0 ignored issues
show
Bug introduced by
The method PhoneNumberUtil() does not exist on bab_functionality. It seems like you code against a sub-type of bab_functionality such as Func_App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

772
        /** @scrutinizer ignore-call */ 
773
        $phoneNumberUtil = $PhoneNumber->PhoneNumberUtil();
Loading history...
773
774
        try {
775
            $phoneNumber = $phoneNumberUtil->parse($phone, 'FR');
776
            $phone = $phoneNumberUtil->format($phoneNumber, $format);
777
        } catch (\libphonenumber\NumberParseException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The type libphonenumber\NumberParseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
778
        }
779
780
        return $phone;
781
    }
782
783
784
785
786
    /**
787
     * Includes Controller class definition.
788
     */
789
    public function includeController()
790
    {
791
        require_once APP_PHP_PATH . '/controller.class.php';
792
    }
793
794
795
    /**
796
     * Includes RecordController class definition.
797
     */
798
    public function includeRecordController()
799
    {
800
        require_once APP_CTRL_PATH . '/record.ctrl.php';
801
    }
802
803
804
    /**
805
     * Instanciates the controller.
806
     *
807
     * @return app_Controller
808
     */
809
    public function Controller()
810
    {
811
        $this->includeController();
812
        return bab_getInstance($this->classPrefix.'Controller')->setApp($this);
813
    }
814
815
816
    /**
817
     * Instanciates a controller class.
818
     *
819
     * @return bab_Controller
820
     */
821
    public function ControllerProxy($className, $proxy = true)
822
    {
823
        $this->includeController();
824
825
        if ($proxy) {
826
            return app_Controller::getProxyInstance($this, $className);
827
        }
828
829
        return new $className($this);
830
    }
831
832
833
834
    /**
835
     * Include class app_Ui
836
     *
837
     */
838
    public function includeUi()
839
    {
840
        require_once APP_UI_PATH . 'ui.class.php';
841
    }
842
843
844
    /**
845
     * The app_Ui object propose an access to all ui files and ui objects (widgets)
846
     *
847
     * @return app_Ui
848
     */
849
    public function Ui()
850
    {
851
        $this->includeUi();
852
        return bab_getInstance($this->classPrefix . 'Ui');//->setApp($this);
853
    }
854
855
    /**
856
     * Get upload path
857
     * if the method return null, no upload functionality
858
     *
859
     * @return bab_Path
860
     */
861
    public function getUploadPath()
862
    {
863
        require_once $GLOBALS['babInstallPath'].'utilit/path.class.php';
864
        return new bab_Path(bab_getAddonInfosInstance($this->getAddonName())->getUploadPath());
865
    }
866
867
868
    /**
869
     *
870
     * @param string $name
871
     * @param mixed $arguments
872
     * @return mixed
873
     */
874
    public function __call($name, $arguments)
875
    {
876
        switch (true) {
877
878
            case substr($name, -strlen('SetClassName')) === 'SetClassName':
879
                $className = substr($name, 0, strlen($name) - strlen('SetClassName'));
880
                if($component = $this->getComponentByName(ucfirst($className))){
881
                    return $component->getSetClassName();
882
                }
883
                $setName = $className.'Set';
884
                return $this->classPrefix . $setName;
885
886
            case substr($name, -strlen('ClassName')) === 'ClassName':
887
                $recordName = substr($name, 0, strlen($name) - strlen('ClassName'));
888
                if($component = $this->getComponentByName(ucfirst($className))){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $className seems to be never defined.
Loading history...
889
                    return $component->getRecordClassName();
890
                }
891
                return $this->classPrefix . $recordName;
892
893
            case substr($name, 0, strlen('include')) === 'include' && substr($name, -strlen('Set')) === 'Set':
894
                $fileNameBase = strtolower(substr(substr($name, 0, strlen($name) - strlen('Set')), strlen('include')));
895
                if($component = $this->getComponentByName(ucfirst($fileNameBase))){
0 ignored issues
show
Unused Code introduced by
The assignment to $component is dead and can be removed.
Loading history...
896
                    return;
897
                }
898
                require_once APP_SET_PATH . $fileNameBase . '.class.php';
899
                return;
900
901
            case substr($name, -strlen('Set')) === 'Set':
902
                if($component = $this->getComponentByName(ucfirst(substr($name, 0, strlen($name) -strlen('Set'))))){
903
                    return $component->recordSet();
904
                }
905
                $includeMethod = 'include' . $name;
906
                $this->$includeMethod();
907
                $setClassNameMethod = $name . 'ClassName';
908
                $className = $this->$setClassNameMethod();
909
                $set = new $className($this);
910
                return $set;
911
            
912
            case ($component = $this->getComponentByName(ucfirst($name))) != false:
913
                return $component;
914
            default:
915
                $setName = $name . 'Set';
916
                $recordClassNameMethod = $name . 'ClassName';
917
                $recordClassName = $this->$recordClassNameMethod();
918
                if (isset($arguments[0])) {
919
                    if ($arguments[0] instanceof $recordClassName) {
920
                        return $arguments[0];
921
                    }
922
                    $set = $this->$setName();
923
                    return $set->get($arguments[0]);
924
                }
925
                return null;
926
        }
927
    }
928
929
930
    /**
931
     * Test if this App implementation handles the specified object class.
932
     *
933
     * The default is to consider that an object Xxxx implemented if the includeXxxxSet() method is
934
     * declared public on this App.
935
     *
936
     * @param	string	$objectClassName		App object name (eg. 'Contact' or 'CatalogItem')
937
     * @return bool
938
     */
939
    public function __isset($objectClassName)
940
    {
941
        if (null === $this->implementedObjects) {
942
943
            $this->implementedObjects = array();
0 ignored issues
show
Bug Best Practice introduced by
The property implementedObjects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
944
945
            $className = get_class($this);
946
            $rClass = new ReflectionClass($className);
947
948
            foreach ($rClass->getMethods(ReflectionMethod::IS_PUBLIC) as $m) {
949
950
                // We consider object Xxxx implemented if the includeXxxxSet() method is
951
                // declared public on this.
952
953
                if ($m->getDeclaringClass()->name !== $className) {
954
                    // The method is declared on an ancestor class.
955
                    continue;
956
                }
957
958
                if (substr($m->name, 0, 7) === 'include' && substr($m->name, -3) === 'Set') {
959
                    $this->implementedObjects[substr($m->name, 7, -3)] = 1;
960
                }
961
            }
962
        }
963
964
        return isset($this->implementedObjects[$objectClassName]);
965
    }
966
    
967
    /**
968
     * Returns the current user id.
969
     *
970
     * @return string		The current user id	or '' if the user is not logged in.
971
     */
972
    public function getCurrentUser()
973
    {
974
        return bab_getUserId();
975
    }
976
}
977
978
if (app_App()) {
979
    spl_autoload_register(array(app_App(), 'loadObject'));
980
}
981