|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug7639; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +abstract class TestCase |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @return static|null |
| 11 | + */ |
| 12 | + public static function getTestFromBacktrace() |
| 13 | + { |
| 14 | + foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT) as $frame) { |
| 15 | + if (($frame['object'] ?? null) instanceof static) { |
| 16 | + assertType('static(Bug7639\\TestCase)', $frame['object']); |
| 17 | + return $frame['object']; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return static|null |
| 26 | + */ |
| 27 | + public static function getTestFromBacktraceWithoutNullCoalesce() |
| 28 | + { |
| 29 | + foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT) as $frame) { |
| 30 | + if (isset($frame['object']) && $frame['object'] instanceof static) { |
| 31 | + assertType('static(Bug7639\\TestCase)', $frame['object']); |
| 32 | + return $frame['object']; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return null; |
| 37 | + } |
| 38 | +} |
0 commit comments