Php::exceptionToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.9666
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   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Stdlib\Helper;
48
49
use ReflectionClass;
50
use Throwable;
51
52
/**
53
 * @class Php
54
 * @package Platine\Stdlib\Helper
55
 */
56
class Php
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(callable|array $callback, mixed ...$args): mixed
66
    {
67
        if (is_string($callback)) {
0 ignored issues
show
introduced by
The condition is_string($callback) is always false.
Loading history...
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')) {
0 ignored issues
show
introduced by
The condition is_object($callback) is always false.
Loading history...
75
            return $callback(...$args);
76
        }
77
78
        if (is_array($callback)) {
0 ignored issues
show
introduced by
The condition is_array($callback) is always true.
Loading history...
79
            [$obj, $method] = $callback;
80
81
            return is_object($obj)
82
                    ? $obj->{$method}(...$args)
83
                    : $obj::$method(...$args);
84
        }
85
86
        //Race condition
87
        //@codeCoverageIgnoreStart
88
        return $callback(...$args);
89
        //@codeCoverageIgnoreEnd
90
    }
91
92
    /**
93
     * Call by array
94
     * @param callable|array<mixed> $callback
95
     * @param array<int, mixed> $args
96
     *
97
     * @return mixed
98
     */
99
    public static function callArray(callable|array $callback, array $args): mixed
100
    {
101
        return self::call($callback, ...$args);
102
    }
103
104
    /**
105
     * Convert an Exception to string
106
     * @param Throwable $err
107
     * @param string $title
108
     * @param bool $debug
109
     * @return string
110
     */
111
    public static function exceptionToString(
112
        Throwable $err,
113
        string $title = '',
114
        bool $debug = false
115
    ): string {
116
        $className = get_class($err);
117
        if ($debug === false) {
118
            return sprintf(
119
                '%s %s(code:%d) %s',
120
                $title,
121
                $className,
122
                $err->getCode(),
123
                $err->getMessage()
124
            );
125
        }
126
127
        return sprintf(
128
            '%s%s(code:%d) %s at %s line %d',
129
            $title ? $title . '-' : '',
130
            $className,
131
            $err->getCode(),
132
            $err->getMessage(),
133
            $err->getFile(),
134
            $err->getLine()
135
        );
136
    }
137
138
    /**
139
     * Convert an Exception to array
140
     * @param Throwable $err
141
     * @param bool $debug
142
     * @return array<string, mixed>
143
     */
144
    public static function exceptionToArray(
145
        Throwable $err,
146
        bool $debug = false
147
    ): array {
148
        if ($debug === false) {
149
            return [
150
                'code' => $err->getCode(),
151
                'error' => $err->getMessage()
152
            ];
153
        }
154
155
        return [
156
            'code' => $err->getCode(),
157
            'error' => sprintf('(%s) %s', get_class($err), $err->getMessage()),
158
            'file' => sprintf('at %s line %d', $err->getFile(), $err->getLine()),
159
            'trace' => $err->getTraceAsString(),
160
        ];
161
    }
162
163
    /**
164
     * Return the class short name
165
     * @param class-string|object $fullClassName
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object.
Loading history...
166
     * @return string
167
     */
168
    public static function getShortClassName(string|object $fullClassName): string
169
    {
170
        $reflect = new ReflectionClass($fullClassName);
171
        return $reflect->getShortName();
172
    }
173
}
174