Core::handle()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 5
nop 3
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Mpyw\Exceper;
4
5
/**
6
 * Class Core
7
 */
8
class Core
9
{
10
    /**
11
     * @param callable $callback
12
     * @param callable $handler
13
     * @param int $types
14
     * @return mixed
15
     * @throws \Exception|\Throwable
16
     */
17
    public static function handle($callback, $handler, $types = null)
18 20
    {
19
        \set_error_handler($handler, $types === null ? \E_ALL | \E_STRICT : $types);
20 20
21
        try {
22
            $result = \call_user_func($callback);
23 20
            \restore_error_handler();
24 7
            return $result;
25 7
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
26 13
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
27 5
        }
28
29
        \restore_error_handler();
30 13
        throw $e;
31 13
    }
32
33
    /**
34
     * @param \Exception|\Throwable $e
35
     * @param string $file
36
     * @param int $line
37
     * @return \Exception|\Throwable $e
38
     */
39
    public static function rewriteLocation($e, $file, $line)
40 8
    {
41
        $rc = new \ReflectionClass($e);
42 8
43
        $rpf = $rc->getProperty('file');
44 8
        $rpl = $rc->getProperty('line');
45 8
46
        $rpf->setAccessible(true);
47 8
        $rpl->setAccessible(true);
48 8
49
        $rpf->setValue($e, $file);
50 8
        $rpl->setValue($e, $line);
51 8
52
        return $e;
53 8
    }
54
}
55