Passed
Pull Request — master (#756)
by Maxim
07:24
created

AbstractBroadcast   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 31
ccs 8
cts 9
cp 0.8889
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 11 3
A formatTopics() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Broadcasting\Driver;
6
7
use Spiral\Broadcasting\BroadcastInterface;
8
9
abstract class AbstractBroadcast implements BroadcastInterface
10
{
11
    /**
12
     * Format the topic array into an array of strings.
13
     *
14
     * @param string[]|\Stringable[] $topics
15
     * @return string[]
16
     */
17 5
    protected function formatTopics(array $topics): array
18
    {
19 5
        return array_map(function ($topic) {
20 5
            return (string)$topic;
21
        }, $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