Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php declare(strict_types=1); |
||
| 14 | |||
| 15 | class Client |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var LoopInterface |
||
| 19 | */ |
||
| 20 | private $loop; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var AsyncClient |
||
| 24 | */ |
||
| 25 | private $client; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string $token |
||
| 29 | * @param array $options |
||
| 30 | * @return Client |
||
| 31 | */ |
||
| 32 | public static function create( |
||
| 33 | string $token, |
||
| 34 | array $options = [] |
||
| 35 | ): self { |
||
| 36 | $loop = LoopFactory::create(); |
||
| 37 | $options = ApiSettings::getOptions($token,$options, 'Sync'); |
||
| 38 | $client = FoundationClientFactory::create($loop, $options); |
||
| 39 | |||
| 40 | try { |
||
| 41 | Scheduler::setAsyncFactory(function () use ($loop) { |
||
| 42 | return new Scheduler\EventLoopScheduler($loop); |
||
| 43 | }); |
||
| 44 | } catch (\Throwable $t) { |
||
|
|
|||
| 45 | } |
||
| 46 | |||
| 47 | $asyncClient = AsyncClient::createFromClient($client); |
||
| 48 | |||
| 49 | return new self($loop, $asyncClient); |
||
| 50 | } |
||
| 80 |