| Conditions | 3 |
| Paths | 3 |
| Total Lines | 22 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | function once($callback) |
||
| 6 | { |
||
| 7 | $trace = debug_backtrace( |
||
| 8 | DEBUG_BACKTRACE_PROVIDE_OBJECT, 2 |
||
| 9 | )[1]; |
||
| 10 | |||
| 11 | $backtrace = new Backtrace($trace); |
||
| 12 | |||
| 13 | if (! $object = $backtrace->getObject()) { |
||
| 14 | throw new Exception('Cannot use `once` outside a class'); |
||
| 15 | } |
||
| 16 | |||
| 17 | $hash = $backtrace->getArgumentHash(); |
||
| 18 | |||
| 19 | if (! isset($object->__memoized[$backtrace->getFunctionName()][$hash])) { |
||
| 20 | $result = call_user_func($callback, $backtrace->getArguments()); |
||
| 21 | |||
| 22 | $object->__memoized[$backtrace->getFunctionName()][$hash] = $result; |
||
| 23 | } |
||
| 24 | |||
| 25 | return $object->__memoized[$backtrace->getFunctionName()][$hash]; |
||
| 26 | } |
||
| 27 |