Skip to content

Commit 2db0730

Browse files
committed
Support for array_* callback in SourceLocation::createHere
1 parent 7ec1c5d commit 2db0730

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Model/SourceLocation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ public function __construct($message, $path, $line, array $context = [])
6262
*/
6363
public static function createHere($message, array $context = [])
6464
{
65-
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1);
65+
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2) as $trace) {
66+
if (isset($trace['file'])) {
67+
break;
68+
}
69+
}
6670

67-
return new self($message, $trace[0]['file'], $trace[0]['line'], $context);
71+
return new self($message, $trace['file'], $trace['line'], $context);
6872
}
6973

7074
/**

tests/Unit/Model/SourceLocationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ public function testCreateHere()
2626
$this->assertEquals('foobar', $location->getMessage());
2727
$this->assertEquals(['foo' => 'bar'], $location->getContext());
2828
}
29+
30+
public function testCreateHereViaCallback()
31+
{
32+
$location = array_map('\Translation\Extractor\Model\SourceLocation::createHere', ['baz'])[0];
33+
34+
$this->assertContains('tests/Unit/Model/SourceLocationTest.php', $location->getPath());
35+
$this->assertEquals(32, $location->getLine());
36+
37+
$this->assertEquals('baz', $location->getMessage());
38+
}
2939
}

0 commit comments

Comments
 (0)