Test Setup Failed
Push — master ( 47635f...8c14aa )
by Php Easy Api
03:03
created

Utils::callBuild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
class Utils
6
{
7
    /**
8
     * @var array $bool
9
     */
10
    private static $bool = [];
11
12
    /**
13
     * string upper case
14
     *
15
     * @param $argument
16
     * @param bool $shift
17
     * @return array
18
     */
19
    public static function upperCase($argument,$shift=true)
20
    {
21
        if($shift){
22
            array_shift($argument);
23
        }
24
25
        return array_map(function($argument){
26
            return ucfirst($argument);
27
        },$argument);
28
    }
29
30
    /**
31
     * encrypt array data
32
     *
33
     * @param $class
34
     * @return string
35
     */
36
    public static function encryptArrayData($class)
37
    {
38
        //the serialized class data
39
        return md5(serialize($class));
40
    }
41
42
43
    /**
44
     * @param $argument
45
     * @return array|string
46
     */
47
    public static function strtolower($argument)
48
    {
49
        if(!is_array($argument)){
50
            return strtolower($argument);
51
        }
52
        return array_map(function($argument){
53
            return strtolower($argument);
54
        },$argument);
55
    }
56
57
    /**
58
     * @param array $data
59
     * @return string
60
     */
61
    public static function generatorNamespace($data=array())
62
    {
63
        return str_replace('.php','',implode("\\",$data));
64
    }
65
66
    /**
67
     * @param $class
68
     * @param bool $extension
69
     * @return mixed
70
     */
71
    public static function getPathFromNamespace($class,$extension=true)
72
    {
73
        if($extension){
74
            $default=root.'/'.str_replace("\\","/",$class).'.php';
75
        }
76
        else{
77
            $default=root.'/'.str_replace("\\","/",$class).'';
78
        }
79
80
        return str_replace("/App",'/src/app',$default);
81
    }
82
83
    /**
84
     * @param $namespace
85
     * @return bool
86
     */
87
    public static function isNamespaceExists($namespace)
88
    {
89
        return (is_string($namespace) && class_exists($namespace)) ? true : false;
90
    }
91
92
    /**
93
     * @param $class
94
     * @param $method
95
     * @return bool
96
     */
97
    public static function existMethod($class,$method)
98
    {
99
        return method_exists($class,$method);
100
    }
101
102
    /**
103
     * @param $first
104
     * @param $second
105
     * @return bool
106
     */
107
    public static function isArrayEqual($first,$second)
108
    {
109
        return (count( $first ) == count( $second ) && !array_diff( $first, $second ));
110
    }
111
112
    /**
113
     * @param $path
114
     * @return YamlManager
115
     */
116
    public static function yaml($path)
117
    {
118
        return new YamlManager($path);
119
    }
120
121
    /**
122
     * @param $path
123
     * @param bool $filename
124
     * @return array
125
     */
126
    public static function glob($path,$filename=false)
127
    {
128
        $configList = [];
129
130
        foreach (glob($path.'/*.php') as $config) {
131
132
            $configArray=str_replace(".php","",explode("/",$config));
133
            $configList[end($configArray)]=$config;
134
        }
135
136
        if($filename===true){
137
            return array_keys($configList);
138
        }
139
140
        return $configList;
141
    }
142
143
    /**
144
     * @param $path
145
     */
146
    public static function makeWritable($path)
147
    {
148
        $dir = new \DirectoryIterator($path);
149
150
        foreach ($dir as $item) {
151
152
            chmod($item->getPathname(), 0777);
153
154
            if ($item->isDir() && !$item->isDot()) {
155
                self::makeWritable($item->getPathname());
156
            }
157
        }
158
    }
159
160
    /**
161
     * @param $namespace
162
     * @param string $seperator
163
     * @return mixed
164
     */
165
    public static function getJustClassName($namespace,$seperator="\\")
166
    {
167
        $path = explode($seperator, $namespace);
168
        return array_pop($path);
169
    }
170
171
    /**
172
     * @param $class
173
     * @param array $param
174
     * @return bool
175
     */
176
    public static function changeClass($class,$param=array())
177
    {
178
        $executionPath=$class;
179
        $dt = fopen($executionPath, "r");
180
        $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

180
        $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($executionPath));
Loading history...
181
        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

181
        fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
182
183
        foreach ($param as $key=>$value){
184
            $content=str_replace($key,$value,$content);
185
        }
186
187
        $dt = fopen($executionPath, "w");
188
        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

188
        fwrite(/** @scrutinizer ignore-type */ $dt, $content);
Loading history...
189
        fclose($dt);
190
191
        return true;
192
    }
193
194
    /**
195
     * @param $data
196
     * @param $callback
197
     * @return mixed
198
     */
199
    public static function returnCallback($data,$callback)
200
    {
201
        return call_user_func_array($callback,[$data]);
202
    }
203
204
    /**
205
     * @param $namespace
206
     * @return string
207
     */
208
    public static function getNamespace($namespace)
209
    {
210
        $rootDelete=str_replace(root.''.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'','',$namespace);
211
212
        return 'App\\'.self::generatorNamespace(
213
                explode(''.DIRECTORY_SEPARATOR.'',$rootDelete)
214
            );
215
216
    }
217
218
    /**
219
     * @param $callback
220
     * @return mixed
221
     */
222
    public static function callbackProcess($callback)
223
    {
224
        return (is_callable($callback)) ? call_user_func_array($callback,[app()]) : $callback;
225
    }
226
227
    /**
228
     * @param $array1
229
     * @param $array2
230
     * @return bool
231
     */
232
    public static function array_diff_key_recursive ($array1, $array2)
233
    {
234
        if(count($array1)!==count($array2)) self::$bool[]=false;
235
236
        foreach ($array1 as $array1_key=>$array1_value){
237
238
            if(!is_array($array1_value)){
239
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
240
            }
241
            else{
242
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
243
244
                if(!isset($array2[$array1_key]) OR !is_array($array2[$array1_key])) $array2[$array1_key]=[];
245
246
                if(isset($array1_value[0])) $array1_value=$array1_value[0];
247
248
                if(isset($array2[$array1_key][0])) $array2[$array1_key]=$array2[$array1_key][0];
249
250
                self::array_diff_key_recursive($array1_value,$array2[$array1_key]);
251
            }
252
        }
253
254
        if(in_array(false,self::$bool)){
255
            return false;
256
        }
257
        return true;
258
    }
259
260
    /**
261
     * @param $data
262
     * @return mixed
263
     */
264
    public static function slashToBackSlash($data)
265
    {
266
        return str_replace("/","\\",$data);
267
    }
268
269
    /**
270
     * @param int $debug
271
     * @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...
272
     * @return null
273
     */
274
    public static function trace($debug=0,$key=null)
275
    {
276
        $trace=debug_backtrace();
277
278
        if($key===null){
0 ignored issues
show
introduced by
The condition $key === null is always true.
Loading history...
279
            return $trace[$debug] ?? null;
280
        }
281
        return $trace[$debug][$key] ?? null;
282
283
    }
284
285
    /**
286
     * @param $dir
287
     * @param $dirPermissions
288
     * @param $filePermissions
289
     */
290
    public  static function chmod_r($dir, $dirPermissions, $filePermissions)
291
    {
292
        $dp = opendir($dir);
293
        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

293
        while($file = readdir(/** @scrutinizer ignore-type */ $dp)) {
Loading history...
294
            if (($file == ".") || ($file == ".."))
295
                continue;
296
297
            $fullPath = $dir."/".$file;
298
299
            if(is_dir($fullPath)) {
300
                chmod($fullPath, $dirPermissions);
301
                self::chmod_r($fullPath, $dirPermissions, $filePermissions);
302
            } else {
303
                chmod($fullPath, $filePermissions);
304
            }
305
306
        }
307
        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

307
        closedir(/** @scrutinizer ignore-type */ $dp);
Loading history...
308
    }
309
310
    /**
311
     * @return array
312
     */
313
    public static function getServiceConf()
314
    {
315
        if(property_exists(core(),'serviceConf') && defined('methodName')){
316
            return core()->serviceConf;
317
        }
318
        return [];
319
    }
320
321
322
    /**
323
     * @param $dir
324
     * @param bool $recursive
325
     * @param string $basedir
326
     * @param bool $include_dirs
327
     * @return array
328
     */
329
    public static function getAllFilesInDirectory($dir, $recursive = true, $basedir = '', $include_dirs = false)
330
    {
331
        if ($dir == '') {return array();} else {$results = array(); $subresults = array();}
332
        if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent
333
        if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;}
334
335
        $files = scandir($dir);
336
        foreach ($files as $key => $value){
337
            if ( ($value != '.') && ($value != '..') ) {
338
                $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
339
                if (is_dir($path)) {
340
                    // optionally include directories in file list
341
                    if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);}
342
                    // optionally get file list for all subdirectories
343
                    if ($recursive) {
344
                        $subdirresults = self::getAllFilesInDirectory($path, $recursive, $basedir, $include_dirs);
345
                        $results = array_merge($results, $subdirresults);
346
                    }
347
                } else {
348
                    // strip basedir and add to subarray to separate file list
349
                    $subresults[] = str_replace($basedir, '', $path);
350
                }
351
            }
352
        }
353
        // merge the subarray to give the list of files then subdirectory files
354
        if (count($subresults) > 0) {$results = array_merge($subresults, $results);}
355
        return $results;
356
    }
357
358
    /**
359
     * @param $files
360
     * @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...
361
     * @return array
362
     */
363
    public static function getPathWithPhpExtension($files,$reelPath=null)
364
    {
365
        $pathWithPhpList = [];
366
367
        foreach ($files as $file){
368
369
            if(preg_match('@(.*).php@is',$file,$pathWithPhp)){
370
371
                if($reelPath===null){
372
                    $pathWithPhpList[] = $pathWithPhp[0];
373
                }
374
                else{
375
                    $pathWithPhpList[] = $reelPath.'/'.$pathWithPhp[0];
376
                }
377
378
            }
379
        }
380
381
        return $pathWithPhpList;
382
    }
383
384
    /**
385
     * @return array
386
     */
387
    public static function getHttpMethods()
388
    {
389
        return [
390
            'get',
391
            'head',
392
            'post',
393
            'put',
394
            'delete',
395
            'connect',
396
            'options',
397
            'trace'
398
        ];
399
    }
400
401
    /**
402
     * @param $class
403
     * @return mixed
404
     */
405
    public static function resolverClass($class)
406
    {
407
        if(self::isNamespaceExists($class)){
408
            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

408
            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...
409
        }
410
411
        return $class;
412
    }
413
414
    /**
415
     * @return array
416
     */
417
    public static function getRequestPathInfo()
418
    {
419
        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...
420
            return explode("/",request()->getPathInfo());
421
        }
422
        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...
423
    }
424
425
    /**
426
     * @param $trace
427
     * @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...
428
     * @return array
429
     */
430
    public static function removeTrace($trace,$remove=null)
431
    {
432
        $list = [];
433
434
        foreach($trace as $key=>$item){
435
436
            if(isset($item['file']) && !preg_match('@'.$remove.'@',$item['file'])){
437
                $list[$key] = $item;
438
            }
439
        }
440
441
        return array_values($list);
442
    }
443
}