Completed
Push — master ( dcfb29...f47acd )
by Camilo
04:11
created

StickerSetArray::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\Telegram\Types\Sticker;
9
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * Mockup class to generate a real telegram update representation
14
 */
15
class StickerSetArray extends CustomType implements CustomArrayType
16
{
17
    public function __construct(array $data = null, LoggerInterface $logger = null)
18
    {
19
        if (count($data) !== 0) {
20
            foreach ($data as $id => $sticker) {
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...
21
                $this->data[$id] = new Sticker($sticker, $logger);
22
            }
23
        }
24
    }
25
26
    /**
27
     * Traverses through our $data, yielding the result set
28
     *
29
     * @return Sticker[]
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
     */
31
    public function traverseObject()
32
    {
33
        foreach ($this->data as $sticker) {
34
            yield $sticker;
35
        }
36
    }
37
}
38