1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Author: panosru |
7
|
|
|
* Date: 27/04/2018 |
8
|
|
|
* Time: 21:23 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Omega\FaultManager\Traits; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait FaultMutator |
15
|
|
|
* @package Omega\FaultManager\Traits |
16
|
|
|
* @codeCoverageIgnore |
17
|
|
|
*/ |
18
|
|
|
trait FaultMutator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param \Throwable $exception |
22
|
|
|
*/ |
23
|
|
|
protected static function mutate(\Throwable $exception): void |
24
|
|
|
{ |
25
|
|
|
// Get exception reflection |
26
|
|
|
$reflection = new \ReflectionObject($exception); |
27
|
|
|
|
28
|
|
|
// When running PHPUnit property "trace" is not available, works fine though in real world... |
29
|
|
|
// Maybe an xdebug issue? |
30
|
|
|
|
31
|
|
|
// Get trace |
32
|
|
|
if ($reflection->hasProperty('trace')) { |
33
|
|
|
$trace = $reflection->getProperty('trace'); |
34
|
|
|
} else { |
35
|
|
|
$parentClass = $reflection->getParentClass(); |
36
|
|
|
// Get parent class \Exception |
37
|
|
|
while (\Exception::class !== $parentClass->getName()) { |
38
|
|
|
$parentClass = $parentClass->getParentClass(); |
39
|
|
|
} |
40
|
|
|
$trace = $parentClass->getProperty('trace'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Get trace |
44
|
|
|
$trace->setAccessible(true); |
45
|
|
|
|
46
|
|
|
// Get stack trace |
47
|
|
|
$stackTrace = $trace->getValue($exception); |
48
|
|
|
// Some times $stackTrace may be empty, especially when we call custom generated exceptions with |
49
|
|
|
// traditional way like throw new \MyCustomGeneratedException() |
50
|
|
|
if (0 < \count($stackTrace)) { |
51
|
|
|
// Remove first in stack because it refers to ReflectionClass::newInstanceArgs() we called previously |
52
|
|
|
\array_shift($stackTrace); |
53
|
|
|
// Remove 'Fault::throw()' from trace if present |
54
|
|
|
if (0 === \strcmp('exception', $stackTrace[0]['function']) && |
55
|
|
|
( |
56
|
|
|
isset($stackTrace[1]) && |
57
|
|
|
0 === \strcmp('throw', $stackTrace[1]['function']) |
58
|
|
|
) |
59
|
|
|
) { |
60
|
|
|
\array_shift($stackTrace); |
61
|
|
|
} |
62
|
|
|
$trace->setValue($exception, $stackTrace); |
63
|
|
|
|
64
|
|
|
// Set "file" and "line" properties |
65
|
|
|
$file = $reflection->getProperty('file'); |
66
|
|
|
$file->setAccessible(true); |
67
|
|
|
$file->setValue($exception, $stackTrace[0]['file']); |
68
|
|
|
|
69
|
|
|
$line = $reflection->getProperty('line'); |
70
|
|
|
$line->setAccessible(true); |
71
|
|
|
$line->setValue($exception, $stackTrace[0]['line']); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|