Skip to content

Commit

Permalink
Merge pull request #43 from sergiz/pr/support-global-functions
Browse files Browse the repository at this point in the history
Support global functions
  • Loading branch information
Rias authored Feb 18, 2020
2 parents d721d3e + b775390 commit c6e5381
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function getFunctionName(): string
*/
public function getObject()
{
if ($this->globalFunction()) {
return $this->zeroStack['file'];
}

return $this->staticCall() ? $this->trace['class'] : $this->trace['object'];
}

Expand All @@ -52,4 +56,9 @@ protected function staticCall()
{
return $this->trace['type'] == '::';
}

protected function globalFunction()
{
return ! isset($this->trace['type']);
}
}
13 changes: 13 additions & 0 deletions tests/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,17 @@ public function getNumbers()
$results = $testClass->getNumbers();
$this->assertEquals($results[0], $results[1]);
}

/** @test */
public function it_will_work_in_global_functions()
{
function globalFunction()
{
return once(function () {
return random_int(1, 10000000);
});
}

$this->assertEquals(globalFunction(), globalFunction());
}
}

0 comments on commit c6e5381

Please sign in to comment.