Completed
Push — master ( 20255a...21e228 )
by Camilo
06:16
created

MessageArray::traverseObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace unreal4u\TelegramAPI\Telegram\Types\Custom;
5
6
use unreal4u\TelegramAPI\Abstracts\CustomType;
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
9
use unreal4u\TelegramAPI\Telegram\Types\Message;
10
11
/**
12
 * Used for methods that will return an array of messages
13
 */
14
class MessageArray extends CustomType implements CustomArrayType
15
{
16
    public function __construct(array $data = null, LoggerInterface $logger = null)
17
    {
18
        if (count($data) !== 0) {
19
            foreach ($data as $telegramResponse) {
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...
20
                // Create an actual Update object and fill the array
21
                $this->data[] = new Message($telegramResponse, $logger);
22
            }
23
        }
24
    }
25
26
    /**
27
     * Traverses through our $data, yielding the result set
28
     *
29
     * @return Update[]
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 $message) {
34
            yield $message;
35
        }
36
    }
37
}
38