Completed
Pull Request — master (#47)
by Rick
03:20
created

UserArray::traverseObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Telegram\Types\Custom;
6
7
use unreal4u\TelegramAPI\Abstracts\CustomType;
8
use unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
9
use unreal4u\TelegramAPI\Telegram\Types\User;
10
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
11
use Psr\Log\LoggerInterface;
12
13
/**
14
 * Mockup class to generate a real telegram update representation
15
 */
16
class UserArray extends TraversableCustomType
17
{
18
    public function __construct(array $data = null, LoggerInterface $logger = null)
19
    {
20
        if (count($data) !== 0) {
21
            foreach ($data as $id => $chatMember) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
22
                $this->data[$id] = new User($chatMember, $logger);
23
            }
24
        }
25
    }
26
}
27