| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry; |
| 5 |
|
| 6 |
/** |
| 7 |
* This class contains all the information about an error stacktrace. |
| 8 |
* |
| 9 |
* @author Stefano Arlandini <sarlandini@alice.it> |
| 10 |
*/ |
| 11 |
final class Stacktrace |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var Frame[] The frames that compose the stacktrace |
| 15 |
*/ |
| 16 |
private $frames = []; |
| 17 |
/** |
| 18 |
* Constructor. |
| 19 |
* |
| 20 |
* @param Frame[] $frames A non-empty list of stack frames. The list must be |
| 21 |
* ordered from caller to callee. The last frame is the |
| 22 |
* one creating the exception |
| 23 |
*/ |
| 24 |
public function __construct(array $frames) |
| 25 |
{ |
| 26 |
if (empty($frames)) { |
| 27 |
throw new \InvalidArgumentException('Expected a non empty list of frames.'); |
| 28 |
} |
| 29 |
foreach ($frames as $frame) { |
| 30 |
if (!$frame instanceof Frame) { |
| 31 |
throw new \UnexpectedValueException(\sprintf('Expected an instance of the "%s" class. Got: "%s".', Frame::class, \get_debug_type($frame))); |
| 32 |
} |
| 33 |
} |
| 34 |
$this->frames = $frames; |
| 35 |
} |
| 36 |
/** |
| 37 |
* Gets the stacktrace frames. |
| 38 |
* |
| 39 |
* @return Frame[] |
| 40 |
*/ |
| 41 |
public function getFrames() : array |
| 42 |
{ |
| 43 |
return $this->frames; |
| 44 |
} |
| 45 |
/** |
| 46 |
* Gets the frame at the given index. |
| 47 |
* |
| 48 |
* @param int $index The index from which the frame should be get |
| 49 |
* |
| 50 |
* @throws \OutOfBoundsException |
| 51 |
*/ |
| 52 |
public function getFrame(int $index) : Frame |
| 53 |
{ |
| 54 |
if ($index < 0 || $index >= \count($this->frames)) { |
| 55 |
throw new \OutOfBoundsException(); |
| 56 |
} |
| 57 |
return $this->frames[$index]; |
| 58 |
} |
| 59 |
/** |
| 60 |
* Adds a new frame to the stacktrace. |
| 61 |
* |
| 62 |
* @param Frame $frame The frame |
| 63 |
*/ |
| 64 |
public function addFrame(Frame $frame) : self |
| 65 |
{ |
| 66 |
\array_unshift($this->frames, $frame); |
| 67 |
return $this; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Removes the frame at the given index from the stacktrace. |
| 71 |
* |
| 72 |
* @param int $index The index of the frame |
| 73 |
* |
| 74 |
* @throws \OutOfBoundsException If the index is out of range |
| 75 |
*/ |
| 76 |
public function removeFrame(int $index) : self |
| 77 |
{ |
| 78 |
if (!isset($this->frames[$index])) { |
| 79 |
throw new \OutOfBoundsException(\sprintf('Cannot remove the frame at index %d.', $index)); |
| 80 |
} |
| 81 |
if (\count($this->frames) === 1) { |
| 82 |
throw new \RuntimeException('Cannot remove all frames from the stacktrace.'); |
| 83 |
} |
| 84 |
\array_splice($this->frames, $index, 1); |
| 85 |
return $this; |
| 86 |
} |
| 87 |
} |
| 88 |
|