GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 64a210...2d6991 )
by Stan
02:42
created

Response   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4
Metric Value
wmc 31
lcom 3
cbo 4
dl 0
loc 210
rs 9.8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A decodeData() 0 8 3
A isErrorReceived() 0 8 3
C buildEntities() 0 33 8
A buildEntity() 0 14 2
A getEntities() 0 4 1
A getEntityByOffset() 0 4 3
A getFirstEntity() 0 4 1
A getRawEntitiesList() 0 12 4
A getEntitiesCount() 0 4 1
A getLastUpdate() 0 4 1
A getOffset() 0 4 1
1
<?php
2
3
/**
4
 * Response class which should be instantiated from received or passed raw JSON string.
5
 *
6
 * @package Teebot (Telegram bot framework)
7
 *
8
 * @author Stanislav Drozdov <[email protected]>
9
 */
10
11
namespace Teebot;
12
13
use Teebot\Entity\Update;
14
use Teebot\Exception\Fatal;
15
use Teebot\Entity\AbstractEntity;
16
use Teebot\Entity\Error;
17
use Teebot\Exception\Critical;
18
use Teebot\Exception\Output;
19
20
class Response
21
{
22
    const DEFAULT_ENTITY_TYPE = Update::class;
23
24
    protected $decodedData = [];
25
26
    protected $lastUpdate = 0;
27
28
    protected $entities = [];
29
30
    protected $parent;
31
32
    /**
33
     * Creates an instanse of Response class from Raw JSON string and builds corresponding entity
34
     * if passed in entity class. This class should be instantiated for every JSON response from Telegram.
35
     *
36
     * @param string              $rawData     Raw JSON string
37
     * @param null|AbstractEntity $entityClass Entity class that should be instantiated with decoded JSON data
38
     * @param null|AbstractEntity $parent      Parent class should be set as parent for newly instantiated entity
39
     */
40
    public function __construct(string $rawData, $entityClass = null, $parent = null)
41
    {
42
        $this->parent      = $parent;
43
        $this->decodedData = $this->decodeData($rawData);
44
45
        if (empty($this->decodedData)) {
46
            Output::log(new Critical('Error decoding data!'));
47
        }
48
49
        $entityClass    = $this->isErrorReceived() ? Error::class : $entityClass;
50
        $entitiesSource = $this->getRawEntitiesList();
51
        $this->entities = $this->buildEntities($entitiesSource, $entityClass);
52
    }
53
54
    /**
55
     * Returns JSON data decoded as an array.
56
     *
57
     * @param string $rawData JSON string
58
     *
59
     * @return array|mixed
60
     */
61
    protected function decodeData(string $rawData) : array
62
    {
63
        if (!is_string($rawData) || !strlen($rawData)) {
64
            return [];
65
        }
66
67
        return json_decode($rawData, true);
68
    }
69
70
    /**
71
     * Checks whether an error response from Telegram was received.
72
     *
73
     * @return bool
74
     */
75
    public function isErrorReceived() : bool
76
    {
77
        if ((isset($this->decodedData['ok']) && $this->decodedData['ok'] === true)) {
78
            return false;
79
        }
80
81
        return true;
82
    }
83
84
    /**
85
     * Builds entity objects array from an array of raw entity data. Returns an array with built entities.
86
     *
87
     * @param array               $rawData     Raw entity data array
88
     * @param null|AbstractEntity $entityClass Entity class
89
     *
90
     * @return array
91
     */
92
    protected function buildEntities(array $rawData, $entityClass = null) : array
93
    {
94
        $entities = [];
95
        $entity   = null;
96
97
        if (empty($rawData)) {
98
            return $entities;
99
        }
100
101
        if (!isset($rawData[0])) {
102
            $rawData = [$rawData];
103
        }
104
105
        foreach ($rawData as $rawItemData) {
106
            if (empty($rawItemData)) {
107
                continue;
108
            }
109
110
            try {
111
                $entity = $this->buildEntity($rawItemData, $entityClass);
112
113
                if ($entity && $entity instanceof Update) {
114
                    $this->lastUpdate = (int) $entity->getUpdateId();
115
                }
116
117
                $entities[] = $entity;
118
            } catch (Fatal $e) {
119
                Output::log($e);
120
            }
121
        }
122
123
        return $entities;
124
    }
125
126
    /**
127
     * Builds desired entity from raw entity's data array. Returns class entity
128
     *
129
     * @param array               $rawItemData  Array with raw entity's data
130
     * @param null|AbstractEntity $entityClass  Entity class to instantiate, if not passed - default
131
     *                                          Entity class will be used.
132
     *
133
     * @return AbstractEntity
134
     */
135
    protected function buildEntity(array $rawItemData, $entityClass = null) : AbstractEntity
136
    {
137
        $entityClass = $entityClass ?? static::DEFAULT_ENTITY_TYPE;
138
        $entity      = null;
0 ignored issues
show
Unused Code introduced by
$entity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
140
        if (!class_exists($entityClass)) {
141
            Output::log(new Critical('Entity "' . $entityClass . '" does not exists or not supported yet!'));
142
        }
143
        /** @var AbstractEntity $entity */
144
        $entity = new $entityClass($rawItemData);
145
        $entity->setParent($this->parent);
0 ignored issues
show
Bug introduced by
It seems like $this->parent can be null; however, setParent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
146
147
        return $entity;
148
    }
149
150
    /**
151
     * Returns an array with entities
152
     *
153
     * @return array
154
     */
155
    public function getEntities() : array
156
    {
157
        return $this->entities;
158
    }
159
160
    /**
161
     * Returns an entity at certain offset or null if entity does not exists.
162
     *
163
     * @param int $offset Offset
164
     *
165
     * @return mixed|null
166
     */
167
    public function getEntityByOffset(int $offset = 0)
168
    {
169
        return is_array($this->entities) && isset($this->entities[$offset]) ? $this->entities[$offset] : null;
170
    }
171
172
    /**
173
     * Returns first entity. More meaningful wrapper for self::getEntityByOffset(0) method.
174
     *
175
     * @return mixed|null
176
     */
177
    public function getFirstEntity()
178
    {
179
        return $this->getEntityByOffset();
180
    }
181
182
    /**
183
     * Returns raw entities list from decoded JSON data array.
184
     *
185
     * @return array
186
     */
187
    protected function getRawEntitiesList() : array
188
    {
189
        if (!is_array($this->decodedData)) {
190
            return [];
191
        }
192
193
        if (isset($this->decodedData['result']) && is_array($this->decodedData['result'])) {
194
            return $this->decodedData['result'];
195
        }
196
197
        return $this->decodedData;
198
    }
199
200
    /**
201
     * Returns count of entities.
202
     *
203
     * @return int
204
     */
205
    public function getEntitiesCount() : int
206
    {
207
        return count($this->entities);
208
    }
209
210
    /**
211
     * Returns last update id gathered from the last Update entity.
212
     *
213
     * @return int
214
     */
215
    public function getLastUpdate() : int
216
    {
217
        return $this->lastUpdate;
218
    }
219
220
    /**
221
     * Returns an offset to skip previous updates in the listener mode.
222
     *
223
     * @return int
224
     */
225
    public function getOffset() : int
226
    {
227
        return $this->lastUpdate + 1;
228
    }
229
}
230