Completed
Push — master ( 54e92f...46abfb )
by Camilo
04:46
created

SetGameScore   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bindToObject() 0 12 3
A getMandatoryFields() 0 7 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Telegram\Methods;
6
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
9
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
10
use unreal4u\TelegramAPI\Exceptions\InvalidResultType;
11
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
12
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;
13
use unreal4u\TelegramAPI\Telegram\Types\Message;
14
15
/**
16
 * Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot,
17
 * returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's
18
 * current score in the chat and force is False
19
 *
20
 * Objects defined as-is January 2017
21
 *
22
 * @see https://core.telegram.org/bots/api#setgamescore
23
 */
24
class SetGameScore extends TelegramMethods
25
{
26
    /**
27
     * User identifier
28
     * @var int
29
     */
30
    public $user_id = 0;
31
32
    /**
33
     * New score, must be non-negative
34
     * @var int
35
     */
36
    public $score = 0;
37
38
    /**
39
     * Pass True, if the high score is allowed to decrease. This can be useful when fixing mistakes or banning cheaters
40
     * @var boolean
41
     */
42
    public $force = false;
43
44
    /**
45
     * Pass True, if the game message should not be automatically edited to include the current scoreboard
46
     * @var boolean
47
     */
48
    public $disable_edit_message = false;
49
50
    /**
51
     * Required if inline_message_id is not specified. Unique identifier for the target chat
52
     * @var int
53
     */
54
    public $chat_id;
55
56
    /**
57
     * Required if inline_message_id is not specified. Identifier of the sent message
58
     * @var int
59
     */
60
    public $message_id;
61
62
    /**
63
     * Required if chat_id and message_id are not specified. Identifier of the inline message
64
     * @var string
65
     */
66
    public $inline_message_id;
67
68
    public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
69
    {
70
        $typeOfResult = $data->getTypeOfResult();
71
        switch ($typeOfResult) {
72
            case 'array':
73
                return new Message($data->getResult(), $logger);
74
            case 'boolean':
75
                return new ResultBoolean($data->getResultBoolean(), $logger);
76
            default:
77
                throw new InvalidResultType('Result is of type: %s. Expecting one of array or boolean');
78
        }
79
    }
80
81
    public function getMandatoryFields(): array
82
    {
83
        // user_id and score are always mandatory
84
        $returnValue[] = 'user_id';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$returnValue was never initialized. Although not strictly required by PHP, it is generally a good practice to add $returnValue = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
85
        $returnValue[] = 'score';
86
        return $this->mandatoryUserOrInlineMessageId($returnValue);
87
    }
88
}
89