| Total Complexity | 8 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
| 1 | <?php |
||
| 20 | final class CallerStateCollector implements StateCollector |
||
| 21 | { |
||
| 22 | public function collect(): array |
||
| 23 | { |
||
| 24 | $backtrace = debug_backtrace(limit: 5); |
||
| 25 | |||
| 26 | $last_logger_frame = []; |
||
| 27 | $caller_frame = []; |
||
| 28 | |||
| 29 | foreach ($backtrace as $frame) { |
||
| 30 | if ($this->isLoggerFrame($frame)) { |
||
| 31 | $last_logger_frame = $frame; |
||
| 32 | } elseif ($last_logger_frame) { |
||
|
|
|||
| 33 | $caller_frame = $frame; |
||
| 34 | break; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | $result = []; |
||
| 39 | if ($last_logger_frame) { |
||
| 40 | $result['context'] = [ |
||
| 41 | 'file' => $last_logger_frame['file'], |
||
| 42 | 'line' => $last_logger_frame['line'], |
||
| 43 | 'class' => $caller_frame['class'] ?? '', |
||
| 44 | 'function' => $caller_frame['function'] ?? '', |
||
| 45 | 'args' => $caller_frame['args'] ?? '', |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | return $result; |
||
| 49 | } |
||
| 50 | |||
| 51 | private function isLoggerFrame(array $frame): bool |
||
| 59 | } |
||
| 60 | } |
||
| 61 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.