TransportFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 7.608

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 1
cts 5
cp 0.2
crap 7.608
rs 10
1
<?php
2
3
namespace BenTools\MercurePHP\Transport;
4
5
use React\Promise\PromiseInterface;
6
7
final class TransportFactory implements TransportFactoryInterface
8
{
9
    /**
10
     * @var TransportFactoryInterface[]
11
     */
12
    private iterable $factories;
13
14
    public function __construct(iterable $factories)
15
    {
16
        $this->factories = $factories;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factories of type iterable is incompatible with the declared type BenTools\MercurePHP\Tran...sportFactoryInterface[] of property $factories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17
    }
18 1
19
    public function supports(string $dsn): bool
20 1
    {
21 1
        foreach ($this->getFactories() as $factory) {
22
            if ($factory->supports($dsn)) {
23 1
                return true;
24
            }
25 1
        }
26
27
        return false;
28
    }
29
30
    public function create(string $dsn): PromiseInterface
31 1
    {
32
        foreach ($this->getFactories() as $factory) {
33
            if (!$factory->supports($dsn)) {
34
                continue;
35
            }
36
37
            return $factory->create($dsn);
38
        }
39
40
        throw new \RuntimeException(\sprintf('Invalid transport DSN %s', $dsn));
41
    }
42
43
    private function getFactories(): array
44
    {
45
        if (\is_array($this->factories)) {
46
            return $this->factories;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factories returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
47
        }
48
49
        $factories = [];
50
        foreach ($this->factories as $factory) {
51
            if ($factory === $this) {
52
                continue;
53
            }
54
            $factories[] = $factory;
55
        }
56
        return $this->factories = (fn(TransportFactoryInterface ...$factories) => $factories)(...$factories);
57
    }
58
}
59