Skip to content

Commit

Permalink
[12.x] Fix once() cache when used in extended static class (#54094)
Browse files Browse the repository at this point in the history
* Fix once() cache when used in extended static class

* Update Onceable.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
FrittenKeeZ and taylorotwell authored Jan 6, 2025
1 parent 537a348 commit 91f9068
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Support/Onceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ protected static function hashFromTrace(array $trace, callable $callable)
$callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [],
);

$class = $callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureCalledClass()?->getName() : null;

$class ??= isset($trace[1]['class']) ? $trace[1]['class'] : null;

return hash('xxh128', sprintf(
'%s@%s%s:%s (%s)',
$trace[0]['file'],
isset($trace[1]['class']) ? ($trace[1]['class'].'@') : '',
$class ? $class.'@' : '',
$trace[1]['function'],
$trace[0]['line'],
serialize($uses),
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ public function null()
$this->assertSame($instance->null(), $instance->null());
$this->assertSame(1, $instance->i);
}

public function testExtendedStaticClassOnceCalls()
{
$first = MyClass::staticRand();
$second = MyExtendedClass::staticRand();

$this->assertNotSame($first, $second);
}
}

$letter = 'a';
Expand Down Expand Up @@ -392,3 +400,7 @@ public function callRand()
return once(fn () => $this->rand());
}
}

class MyExtendedClass extends MyClass
{
}

0 comments on commit 91f9068

Please sign in to comment.