Repository navigation
Replace all debug_backtrace boilerplate with get_backtrace_loc - #1654
Conversation
| * How deep in the call stack to get the location. 0 is the caller | ||
| * of `get_backtrace_loc`, 1 is the caller of that function, etc | ||
| * @return string | ||
| * Returns a trace location like " in foo.php on line 742" |
There was a problem hiding this comment.
It feels weird to have this function return leading whitespace -- that was unexpected. I'd rather the callers manage adding whitespace where needed.
| $trace = debug_backtrace(); | ||
| if (($file = $trace[$depth]["file"] ?? "")) { | ||
| $file = " in $file"; | ||
| } | ||
| if (($line = $trace[$depth]["line"] ?? "")) { | ||
| $line = " on line $line"; | ||
| } | ||
| return $file . $line; |
There was a problem hiding this comment.
Perhaps something like this:
| $trace = debug_backtrace(); | |
| if (($file = $trace[$depth]["file"] ?? "")) { | |
| $file = " in $file"; | |
| } | |
| if (($line = $trace[$depth]["line"] ?? "")) { | |
| $line = " on line $line"; | |
| } | |
| return $file . $line; | |
| $return = []; | |
| $trace = debug_backtrace(); | |
| if (($file = $trace[$depth]["file"] ?? "")) { | |
| $return[] = "in $file"; | |
| } | |
| if (($line = $trace[$depth]["line"] ?? "")) { | |
| $return[] = "on line $line"; | |
| } | |
| return join(" ", $return); |
There was a problem hiding this comment.
The leading whitespace is deliberate though: it's there to make it easier on the caller to just append the location to the error message without having to worry about having to insert an extra space when the location isn't empty, eg Undefined property via __set(): ' . $name . get_backtrace_loc()
There was a problem hiding this comment.
Yeah, but we can just:
'Undefined property via __get(): $name ' . get_backtrace_loc(),There was a problem hiding this comment.
I don't really care if there's a trailing whitespace on exception messages 🤷
There was a problem hiding this comment.
ACK. Changed as you suggested.
...which does thing in a way that PHPStan level 7 approves of and can handle missing line numbers or filename in the backtrace info. NB this produces different DPDatabase log output "in foo.php on line N" instead of "foo.php:N"
717d709 to
6316c7b
Compare
...which does thing in a way that PHPStan level 7 approves of and can handle missing line numbers or filename in the backtrace info.
NB this produces different DPDatabase log output
"in foo.php on line N" instead of "foo.php:N"