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 |
||
28 | class KeyboardButton extends Entity |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function __construct($data) |
||
34 | { |
||
35 | if (is_string($data)) { |
||
36 | $data = ['text' => $data]; |
||
37 | } |
||
38 | parent::__construct($data); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Check if the passed data array could be a KeyboardButton. |
||
43 | * |
||
44 | * @param array $data |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public static function couldBe($data) |
||
49 | { |
||
50 | return is_array($data) && array_key_exists('text', $data); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | protected function validate() |
||
57 | { |
||
58 | if ($this->getProperty('text', '') === '') { |
||
59 | throw new TelegramException('You must add some text to the button!'); |
||
60 | } |
||
61 | |||
62 | if ($this->getRequestContact() && $this->getRequestLocation()) { |
||
63 | throw new TelegramException('You must use only one of these fields: request_contact, request_location!'); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | View Code Duplication | public function __call($method, $args) |
|
79 | } |
||
80 |
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.