| Total Lines | 71 |
| Code Lines | 33 |
| 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 | 9 | private function makeLoader(Closure $batchLoadFunction, $defaultResolveValue) |
|
| 38 | { |
||
| 39 | 9 | return new class ($batchLoadFunction, $defaultResolveValue) |
|
| 40 | { |
||
| 41 | 9 | private array $deferredPromises; |
|
| 42 | 9 | private bool $needsResolving = false; |
|
| 43 | |||
| 44 | public function __construct(private Closure $batchLoadFunction, private $defaultResolveValue) |
||
| 45 | 9 | { |
|
| 46 | } |
||
| 47 | 9 | ||
| 48 | 9 | public function load($key) |
|
| 49 | 9 | { |
|
| 50 | 8 | $serializedKey = self::serializeKey($key); |
|
| 51 | 8 | ||
| 52 | [$deferred] = $this->deferredPromises[$serializedKey] |
||
| 53 | 8 | ?? $this->deferredPromises[$serializedKey] = [new Deferred(), $key]; |
|
| 54 | 9 | ||
| 55 | 9 | $this->scheduleResolveIfNeeded(); |
|
| 56 | 9 | ||
| 57 | return $deferred->promise(); |
||
| 58 | } |
||
| 59 | |||
| 60 | 24 | private function scheduleResolveIfNeeded() |
|
| 61 | { |
||
| 62 | 24 | if (! $this->needsResolving) { |
|
| 63 | Loop::defer(Closure::fromCallable([$this, 'resolve'])); |
||
| 64 | $this->needsResolving = true; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | private function resolve() |
||
| 69 | { |
||
| 70 | $indexedOriginalKeys = []; |
||
| 71 | foreach ($this->deferredPromises as [$_, $originalKey]) { |
||
| 72 | $indexedOriginalKeys[] = $originalKey; |
||
| 73 | } |
||
| 74 | |||
| 75 | $result = ($this->batchLoadFunction)($indexedOriginalKeys); |
||
| 76 | |||
| 77 | $currentIndex = 0; |
||
| 78 | foreach ($result as $key => $value) { |
||
| 79 | if ($key === $currentIndex) { |
||
| 80 | $key = $indexedOriginalKeys[$key] ?? $key; |
||
| 81 | } |
||
| 82 | $key = self::serializeKey($key); |
||
| 83 | |||
| 84 | if ([$deferred] = $this->deferredPromises[$key] ?? null) { |
||
| 85 | $deferred->resolve($value); |
||
| 86 | unset($this->deferredPromises[$key]); |
||
| 87 | } |
||
| 88 | |||
| 89 | ++$currentIndex; |
||
| 90 | } |
||
| 91 | |||
| 92 | foreach ($this->deferredPromises as $key => [$deferredPromise]) { |
||
| 93 | $deferredPromise->resolve($this->defaultResolveValue); |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->deferredPromises = []; |
||
| 97 | $this->needsResolving = false; |
||
| 98 | } |
||
| 99 | |||
| 100 | private static function serializeKey($key) |
||
| 101 | { |
||
| 102 | if (is_object($key)) { |
||
| 103 | return spl_object_hash($key); |
||
| 104 | } elseif (is_array($key)) { |
||
| 105 | return md5(json_encode($key)); |
||
| 106 | } |
||
| 107 | return (string) $key; |
||
| 108 | } |
||
| 117 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.