|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Debug\Stacktrace; |
| 6 | + |
| 7 | +use ReflectionFunction; |
| 8 | +use ReflectionMethod; |
| 9 | +use ReflectionParameter; |
| 10 | + |
| 11 | +use function Tempest\Support\Path\to_relative_path; |
| 12 | + |
| 13 | +final class Frame |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @param array<Argument> $arguments |
| 17 | + */ |
| 18 | + public function __construct( |
| 19 | + private(set) string $file, |
| 20 | + private(set) int $line, |
| 21 | + private(set) ?string $class, |
| 22 | + private(set) ?string $function, |
| 23 | + private(set) ?string $type, |
| 24 | + private(set) bool $isVendor, |
| 25 | + private(set) ?CodeSnippet $snippet, |
| 26 | + private(set) string $absoluteFile, |
| 27 | + private(set) string $relativeFile, |
| 28 | + private(set) array $arguments, |
| 29 | + private(set) int $index, |
| 30 | + ) {} |
| 31 | + |
| 32 | + public static function fromArray(array $frame, int $contextLines = 5, ?string $rootPath = null, int $index = 1): self |
| 33 | + { |
| 34 | + $absoluteFile = $frame['file'] ?? ''; |
| 35 | + $line = $frame['line'] ?? 0; |
| 36 | + $isVendor = self::isVendorFile($absoluteFile, $rootPath); |
| 37 | + $snippet = null; |
| 38 | + |
| 39 | + if ($absoluteFile && $line && ! $isVendor && file_exists($absoluteFile)) { |
| 40 | + $snippet = self::extractCodeSnippet($absoluteFile, $line, $contextLines); |
| 41 | + } |
| 42 | + |
| 43 | + return new self( |
| 44 | + file: $absoluteFile, |
| 45 | + line: $line, |
| 46 | + class: $frame['class'] ?? null, |
| 47 | + function: $frame['function'] ?? null, |
| 48 | + type: $frame['type'] ?? null, |
| 49 | + isVendor: $isVendor, |
| 50 | + snippet: $snippet, |
| 51 | + absoluteFile: $absoluteFile, |
| 52 | + relativeFile: $rootPath ? to_relative_path($rootPath, $absoluteFile) : $absoluteFile, |
| 53 | + arguments: self::extractArguments($frame), |
| 54 | + index: $index, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return array<Argument> |
| 60 | + */ |
| 61 | + public static function extractArguments(array $frame): array |
| 62 | + { |
| 63 | + if (! isset($frame['args']) || ! is_array($frame['args'])) { |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + $arguments = $frame['args']; |
| 68 | + $parameterNames = []; |
| 69 | + |
| 70 | + try { |
| 71 | + $reflection = isset($frame['class'], $frame['function']) |
| 72 | + ? new ReflectionMethod(objectOrMethod: $frame['class'], method: $frame['function']) |
| 73 | + : new ReflectionFunction(function: $frame['function']); |
| 74 | + |
| 75 | + $parameterNames = array_map( |
| 76 | + callback: fn (ReflectionParameter $param) => $param->getName(), |
| 77 | + array: $reflection->getParameters(), |
| 78 | + ); |
| 79 | + } catch (\Throwable) { |
| 80 | + // @mago-expect lint:no-empty-catch-clause |
| 81 | + } |
| 82 | + |
| 83 | + $result = []; |
| 84 | + foreach ($arguments as $index => $value) { |
| 85 | + $result[] = Argument::make( |
| 86 | + name: $parameterNames[$index] ?? $index, |
| 87 | + value: $value, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return $result; |
| 92 | + } |
| 93 | + |
| 94 | + public static function isVendorFile(string $file, ?string $rootPath = null): bool |
| 95 | + { |
| 96 | + if ($file === '') { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + if ($rootPath !== null) { |
| 101 | + return ! str_starts_with( |
| 102 | + haystack: str_replace('\\', '/', $file), |
| 103 | + needle: str_replace('\\', '/', $rootPath), |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + return str_contains($file, '/vendor/') || str_contains($file, '\\vendor\\'); |
| 108 | + } |
| 109 | + |
| 110 | + public static function extractCodeSnippet(string $file, int $line, int $contextLines): ?CodeSnippet |
| 111 | + { |
| 112 | + $fileLines = file($file, FILE_IGNORE_NEW_LINES); |
| 113 | + |
| 114 | + if ($fileLines === false) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + $startLine = max(1, $line - $contextLines); |
| 119 | + $endLine = min(count($fileLines), $line + $contextLines); |
| 120 | + $lines = []; |
| 121 | + |
| 122 | + for ($i = $startLine; $i <= $endLine; $i++) { |
| 123 | + $lines[$i] = $fileLines[$i - 1]; |
| 124 | + } |
| 125 | + |
| 126 | + if ($lines === []) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + return new CodeSnippet( |
| 131 | + lines: $lines, |
| 132 | + highlightedLine: $line, |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + public function getMethodName(): string |
| 137 | + { |
| 138 | + if (! $this->class) { |
| 139 | + return $this->function ?? ''; |
| 140 | + } |
| 141 | + |
| 142 | + $type = match ($this->type) { |
| 143 | + '::' => '::', |
| 144 | + '->' => '->', |
| 145 | + default => '', |
| 146 | + }; |
| 147 | + |
| 148 | + return $this->class . $type . ($this->function ?? ''); |
| 149 | + } |
| 150 | +} |
0 commit comments