| Conditions | 4 |
| Paths | 1 |
| Total Lines | 52 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 34 | public function servicing( |
||
| 35 | Address $advertise, |
||
| 36 | string $service, |
||
| 37 | array $tags = [], |
||
| 38 | int $heartbeat = Defaults::HEARTBEAT |
||
| 39 | ) : Service { |
||
| 40 | $serviced = (new Service($service)) |
||
| 41 | ->setEndpoints( |
||
| 42 | (new Endpoint($advertise)) |
||
| 43 | ->relatedService($service) |
||
| 44 | ->setTags(...$tags) |
||
| 45 | ->resetID() |
||
| 46 | )->setKeepalive($heartbeat) |
||
| 47 | ; |
||
| 48 | |||
| 49 | async(static function (Agent $agent, Service $service) { |
||
| 50 | for (;;) { |
||
| 51 | /** |
||
| 52 | * @var Result $registered |
||
| 53 | */ |
||
| 54 | |||
| 55 | try { |
||
| 56 | $registered = yield (new AgentServiceRegister($agent))->service($service)->result(); |
||
| 57 | } catch (Throwable $e) { |
||
| 58 | logger('consul')->warning('Service registering error', [ |
||
| 59 | 'svc' => $service->id(), |
||
| 60 | 'error' => sprintf('%s::%s', get_class($e), $e->getMessage()), |
||
| 61 | ]); |
||
| 62 | goto RETRYING; |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($registered->success()) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | logger('consul')->info('Service registering failed', [ |
||
| 70 | 'svc' => $service->id(), |
||
| 71 | 'agent' => $service->hosting(), |
||
| 72 | ]); |
||
| 73 | |||
| 74 | RETRYING: |
||
| 75 | |||
| 76 | yield msleep($sleep = rand(Defaults::ERROR_RETRY_MIN, Defaults::ERROR_RETRY_MAX)); |
||
| 77 | |||
| 78 | logger('consul')->notice('Service register retrying', [ |
||
| 79 | 'svc' => $service->id(), |
||
| 80 | 'delay' => $sleep, |
||
| 81 | ]); |
||
| 82 | } |
||
| 83 | }, null, $this->agent, $serviced)->fusion(); |
||
| 84 | |||
| 85 | return $serviced; |
||
| 86 | } |
||
| 88 |