Test Setup Failed
Push — master ( 9946a0...d0d1d4 )
by Php Easy Api
04:56
created

Utils::trace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use Resta\Support\YamlManager;
6
use Resta\Container\ContainerResolve;
7
8
class Utils
9
{
10
    /**
11
     * @var array $bool
12
     */
13
    private static $bool=[];
14
15
    /**
16
     * @var array
17
     */
18
    private static $recursiveArray= [];
0 ignored issues
show
introduced by
The private property $recursiveArray is not used, and could be removed.
Loading history...
19
20
    /**
21
     * @return \DI\Container
22
     */
23
    public static function callBuild()
24
    {
25
        //di-container
26
        return \DI\ContainerBuilder::buildDevContainer();
27
    }
28
29
30
    /**
31
     * @param null $class
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $class is correct as it would always require null to be passed?
Loading history...
32
     * @return mixed
33
     * @throws \DI\DependencyException
34
     * @throws \DI\NotFoundException
35
     */
36
    public static function resolve($class=null)
37
    {
38
        //class resolve
39
        if($class!==null){
0 ignored issues
show
introduced by
The condition $class !== null is always false.
Loading history...
40
            $container = self::callBuild();
41
            return $container->get($class);
42
        }
43
    }
44
45
    /**
46
     * @param $argument
47
     * @param bool $shift
48
     * @return array
49
     */
50
    public static function upperCase($argument,$shift=true)
51
    {
52
        if($shift){
53
            array_shift($argument);
54
        }
55
56
        return array_map(function($argument){
57
            return ucfirst($argument);
58
        },$argument);
59
    }
60
61
    /**
62
     * encrypt array data
63
     *
64
     * @param $class
65
     * @return string
66
     */
67
    public static function encryptArrayData($class)
68
    {
69
        //the serialized class data
70
        return md5(serialize($class));
71
    }
72
73
74
    /**
75
     * @param $argument
76
     * @return array|string
77
     */
78
    public static function strtolower($argument)
79
    {
80
        if(!is_array($argument)){
81
            return strtolower($argument);
82
        }
83
        return array_map(function($argument){
84
            return strtolower($argument);
85
        },$argument);
86
    }
87
88
    /**
89
     * @param array $data
90
     * @return string
91
     */
92
    public static function generatorNamespace($data=array())
93
    {
94
        return str_replace('.php','',implode("\\",$data));
95
    }
96
97
    /**
98
     * @param $class
99
     * @param bool $extension
100
     * @return mixed
101
     */
102
    public static function getPathFromNamespace($class,$extension=true)
103
    {
104
        if($extension){
105
            $default=root.'/'.str_replace("\\","/",$class).'.php';
106
        }
107
        else{
108
            $default=root.'/'.str_replace("\\","/",$class).'';
109
        }
110
111
        return str_replace("/App",'/src/app',$default);
112
    }
113
114
    /**
115
     * @param $namespace
116
     * @return bool
117
     */
118
    public static function isNamespaceExists($namespace)
119
    {
120
        return (is_string($namespace) && class_exists($namespace)) ? true : false;
121
    }
122
123
    /**
124
     * @param $class
125
     * @param $method
126
     * @return bool
127
     */
128
    public static function existMethod($class,$method)
129
    {
130
        return method_exists($class,$method);
131
    }
132
133
    /**
134
     * @param $first
135
     * @param $second
136
     * @return bool
137
     */
138
    public static function isArrayEqual($first,$second)
139
    {
140
        return (count( $first ) == count( $second ) && !array_diff( $first, $second ));
141
    }
142
143
    /**
144
     * @param $path
145
     * @return YamlManager
146
     */
147
    public static function yaml($path)
148
    {
149
        return new YamlManager($path);
150
    }
151
152
    /**
153
     * @param $path
154
     * @param bool $filename
155
     * @return array
156
     */
157
    public static function glob($path,$filename=false)
158
    {
159
        $configList = [];
160
161
        foreach (glob($path.'/*.php') as $config) {
162
163
            $configArray=str_replace(".php","",explode("/",$config));
164
            $configList[end($configArray)]=$config;
165
        }
166
167
        if($filename===true){
168
            return array_keys($configList);
169
        }
170
171
        return $configList;
172
    }
173
174
    /**
175
     * @param $path
176
     */
177
    public static function makeWritable($path)
178
    {
179
        $dir = new \DirectoryIterator($path);
180
181
        foreach ($dir as $item) {
182
183
            chmod($item->getPathname(), 0777);
184
185
            if ($item->isDir() && !$item->isDot()) {
186
                self::makeWritable($item->getPathname());
187
            }
188
        }
189
    }
190
191
    /**
192
     * @param $namespace
193
     * @param string $seperator
194
     * @return mixed
195
     */
196
    public static function getJustClassName($namespace,$seperator="\\")
197
    {
198
        $path = explode($seperator, $namespace);
199
        return array_pop($path);
200
    }
201
202
    /**
203
     * @param $class
204
     * @param array $param
205
     * @return bool
206
     */
207
    public static function changeClass($class,$param=array())
208
    {
209
        $executionPath=$class;
210
        $dt = fopen($executionPath, "r");
211
        $content = fread($dt, filesize($executionPath));
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fread() does only seem to accept resource, 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

211
        $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($executionPath));
Loading history...
212
        fclose($dt);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

212
        fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
213
214
        foreach ($param as $key=>$value){
215
            $content=str_replace($key,$value,$content);
216
        }
217
218
        $dt = fopen($executionPath, "w");
219
        fwrite($dt, $content);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

219
        fwrite(/** @scrutinizer ignore-type */ $dt, $content);
Loading history...
220
        fclose($dt);
221
222
        return true;
223
    }
224
225
    /**
226
     * @param $data
227
     * @param $callback
228
     * @return mixed
229
     */
230
    public static function returnCallback($data,$callback)
231
    {
232
        return call_user_func_array($callback,[$data]);
233
    }
234
235
    /**
236
     * @param $namespace
237
     * @return string
238
     */
239
    public static function getNamespace($namespace)
240
    {
241
        $rootDelete=str_replace(root.''.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'','',$namespace);
242
243
        return 'App\\'.self::generatorNamespace(
244
                explode(''.DIRECTORY_SEPARATOR.'',$rootDelete)
245
            );
246
247
    }
248
249
    /**
250
     * @param $callback
251
     * @return mixed
252
     */
253
    public static function callbackProcess($callback)
254
    {
255
        return (is_callable($callback)) ? call_user_func_array($callback,[app()]) : $callback;
256
    }
257
258
    /**
259
     * @param $array1
260
     * @param $array2
261
     * @return bool
262
     */
263
    public static function array_diff_key_recursive ($array1, $array2)
264
    {
265
        if(count($array1)!==count($array2)) self::$bool[]=false;
266
267
        foreach ($array1 as $array1_key=>$array1_value){
268
269
            if(!is_array($array1_value)){
270
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
271
            }
272
            else{
273
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
274
275
                if(!isset($array2[$array1_key]) OR !is_array($array2[$array1_key])) $array2[$array1_key]=[];
276
277
                if(isset($array1_value[0])) $array1_value=$array1_value[0];
278
279
                if(isset($array2[$array1_key][0])) $array2[$array1_key]=$array2[$array1_key][0];
280
281
                self::array_diff_key_recursive($array1_value,$array2[$array1_key]);
282
            }
283
        }
284
285
        if(in_array(false,self::$bool)){
286
            return false;
287
        }
288
        return true;
289
    }
290
291
    /**
292
     * @param $data
293
     * @return mixed
294
     */
295
    public static function slashToBackSlash($data)
296
    {
297
        return str_replace("/","\\",$data);
298
    }
299
300
    /**
301
     * @param int $debug
302
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
303
     * @return null
304
     */
305
    public static function trace($debug=0,$key=null)
306
    {
307
        $trace=debug_backtrace();
308
309
        if($key===null){
0 ignored issues
show
introduced by
The condition $key === null is always true.
Loading history...
310
            return $trace[$debug] ?? null;
311
        }
312
        return $trace[$debug][$key] ?? null;
313
314
    }
315
316
    /**
317
     * @param $dir
318
     * @param $dirPermissions
319
     * @param $filePermissions
320
     */
321
    public  static function chmod_r($dir, $dirPermissions, $filePermissions)
322
    {
323
        $dp = opendir($dir);
324
        while($file = readdir($dp)) {
0 ignored issues
show
Bug introduced by
It seems like $dp can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

324
        while($file = readdir(/** @scrutinizer ignore-type */ $dp)) {
Loading history...
325
            if (($file == ".") || ($file == ".."))
326
                continue;
327
328
            $fullPath = $dir."/".$file;
329
330
            if(is_dir($fullPath)) {
331
                chmod($fullPath, $dirPermissions);
332
                self::chmod_r($fullPath, $dirPermissions, $filePermissions);
333
            } else {
334
                chmod($fullPath, $filePermissions);
335
            }
336
337
        }
338
        closedir($dp);
0 ignored issues
show
Bug introduced by
It seems like $dp can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

338
        closedir(/** @scrutinizer ignore-type */ $dp);
Loading history...
339
    }
340
341
    /**
342
     * @return array
343
     */
344
    public static function getServiceConf()
345
    {
346
        if(property_exists(core(),'serviceConf') && defined('methodName')){
347
            return core()->serviceConf;
348
        }
349
        return [];
350
    }
351
352
353
    /**
354
     * @param $dir
355
     * @param bool $recursive
356
     * @param string $basedir
357
     * @param bool $include_dirs
358
     * @return array
359
     */
360
    public static function getAllFilesInDirectory($dir, $recursive = true, $basedir = '', $include_dirs = false)
361
    {
362
        if ($dir == '') {return array();} else {$results = array(); $subresults = array();}
363
        if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent
364
        if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;}
365
366
        $files = scandir($dir);
367
        foreach ($files as $key => $value){
368
            if ( ($value != '.') && ($value != '..') ) {
369
                $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
370
                if (is_dir($path)) {
371
                    // optionally include directories in file list
372
                    if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);}
373
                    // optionally get file list for all subdirectories
374
                    if ($recursive) {
375
                        $subdirresults = self::getAllFilesInDirectory($path, $recursive, $basedir, $include_dirs);
376
                        $results = array_merge($results, $subdirresults);
377
                    }
378
                } else {
379
                    // strip basedir and add to subarray to separate file list
380
                    $subresults[] = str_replace($basedir, '', $path);
381
                }
382
            }
383
        }
384
        // merge the subarray to give the list of files then subdirectory files
385
        if (count($subresults) > 0) {$results = array_merge($subresults, $results);}
386
        return $results;
387
    }
388
389
    /**
390
     * @param $files
391
     * @param null $reelPath
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $reelPath is correct as it would always require null to be passed?
Loading history...
392
     * @return array
393
     */
394
    public static function getPathWithPhpExtension($files,$reelPath=null)
395
    {
396
        $pathWithPhpList = [];
397
398
        foreach ($files as $file){
399
400
            if(preg_match('@(.*).php@is',$file,$pathWithPhp)){
401
402
                if($reelPath===null){
403
                    $pathWithPhpList[] = $pathWithPhp[0];
404
                }
405
                else{
406
                    $pathWithPhpList[] = $reelPath.'/'.$pathWithPhp[0];
407
                }
408
409
            }
410
        }
411
412
        return $pathWithPhpList;
413
    }
414
415
    /**
416
     * @return array
417
     */
418
    public static function getHttpMethods()
419
    {
420
        return [
421
            'get',
422
            'head',
423
            'post',
424
            'put',
425
            'delete',
426
            'connect',
427
            'options',
428
            'trace'
429
        ];
430
    }
431
432
    /**
433
     * @param $class
434
     * @return mixed
435
     */
436
    public static function resolverClass($class)
437
    {
438
        if(self::isNamespaceExists($class)){
439
            return app()->resolve($class);
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on Resta\Contracts\ApplicationHelpersContracts. ( Ignorable by Annotation )

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

439
            return app()->/** @scrutinizer ignore-call */ resolve($class);

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.

Loading history...
440
        }
441
442
        return $class;
443
    }
444
445
    /**
446
     * @return array
447
     */
448
    public static function getRequestPathInfo()
449
    {
450
        if(is_null(BootStaticManager::getRequestPath())){
0 ignored issues
show
introduced by
The condition is_null(Resta\Support\Bo...ager::getRequestPath()) is always false.
Loading history...
451
            return explode("/",request()->getPathInfo());
452
        }
453
        return BootStaticManager::getRequestPath();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Resta\Support\Boo...nager::getRequestPath() returns the type string which is incompatible with the documented return type array.
Loading history...
454
    }
455
456
    /**
457
     * @param $trace
458
     * @param null $remove
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $remove is correct as it would always require null to be passed?
Loading history...
459
     * @return array
460
     */
461
    public static function removeTrace($trace,$remove=null)
462
    {
463
        $list = [];
464
465
        foreach($trace as $key=>$item){
466
467
            if(isset($item['file']) && !preg_match('@'.$remove.'@',$item['file'])){
468
                $list[$key] = $item;
469
            }
470
        }
471
472
        return array_values($list);
473
    }
474
}