Skip to content

Commit 1de6a4d

Browse files
authored
Merge pull request #95 from ostrolucky/feature/sourceLocation-array_map
Support for array_* callback in SourceLocation::createHere
2 parents ea9a958 + d18598f commit 1de6a4d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Model/SourceLocation.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ 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+
// File is not set if we call from an anonymous context like an array_map function.
67+
if (isset($trace['file'])) {
68+
break;
69+
}
70+
}
6671

67-
return new self($message, $trace[0]['file'], $trace[0]['line'], $context);
72+
return new self($message, $trace['file'], $trace['line'], $context);
6873
}
6974

7075
/**

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)