| Total Lines | 79 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | 8 | private function makeLoader(Closure $batchLoadFunction, $defaultResolveValue) |
|
| 38 | { |
||
| 39 | 8 | return new class ($batchLoadFunction, $defaultResolveValue) |
|
| 40 | { |
||
| 41 | private $batchLoadFunction; |
||
| 42 | private $defaultResolveValue; |
||
| 43 | |||
| 44 | private $deferredPromises; |
||
| 45 | private $needsResolving; |
||
| 46 | |||
| 47 | public function __construct(Closure $batchLoadFunction, $defaultResolveValue = null) |
||
| 48 | { |
||
| 49 | 8 | $this->batchLoadFunction = $batchLoadFunction; |
|
| 50 | 8 | $this->defaultResolveValue = $defaultResolveValue; |
|
| 51 | |||
| 52 | 8 | $this->deferredPromises = []; |
|
| 53 | 8 | $this->needsResolving = false; |
|
| 54 | } |
||
| 55 | |||
| 56 | public function load($key) |
||
| 57 | { |
||
| 58 | 8 | $serializedKey = self::serializeKey($key); |
|
| 59 | |||
| 60 | 8 | [$deferred] = $this->deferredPromises[$serializedKey] |
|
| 61 | 8 | ?? $this->deferredPromises[$serializedKey] = [new Deferred(), $key]; |
|
| 62 | |||
| 63 | 8 | $this->scheduleResolveIfNeeded(); |
|
| 64 | |||
| 65 | 8 | return $deferred->promise(); |
|
| 66 | } |
||
| 67 | |||
| 68 | private function scheduleResolveIfNeeded() |
||
| 69 | { |
||
| 70 | 8 | if (! $this->needsResolving) { |
|
| 71 | 8 | Loop::defer(Closure::fromCallable([$this, 'resolve'])); |
|
| 72 | 8 | $this->needsResolving = true; |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | private function resolve() |
||
| 77 | { |
||
| 78 | 8 | $indexedOriginalKeys = []; |
|
| 79 | 8 | foreach ($this->deferredPromises as [$_, $originalKey]) { |
|
| 80 | 8 | $indexedOriginalKeys[] = $originalKey; |
|
| 81 | } |
||
| 82 | |||
| 83 | 8 | $result = ($this->batchLoadFunction)($indexedOriginalKeys); |
|
| 84 | |||
| 85 | 7 | $currentIndex = 0; |
|
| 86 | 7 | foreach ($result as $key => $value) { |
|
| 87 | 7 | if ($key === $currentIndex) { |
|
| 88 | 7 | $key = $indexedOriginalKeys[$key] ?? $key; |
|
| 89 | } |
||
| 90 | 7 | $key = self::serializeKey($key); |
|
| 91 | |||
| 92 | 7 | if ([$deferred] = $this->deferredPromises[$key] ?? null) { |
|
| 93 | 7 | $deferred->resolve($value); |
|
| 94 | 7 | unset($this->deferredPromises[$key]); |
|
| 95 | } |
||
| 96 | |||
| 97 | 7 | ++$currentIndex; |
|
| 98 | } |
||
| 99 | |||
| 100 | 7 | foreach ($this->deferredPromises as $key => [$deferredPromise]) { |
|
| 101 | 1 | $deferredPromise->resolve($this->defaultResolveValue); |
|
| 102 | } |
||
| 103 | |||
| 104 | 7 | $this->deferredPromises = []; |
|
| 105 | 7 | $this->needsResolving = false; |
|
| 106 | } |
||
| 107 | |||
| 108 | private static function serializeKey($key) |
||
| 109 | { |
||
| 110 | 8 | if (is_object($key)) { |
|
| 111 | 1 | return spl_object_hash($key); |
|
| 112 | 8 | } elseif (is_array($key)) { |
|
| 113 | 1 | return md5(json_encode($key)); |
|
| 114 | } |
||
| 115 | 8 | return (string) $key; |
|
| 116 | } |
||
| 125 |