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); |
||
| 13 | class PushEvent extends BaseEvent |
||
| 14 | { |
||
| 15 | private const SPECIAL_REPOSITORIES = [ |
||
| 16 | 'ekinhbayar/gitamp', |
||
| 17 | 'amphp/amp', |
||
| 18 | ]; |
||
| 19 | |||
| 20 | 4 | public function __construct(array $event) |
|
| 21 | { |
||
| 22 | 4 | parent::__construct( |
|
| 23 | 4 | (int) $event['id'], |
|
| 24 | 4 | new Type(1), |
|
| 25 | 4 | new Information($this->buildUrl($event), $this->buildPayload($event), $this->buildMessage($event)), |
|
| 26 | 4 | new Ring(3000, 80), |
|
| 27 | 4 | $this->buildSound($event), |
|
| 28 | ); |
||
|
|
|||
| 29 | } |
||
| 30 | |||
| 31 | 4 | private function buildUrl(array $event): string |
|
| 32 | { |
||
| 33 | 4 | return 'https://github.com/' . $event['repo']['name']; |
|
| 34 | } |
||
| 35 | |||
| 36 | 4 | private function buildPayload(array $event): string |
|
| 37 | { |
||
| 38 | 4 | if (isset($event['payload']['commits'][0]['message'])) { |
|
| 39 | 2 | return $event['payload']['commits'][0]['message']; |
|
| 40 | } |
||
| 41 | |||
| 42 | 2 | return 'https://github.com/' . $event['actor']['login']; |
|
| 43 | } |
||
| 44 | |||
| 45 | 4 | private function buildMessage(array $event): string |
|
| 46 | { |
||
| 47 | 4 | return \sprintf('%s pushed to %s', $event['actor']['login'], $event['repo']['name']); |
|
| 48 | } |
||
| 49 | |||
| 50 | 4 | private function buildSound(array $event): BaseSound |
|
| 51 | { |
||
| 52 | 4 | if (\in_array($event['repo']['name'], self::SPECIAL_REPOSITORIES, true)) { |
|
| 53 | 2 | return new CelestaEgg(); |
|
| 54 | } |
||
| 59 |