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 |
||
| 19 | abstract class AbstractEntityEvent |
||
| 20 | { |
||
| 21 | /** @var AbstractEntity $entity */ |
||
| 22 | protected $entity = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Abstract method of each entity event which will be called if certain entity presents in response |
||
| 26 | * and it's class exists or mapped in the config. |
||
| 27 | * |
||
| 28 | * @return null|bool |
||
| 29 | */ |
||
| 30 | abstract public function run(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Returns chat id if the type of calling entity or it's parent is Message |
||
| 34 | * |
||
| 35 | * @return null|int |
||
| 36 | */ |
||
| 37 | public function getChatId() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Sets entity |
||
| 54 | * |
||
| 55 | * @param AbstractEntity $entity Entity object |
||
| 56 | */ |
||
| 57 | public function setEntity(AbstractEntity $entity) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sends text message to current chat |
||
| 64 | * |
||
| 65 | * @param string $text Message text |
||
| 66 | * |
||
| 67 | * @return bool|\Teebot\Response |
||
| 68 | */ |
||
| 69 | View Code Duplication | protected function sendMessage($text) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Replies on the message |
||
| 86 | * |
||
| 87 | * @param SendMessage $sendMessage Object of the SendMessage method |
||
| 88 | * |
||
| 89 | * @return bool|\Teebot\Response |
||
| 90 | */ |
||
| 91 | View Code Duplication | protected function reply(SendMessage $sendMessage) { |
|
| 103 | } |
||
| 104 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.