Passed
Push — develop ( c09191...4b5082 )
by nguereza
02:18
created

Php::callArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine Stdlib
5
 *
6
 * Platine Stdlib is a the collection of frequently used php features
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Stdlib
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Php.php
33
 *
34
 *  The PHP helper class
35
 *
36
 *  @package    Platine\Stdlib\Helper
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Stdlib\Helper;
48
49
use Throwable;
50
51
/**
52
 * Class Php
53
 * @package Platine\Stdlib\Helper
54
 */
55
class Php
56
{
57
58
    /**
59
     * Call by callback
60
     * @param callable|array<mixed> $callback
61
     * @param mixed ...$args
62
     *
63
     * @return mixed
64
     */
65
    public static function call($callback, ...$args)
66
    {
67
        if (is_string($callback)) {
68
            // className::method
69
            if (strpos($callback, '::') > 0) {
70
                $callback = explode('::', $callback, 2);
71
            } elseif (function_exists($callback)) { //function
72
                return $callback(...$args);
73
            }
74
        } elseif (is_object($callback) && method_exists($callback, '__invoke')) {
75
            return $callback(...$args);
76
        }
77
78
        if (is_array($callback)) {
79
            [$obj, $method] = $callback;
80
81
            return is_object($obj)
82
                    ? $obj->{$method}(...$args)
83
                    : $obj::$method(...$args);
84
        }
85
86
        return $callback(...$args);
87
    }
88
89
    /**
90
     * Call by array
91
     * @param callable|array<mixed> $callback
92
     * @param array<int, mixed> $args
93
     *
94
     * @return mixed
95
     */
96
    public static function callArray($callback, array $args)
97
    {
98
        return self::call($callback, ...$args);
99
    }
100
101
    /**
102
     * Dump variables
103
     * @param mixed ...$vars
104
     * @return string
105
     */
106
    public static function dumpVars(...$vars): string
107
    {
108
        ob_start();
109
        var_dump(...$vars);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($vars) looks like debug code. Are you sure you do not want to remove it?
Loading history...
110
111
        $string = (string) ob_get_clean();
112
113
        return (string) preg_replace("/=>\n\s+/", '=> ', $string);
114
    }
115
116
    /**
117
     * Print variables
118
     * @param mixed ...$vars
119
     * @return string
120
     */
121
    public static function printVars(...$vars): string
122
    {
123
        $string = '';
124
        foreach ($vars as $var) {
125
            $string .= print_r($var, true) . PHP_EOL;
0 ignored issues
show
Bug introduced by
Are you sure print_r($var, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

125
            $string .= /** @scrutinizer ignore-type */ print_r($var, true) . PHP_EOL;
Loading history...
126
        }
127
128
        return (string) preg_replace("/Array\n\s+\(/", 'Array (', $string);
129
    }
130
131
    /**
132
     * Export variable
133
     * @param mixed $var
134
     * @return string
135
     */
136
    public static function exportVar($var): string
137
    {
138
        $string = var_export($var, true);
139
140
        return (string) preg_replace(
141
            '/=>\s+\n\s+array \(/',
142
            '=> array (',
143
            $string
144
        );
145
    }
146
147
    /**
148
     * Convert an Exception to string
149
     * @param Throwable $err
150
     * @param string $title
151
     * @param bool $debug
152
     * @return string
153
     */
154
    public static function exceptionToString(
155
        Throwable $err,
156
        string $title = '',
157
        bool $debug = false
158
    ): string {
159
        $className = get_class($err);
160
        if (!$debug) {
161
            return sprintf(
162
                '%s %s(code:%d) %s',
163
                $title,
164
                $className,
165
                $err->getCode(),
166
                $err->getMessage()
167
            );
168
        }
169
170
        return sprintf(
171
            '%s%s(code:%d) %s at %s line %d',
172
            $title ? $title . '-' : '',
173
            $className,
174
            $err->getCode(),
175
            $err->getMessage(),
176
            $err->getFile(),
177
            $err->getLine()
178
        );
179
    }
180
181
    /**
182
     * Convert an Exception to array
183
     * @param Throwable $err
184
     * @param bool $debug
185
     * @return array<string, mixed>
186
     */
187
    public static function exceptionToArray(
188
        Throwable $err,
189
        bool $debug = false
190
    ): array {
191
        if (!$debug) {
192
            return [
193
                'code' => $err->getCode(),
194
                'error' => $err->getMessage()
195
            ];
196
        }
197
198
        return [
199
                'code' => $err->getCode(),
200
                'error' => sprintf('(%s) %s', get_class($err), $err->getMessage()),
201
                'file' => sprintf('at %s line %d', $err->getFile(), $err->getLine()),
202
                'trace' => $err->getTraceAsString(),
203
            ];
204
    }
205
}
206