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 ( 5ac723...c719eb )
by Stan
07:02
created

Response::buildEntities()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 9
nop 2
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\Api;
12
13
use Teebot\Api\Entity\Update;
14
use Teebot\Api\Exception\BuildEntityException;
15
use Teebot\Api\Exception\DecodingDataException;
16
use Teebot\Api\Entity\AbstractEntity;
17
use Teebot\Api\Entity\Error;
18
19
class Response
20
{
21
    const DEFAULT_ENTITY_TYPE = Update::class;
22
23
    protected $decodedData = [];
24
25
    protected $lastUpdate = 0;
26
27
    protected $entities = [];
28
29
    protected $parent;
30
31
    /**
32
     * Creates an instanse of Response class from Raw JSON string and builds corresponding entity
33
     * if passed in entity class. This class should be instantiated for every JSON response from Telegram.
34
     *
35
     * @param string              $rawData     Raw JSON string
36
     * @param null|string         $entityClass Entity class that should be instantiated with decoded JSON data
37
     * @param null|AbstractEntity $parent      Parent class should be set as parent for newly instantiated entity
38
     *
39
     * @throws DecodingDataException
40
     */
41
    public function __construct($rawData, $entityClass = null, $parent = null)
42
    {
43
        $this->parent      = $parent;
44
        $this->decodedData = $this->decodeData($rawData);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->decodeData($rawData) of type * is incompatible with the declared type array of property $decodedData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
46
        if (empty($this->decodedData)) {
47
            throw new DecodingDataException('Error decoding data!');
48
        }
49
50
        $entityClass    = $this->isErrorReceived() ? Error::class : $entityClass;
51
        $entitiesSource = $this->getRawEntitiesList();
52
        $this->entities = $this->buildEntities($entitiesSource, $entityClass);
53
    }
54
55
    /**
56
     * Returns JSON data decoded as an array.
57
     *
58
     * @param string $rawData JSON string
59
     *
60
     * @return array|mixed
61
     */
62
    protected function decodeData($rawData)
63
    {
64
        if (!is_string($rawData) || !strlen($rawData)) {
65
            return [];
66
        }
67
68
        try {
69
            $result = json_decode($rawData, true);
70
        } catch (\Exception $e) {
71
            return [];
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * Checks whether an error response from Telegram was received.
79
     *
80
     * @return bool
81
     */
82
    public function isErrorReceived()
83
    {
84
        if ((isset($this->decodedData['ok']) && $this->decodedData['ok'] === true)) {
85
            return false;
86
        }
87
88
        return true;
89
    }
90
91
    /**
92
     * Builds entity objects array from an array of raw entity data. Returns an array with built entities.
93
     *
94
     * @param array       $rawData     Raw entity data array
95
     * @param null|string $entityClass Entity class
96
     *
97
     * @return array
98
     */
99
    protected function buildEntities(array $rawData, $entityClass = null)
100
    {
101
        $entities = [];
102
        $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...
103
104
        if (empty($rawData)) {
105
            return $entities;
106
        }
107
108
        if (!isset($rawData[0])) {
109
            $rawData = [$rawData];
110
        }
111
112
        foreach ($rawData as $rawItemData) {
113
            if (empty($rawItemData)) {
114
                continue;
115
            }
116
117
            $entity = $this->buildEntity($rawItemData, $entityClass);
118
119
            if ($entity && $entity instanceof Update) {
120
                $this->lastUpdate = (int) $entity->getUpdateId();
121
            }
122
123
            $entities[] = $entity;
124
        }
125
126
        return $entities;
127
    }
128
129
    /**
130
     * Builds desired entity from raw entity's data array. Returns class entity
131
     *
132
     * @param array       $rawItemData Array with raw entity's data
133
     * @param null|string $entityClass Entity class to instantiate, if not passed - default Entity class will be used.
134
     *
135
     * @return AbstractEntity
136
     *
137
     * @throws BuildEntityException
138
     */
139
    protected function buildEntity(array $rawItemData, $entityClass = null)
140
    {
141
        $entityClass = $entityClass ? $entityClass : static::DEFAULT_ENTITY_TYPE;
142
        $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...
143
144
        if (!class_exists($entityClass)) {
145
            throw new BuildEntityException('Entity "' . $entityClass . '" does not exists or not supported yet!');
146
        }
147
148
        /** @var AbstractEntity $entity */
149
        $entity = new $entityClass($rawItemData);
150
        $entity->setParent($this->parent);
151
152
        return $entity;
153
    }
154
155
    /**
156
     * Returns an array with entities
157
     *
158
     * @return array
159
     */
160
    public function getEntities()
161
    {
162
        return $this->entities;
163
    }
164
165
    /**
166
     * Returns an entity at certain offset or null if entity does not exists.
167
     *
168
     * @param int $offset Offset
169
     *
170
     * @return mixed|null
171
     */
172
    public function getEntityByOffset($offset = 0)
173
    {
174
        return is_array($this->entities) && isset($this->entities[$offset]) ? $this->entities[$offset] : null;
175
    }
176
177
    /**
178
     * Returns first entity. More meaningful wrapper for self::getEntityByOffset(0) method.
179
     *
180
     * @return mixed|null
181
     */
182
    public function getFirstEntity()
183
    {
184
        return $this->getEntityByOffset();
185
    }
186
187
    /**
188
     * Returns raw entities list from decoded JSON data array.
189
     *
190
     * @return array
191
     */
192
    protected function getRawEntitiesList()
193
    {
194
        if (!is_array($this->decodedData) || empty($this->decodedData)) {
195
            return [];
196
        }
197
198
        if (isset($this->decodedData['result']) && is_array($this->decodedData['result'])) {
199
            return $this->decodedData['result'];
200
        }
201
202
        return $this->decodedData;
203
    }
204
205
    /**
206
     * Returns count of entities.
207
     *
208
     * @return int
209
     */
210
    public function getEntitiesCount()
211
    {
212
        return count($this->entities);
213
    }
214
215
    /**
216
     * Returns last update id gathered from the last Update entity.
217
     *
218
     * @return int
219
     */
220
    public function getLastUpdate()
221
    {
222
        return $this->lastUpdate;
223
    }
224
225
    /**
226
     * Returns an offset to skip previous updates in the listener mode.
227
     *
228
     * @return int
229
     */
230
    public function getOffset()
231
    {
232
        return $this->lastUpdate + 1;
233
    }
234
}
235