22
33namespace Kraken \Throwable ;
44
5- abstract class Error extends \Error
5+ class Error extends \Error
66{
77 /**
88 * @param string $message
9- * @param \Error|\Exception $previous
9+ * @param \Error|\Exception|null $previous
1010 */
11- public function __construct ($ message = 'Unknown exception ' , $ previous = null )
11+ public function __construct ($ message = 'Unknown error ' , $ previous = null )
1212 {
1313 parent ::__construct ($ message , 0 , $ previous );
1414 }
@@ -18,40 +18,46 @@ public function __construct($message = 'Unknown exception', $previous = null)
1818 */
1919 public function __toString ()
2020 {
21- return self ::toString ($ this );
21+ return static ::toString ($ this );
2222 }
2323
2424 /**
25+ * Return Error full trace in string format.
26+ *
2527 * @param \Error|\Exception $ex
2628 * @return string
2729 */
2830 public static function toString ($ ex )
2931 {
3032 return implode ("\n" , [
31- "" ,
32- self :: toExceptionString ($ ex ),
33- "\t" . 'stack trace: ' ,
34- self :: toTraceString ($ ex )
33+ "\t" . ' Throwable trace: ' ,
34+ static :: toThrowableString ($ ex ),
35+ "\t" . 'Stack trace: ' ,
36+ static :: toStackString ($ ex )
3537 ]);
3638 }
3739
3840 /**
41+ * Return Error full trace in array format.
42+ *
3943 * @param \Error|\Exception $ex
4044 * @return mixed
4145 */
4246 public static function toTrace ($ ex )
4347 {
44- return ThrowableHelper ::getThrowableStack ($ ex );
48+ return Throwable ::getThrowableStack ($ ex );
4549 }
4650
4751 /**
52+ * Return Error stack trace in array format.
53+ *
4854 * @param \Error|\Exception $ex
4955 * @return string[]
5056 */
51- public static function toTraceStack ($ ex )
57+ public static function toStackTrace ($ ex )
5258 {
5359 $ list = [];
54- for ($ stack = ThrowableHelper ::getThrowableStack ($ ex ); $ stack !== null ; $ stack = $ stack ['prev ' ])
60+ for ($ stack = Throwable ::getThrowableStack ($ ex ); $ stack !== null ; $ stack = $ stack ['prev ' ])
5561 {
5662 $ list = array_merge ($ stack ['trace ' ], $ list );
5763 }
@@ -60,48 +66,60 @@ public static function toTraceStack($ex)
6066 }
6167
6268 /**
69+ * Return Error throwable trace in array format.
70+ *
6371 * @param \Error|\Exception $ex
6472 * @return string[]
6573 */
66- public static function toExceptionStack ($ ex )
74+ public static function toThrowableTrace ($ ex )
6775 {
6876 $ list = [];
69- for ($ stack = ThrowableHelper ::getThrowableStack ($ ex ); $ stack !== null ; $ stack = $ stack ['prev ' ])
77+ for ($ stack = Throwable ::getThrowableStack ($ ex ); $ stack !== null ; $ stack = $ stack ['prev ' ])
7078 {
71- $ list [] = ThrowableHelper ::parseThrowableMessage ($ stack );
79+ $ list [] = Throwable ::parseThrowableMessage ($ stack );
7280 }
7381
7482 return array_reverse ($ list );
7583 }
7684
7785 /**
86+ * Return Error stack trace in string format.
87+ *
7888 * @param \Error|\Exception $ex
7989 * @return string
8090 */
81- public static function toTraceString ($ ex )
91+ public static function toStackString ($ ex )
8292 {
8393 $ stack = [];
8494 $ i = 0 ;
85- foreach (self ::toTraceStack ($ ex ) as $ element )
95+ $ trace = static ::toStackTrace ($ ex );
96+ $ pad = strlen (count ($ trace )) > 2 ?: 2 ;
97+
98+ foreach ($ trace as $ element )
8699 {
87- $ stack [] = "\t# " . $ i . ' ' . $ element ;
100+ $ stack [] = "\t" . str_pad ( '' . $ i , $ pad , ' ' , STR_PAD_LEFT ) . '. ' . $ element ;
88101 ++$ i ;
89102 }
90103
91104 return implode ("\n" , $ stack );
92105 }
93106
94107 /**
108+ * Return Error throwable trace in string format.
109+ *
95110 * @param \Error|\Exception $ex
96111 * @return string
97112 */
98- public static function toExceptionString ($ ex )
113+ public static function toThrowableString ($ ex )
99114 {
100115 $ stack = [];
101116 $ i = 0 ;
102- foreach (self ::toExceptionStack ($ ex ) as $ element )
117+ $ trace = static ::toThrowableTrace ($ ex );
118+ $ pad = strlen (count ($ trace )) > 2 ?: 2 ;
119+
120+ foreach ($ trace as $ element )
103121 {
104- $ stack [] = "\t# " . $ i . ' ' . $ element ;
122+ $ stack [] = "\t" . str_pad ( '' . $ i , $ pad , ' ' , STR_PAD_LEFT ) . '. ' . $ element ;
105123 ++$ i ;
106124 }
107125
0 commit comments