Passed
Pull Request — master (#656)
by Abdul Malik
16:45
created

AbstractBroadcast::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Broadcasting\Driver;
6
7
use Stringable;
8
use Traversable;
9
use Spiral\Broadcasting\BroadcastInterface;
10
11
abstract class AbstractBroadcast implements BroadcastInterface
12
{
13
    /**
14
     * Format the topic array into an array of strings.
15
     *
16
     * @param string[]|Stringable[] $topics
17
     * @return string[]
18
     */
19 5
    protected function formatTopics(array $topics): array
20
    {
21 5
        return array_map(fn ($topic) => (string)$topic, $topics);
22
    }
23
24
    /**
25
     * @template T of mixed
26
     * @param iterable<T>|T $entries
27
     * @return array<T>
28
     */
29 5
    protected function toArray($entries): array
30
    {
31
        switch (true) {
32 5
            case \is_array($entries):
33 3
                return $entries;
34
35 4
            case $entries instanceof Traversable:
36
                return \iterator_to_array($entries, false);
37
38
            default:
39 4
                return [$entries];
40
        }
41
    }
42
}
43