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 |
||
30 | trait MessageCommand |
||
31 | { |
||
32 | /** @} */ |
||
33 | |||
34 | /** \brief Chat id of the current user/group/channel. */ |
||
35 | protected $_chat_id; |
||
36 | |||
37 | /** |
||
38 | * \addtogroup Commands |
||
39 | * \brief What commands are |
||
40 | * @{ |
||
41 | */ |
||
42 | |||
43 | /** \brief (<i>Internal</i>)Store the command triggered on message. */ |
||
44 | protected $_message_commands = []; |
||
45 | |||
46 | /** |
||
47 | * \brief Add a function that will be executed everytime a message contain the selected command |
||
48 | * \details Use this syntax: |
||
49 | * |
||
50 | * addMessageCommand("start", function($bot, $message) { |
||
51 | * $bot->sendMessage("Hi"); }); |
||
52 | * @param string $command The command that will trigger this function (without slash). Eg: "start", "help", "about" |
||
53 | * @param callable $script The function that will be triggered by a command. |
||
54 | * Must take an object(the bot) and an array(the message received). |
||
55 | */ |
||
56 | public function addMessageCommand(string $command, callable $script) |
||
64 | |||
65 | /** |
||
66 | * \brief (<i>Internal</i>)Process a message checking if it trigger any MessageCommand. |
||
67 | * @param string $message Message to process. |
||
68 | * @return bool True if the message trigger any command. |
||
69 | */ |
||
70 | View Code Duplication | protected function processMessageCommand(array $message) : bool |
|
89 | } |
||
90 |
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.