Completed
Push — master ( 5edef7...e95395 )
by Mattias
02:05
created

Bot::botCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace TelegramBot;
4
5
use TelegramBot\APIMessage;
6
7
class Bot implements BotInterface
8
{
9
    use \League\Event\EmitterTrait;
10
11
  /** @var APIPollClient */
12
  private $client;
13
14
  /**
15
   * @param APIClient $client
16
   */
17 7
  public function __construct(APIPollClient $client)
18
  {
19 7
      $this->client = $client;
20 7
  }
21
22 1
    public function poll()
23
    {
24 1
        $this->client->poll([$this, '_handlePollResponse']);
0 ignored issues
show
Documentation introduced by
array($this, '_handlePollResponse') is of type array<integer,this<Teleg...t\\Bot>","1":"string"}>, but the function expects a object<TelegramBot\CallableInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25 1
    }
26
27
  /**
28
   * @param \React\HttpClient\Response $response
29
   */
30 1
  public function _handlePollResponse(\React\HttpClient\Response $response)
31
  {
32 1
      $response->on('data', [$this, '_handlePollData']);
33 1
      $response->on('error', [$this, '_handlePollError']);
34 1
  }
35
36
  /**
37
   * @param array $data
38
   * @param \React\HttpClient\Response $response
39
   */
40 2
  public function _handlePollData($data, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
  {
42 2
      $data = json_decode($data, 1);
43 2
      $messageData = $data['result'];
44
45 2
      foreach ($messageData as $message) {
46 2
          $apiMessage = new APIMessage($message);
47 2
          $this->client->markMessageHandled($apiMessage);
48
49 2
          if (!$apiMessage->hasText()) {
50 1
              continue;
51
          }
52
53 1
          $this->getEmitter()->emit(
54 1
        $apiMessage->getText(),
55 1
        ['message' => $message, 'responder' => $this->getResponder($apiMessage)]
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with array('message' => $mess...Responder($apiMessage)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
      );
57
      }
58 2
  }
59
60
  /**
61
   * @param \React\HttpClient\Response $response
62
   */
63 1
  public function _handlePollError(\React\HttpClient\Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
  {
65 1
      throw new \Exception();
66
  }
67
68
  /**
69
   * @param APIMessage $message
70
   * @return function
71
   */
72
  public function getResponder(APIMessage $message)
73
  {
74 1
      return function ($text) use ($message) {
75
          $this->client->send($text, $message);
76 1
      };
77
  }
78
}
79