Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between closures #42

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class Backtrace
/** @var array */
protected $trace;

/** @var array */
protected $zeroStack;

public function __construct(array $trace)
{
$this->trace = $trace;
$this->trace = $trace[1];
$this->zeroStack = $trace[0];
}

public function getArguments(): array
Expand All @@ -36,7 +40,12 @@ public function getHash(): string
return is_object($argument) ? spl_object_hash($argument) : $argument;
}, $this->getArguments());

return md5($this->getFunctionName().serialize($normalizedArguments));
$prefix = $this->getFunctionName();
if (strpos($prefix, '{closure}') !== false) {
$prefix = $this->zeroStack['line'];
}

return md5($prefix.serialize($normalizedArguments));
}

protected function staticCall()
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ function once($callback)
{
$trace = debug_backtrace(
DEBUG_BACKTRACE_PROVIDE_OBJECT, 2
)[1];
);

$backtrace = new Backtrace($trace);

if ($trace['function'] === 'eval') {
if ($backtrace->getFunctionName() === 'eval') {
return call_user_func($callback);
}

Expand Down
53 changes: 53 additions & 0 deletions tests/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,57 @@ public function it_will_not_throw_error_with_eval()

$this->assertTrue(in_array($result, range(1, 1000)));
}

/** @test */
public function it_will_differentiate_between_closures()
{
$testClass = new class() {
public function getNumber()
{
$closure = function () {
return once(function () {
return random_int(1, 1000);
});
};

return $closure();
}

public function secondNumber()
{
$closure = function () {
return once(function () {
return random_int(1001, 2000);
});
};

return $closure();
}
};

$this->assertNotEquals($testClass->getNumber(), $testClass->secondNumber());
}

/** @test */
public function it_will_run_callback_once_for_closure_called_on_differemt_lines()
{
$testClass = new class() {
public function getNumbers()
{
$closure = function () {
return once(function () {
return random_int(1, 10000000);
});
};

$numbers[] = $closure();
$numbers[] = $closure();

return $numbers;
}
};

$results = $testClass->getNumbers();
$this->assertEquals($results[0], $results[1]);
}
}