PocketMine-MP 5.18.2 git-00e39821f06a4b6d728d35053c2621dbb19369ff
ExceptionTraceCleaner.php
1<?php
2
3/*
4 * This file is part of RakLib.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/RakLib>
6 *
7 * RakLib is not affiliated with Jenkins Software LLC nor RakNet.
8 *
9 * RakLib is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 */
14
15declare(strict_types=1);
16
17namespace raklib\utils;
18
19use function array_reverse;
20use function count;
21use function function_exists;
22use function get_class;
23use function gettype;
24use function is_object;
25use function method_exists;
26use function str_replace;
27use function strval;
28use function substr;
29use function xdebug_get_function_stack;
30
32 public function __construct(
33 private string $mainPath
34 ){}
35
41 public function getTrace(int $start = 0, ?array $trace = null) : array{
42 if($trace === null){
43 if(function_exists("xdebug_get_function_stack") && count($trace = @xdebug_get_function_stack()) !== 0){
44 $trace = array_reverse($trace);
45 }else{
46 $e = new \Exception();
47 $trace = $e->getTrace();
48 }
49 }
50
51 $messages = [];
52 $j = 0;
53 for($i = $start; isset($trace[$i]); ++$i, ++$j){
54 $params = "";
55 if(isset($trace[$i]["args"]) or isset($trace[$i]["params"])){
56 if(isset($trace[$i]["args"])){
57 $args = $trace[$i]["args"];
58 }else{
59 $args = $trace[$i]["params"];
60 }
61 foreach($args as $name => $value){
62 $params .= (is_object($value) ? get_class($value) . " " . (method_exists($value, "__toString") ? $value->__toString() : "object") : gettype($value) . " " . @strval($value)) . ", ";
63 }
64 }
65 $messages[] = "#$j " . (isset($trace[$i]["file"]) ? $this->cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . substr($params, 0, -2) . ")";
66 }
67
68 return $messages;
69 }
70
71 public function cleanPath(string $path) : string{
72 return str_replace(["\\", ".php", "phar://", str_replace(["\\", "phar://"], ["/", ""], $this->mainPath)], ["/", "", "", ""], $path);
73 }
74}
getTrace(int $start=0, ?array $trace=null)