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 IssueCommentEvent 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(4), |
|
| 25 | 4 | new Information( |
|
| 26 | 4 | $event['payload']['issue']['html_url'], |
|
| 27 | 4 | $this->buildPayload($event), |
|
| 28 | 4 | $this->buildMessage($event) |
|
| 29 | ), |
||
| 30 | 4 | new Ring(3000, 80), |
|
| 31 | 4 | $this->buildSound($event), |
|
| 32 | ); |
||
|
|
|||
| 33 | } |
||
| 34 | |||
| 35 | 4 | private function buildPayload(array $event): string |
|
| 36 | { |
||
| 37 | 4 | if (isset($event['comment']['body'])) { |
|
| 38 | 2 | return $event['comment']['body']; |
|
| 39 | } |
||
| 40 | |||
| 41 | 2 | return $event['payload']['issue']['title']; |
|
| 42 | } |
||
| 43 | |||
| 44 | 4 | private function buildMessage(array $event): string |
|
| 45 | { |
||
| 46 | 4 | return \sprintf('%s commented in %s', $event['actor']['login'], $event['repo']['name']); |
|
| 47 | } |
||
| 48 | |||
| 49 | 4 | private function buildSound(array $event): BaseSound |
|
| 50 | { |
||
| 51 | 4 | if (\in_array($event['repo']['name'], self::SPECIAL_REPOSITORIES, true)) { |
|
| 52 | 2 | return new ClavEgg(); |
|
| 53 | } |
||
| 58 |