MetricsHandlerFactory::getFactories()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 14
rs 10
ccs 4
cts 5
cp 0.8
crap 4.128
1
<?php
2
3
namespace BenTools\MercurePHP\Metrics;
4
5
use React\Promise\PromiseInterface;
6
7
final class MetricsHandlerFactory implements MetricsHandlerFactoryInterface
8
{
9
    /**
10
     * @var MetricsHandlerFactoryInterface[]
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\Metr...ndlerFactoryInterface[] 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 2
19
    public function supports(string $dsn): bool
20 2
    {
21 2
        foreach ($this->getFactories() as $factory) {
22
            if ($factory->supports($dsn)) {
23 1
                return true;
24
            }
25 1
        }
26 1
27 1
        return false;
28
    }
29
30
    public function create(string $dsn): PromiseInterface
31
    {
32
        foreach ($this->getFactories() as $factory) {
33
            if (!$factory->supports($dsn)) {
34 1
                continue;
35
            }
36 1
37 1
            return $factory->create($dsn);
38
        }
39
40
        throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn));
41 1
    }
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 2
        }
48
49 2
        $factories = [];
50
        foreach ($this->factories as $factory) {
51 2
            if ($factory === $this) {
52 2
                continue;
53
            }
54
            $factories[] = $factory;
55
        }
56
        return $this->factories = (fn(MetricsHandlerFactoryInterface ...$factories) => $factories)(...$factories);
57
    }
58
}
59