Streams::wrap()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
nc 6
nop 1
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace Bdf\Collection\Stream;
4
5
use Traversable;
6
use function is_array;
7
8
/**
9
 * Utility class for handle streams
10
 */
11
final class Streams
12
{
13
    /**
14
     * Wrap a value into a stream
15
     * The best stream implementation is used according to the value type :
16
     *
17
     * - If the value is a Streamable object, return the related stream
18
     * - If the value is null or an empty array, return an EmptyStream
19
     * - If the value is an array, return an ArrayStream
20
     * - If the value is Traversable, return an IteratorStream
21
     * - In other cases, return a SingletonStream
22
     *
23
     * @template V
24
     *
25
     * @param V|V[]|Traversable<mixed, V> $value The value to wrap
0 ignored issues
show
Bug introduced by
The type Bdf\Collection\Stream\V was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     *
27
     * @return StreamInterface<V, mixed>
28
     */
29 37
    public static function wrap($value): StreamInterface
30
    {
31 37
        if ($value instanceof StreamInterface) {
32 1
            return $value;
33
        }
34
35 37
        if ($value instanceof Streamable) {
36 1
            return $value->stream();
37
        }
38
39 37
        if ($value === null || $value === []) {
40 2
            return EmptyStream::instance();
41
        }
42
43 37
        if (is_array($value)) {
44 37
            return new ArrayStream($value);
45
        }
46
47 3
        if ($value instanceof Traversable) {
0 ignored issues
show
introduced by
$value is always a sub-type of Traversable.
Loading history...
48 1
            return new IteratorStream($value);
49
        }
50
51 3
        return new SingletonStream($value);
52
    }
53
}
54