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
| * 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 |
There was a problem hiding this comment.
0 is the location in this function (which we probably never actually want).
1 is the caller of get_backtrace_loc (which is why $depth defaults to 1).
It would be more useful if $depth was what this comment says, defaults to 0, and the function always returns $depth + 1.
There was a problem hiding this comment.
Perhaps I'm misunderstanding you, but I don't think you're right about the behavior of $depth
nl -b a test.php
1 <?php
2
3 $require_login = false;
4 $relPath = "pinc/";
5 include_once("pinc/base.inc");
6 include_once("pinc/bootstrap.inc");
7 include_once("pinc/misc.inc");
8
9 function foo() {
10 var_dump(get_backtrace_loc(depth: 0));
11 var_dump(get_backtrace_loc(depth: 1));
12 }
13
14 foo();
php test.php
string(41) "in test.php on line 10"
string(41) "in test.php on line 14"0 returns the line where get_backtrace_loc is called from.
1 returns the line where foo is called from.
...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"