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 |
||
| 15 | class Client |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * RegExp for bot commands |
||
| 19 | */ |
||
| 20 | const REGEXP = '/^(?:@\w+\s)?\/([^\s@]+)(@\S+)?\s?(.*)$/'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \TelegramBot\Api\BotApi |
||
| 24 | */ |
||
| 25 | protected $api; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \TelegramBot\Api\Events\EventCollection |
||
| 29 | */ |
||
| 30 | protected $events; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Client constructor |
||
| 34 | * |
||
| 35 | * @param string $token Telegram Bot API token |
||
| 36 | * @param string|null $trackerToken Yandex AppMetrica application api_key |
||
| 37 | */ |
||
| 38 | 7 | public function __construct($token, $trackerToken = null) |
|
| 39 | { |
||
| 40 | 7 | $this->api = new BotApi($token); |
|
| 41 | 7 | $this->events = new EventCollection($trackerToken); |
|
| 42 | 7 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Use this method to add command. Parameters will be automatically parsed and passed to closure. |
||
| 46 | * |
||
| 47 | * @param string $name |
||
| 48 | * @param \Closure $action |
||
| 49 | * |
||
| 50 | * @return \TelegramBot\Api\Client |
||
| 51 | */ |
||
| 52 | public function command($name, Closure $action) |
||
| 56 | |||
| 57 | public function channelPost(Closure $action) |
||
| 61 | |||
| 62 | public function inlineQuery(Closure $action) |
||
| 66 | |||
| 67 | public function shippingQuery(Closure $action) |
||
| 71 | |||
| 72 | public function preCheckoutQuery(Closure $action) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Use this method to add an event. |
||
| 79 | * If second closure will return true (or if you are passed null instead of closure), first one will be executed. |
||
| 80 | * |
||
| 81 | * @param \Closure $event |
||
| 82 | * @param \Closure|null $checker |
||
| 83 | * |
||
| 84 | * @return \TelegramBot\Api\Client |
||
| 85 | */ |
||
| 86 | 1 | public function on(Closure $event, Closure $checker = null) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Handle updates |
||
| 95 | * |
||
| 96 | * @param Update[] $updates |
||
| 97 | */ |
||
| 98 | 4 | public function handle(array $updates) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Webhook handler |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | * @throws \TelegramBot\Api\InvalidJsonException |
||
| 111 | */ |
||
| 112 | public function run() |
||
| 118 | |||
| 119 | public function getRawBody() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns event function to handling the command. |
||
| 126 | * |
||
| 127 | * @param \Closure $action |
||
| 128 | * |
||
| 129 | * @return \Closure |
||
| 130 | */ |
||
| 131 | 4 | protected static function getEvent(Closure $action) |
|
| 158 | |||
| 159 | View Code Duplication | protected static function getChannelPostEvent(Closure $action) |
|
| 171 | |||
| 172 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
| 184 | |||
| 185 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
| 197 | |||
| 198 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Returns check function to handling the command. |
||
| 213 | * |
||
| 214 | * @param string $name |
||
| 215 | * |
||
| 216 | * @return \Closure |
||
| 217 | */ |
||
| 218 | 4 | protected static function getChecker($name) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Returns check function to handling the inline queries. |
||
| 234 | * |
||
| 235 | * @return Closure |
||
| 236 | */ |
||
| 237 | protected static function getChannelPostChecker() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns check function to handling the inline queries. |
||
| 246 | * |
||
| 247 | * @return Closure |
||
| 248 | */ |
||
| 249 | 4 | protected static function getInlineQueryChecker() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns check function to handling the shipping queries. |
||
| 258 | * |
||
| 259 | * @return Closure |
||
| 260 | */ |
||
| 261 | protected static function getShippingQueryChecker() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns check function to handling the pre checkout queries. |
||
| 270 | * |
||
| 271 | * @return Closure |
||
| 272 | */ |
||
| 273 | protected static function getPreCheckoutQueryChecker() |
||
| 279 | |||
| 280 | 1 | public function __call($name, array $arguments) |
|
| 289 | } |
||
| 290 |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope