Test Failed
Pull Request — master (#1236)
by Aleksei
12:18
created

Proxy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
eloc 12
c 2
b 1
f 0
dl 0
loc 53
ccs 11
cts 13
cp 0.8462
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getInterface() 0 3 1
A hasFactory() 0 3 1
A getReturnClass() 0 3 1
A __construct() 0 13 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Config;
6
7
use Psr\Container\ContainerInterface;
8
9
class Proxy extends Binding
10
{
11
    private readonly bool $hasFactory;
12
    /**
13
     * @template T
14
     * @param class-string<T> $interface
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
15
     * @param null|\Closure(ContainerInterface, \Stringable|string|null): T $fallbackFactory Factory that will be used
16
     *        to create an instance if the value is resolved from a proxy.
17 398
     */
18
    public function __construct(
19
        protected readonly string $interface,
20
        public readonly bool $singleton = false,
21
        public readonly ?\Closure $fallbackFactory = null,
22 398
    ) {
23 398
        \interface_exists($interface) or throw new \InvalidArgumentException(
24 398
            "Interface `{$interface}` does not exist.",
25 397
        );
26 397
        $this->singleton and $this->fallbackFactory !== null and throw new \InvalidArgumentException(
27 397
            'Singleton proxies must not have a fallback factory.',
28
        );
29
        $this->hasFactory = $fallbackFactory !== null && (new \ReflectionFunction($fallbackFactory))
0 ignored issues
show
Bug introduced by
The property hasFactory is declared read-only in Spiral\Core\Config\Proxy.
Loading history...
30
                ->getReturnType()->__toString() !== 'never';
31
    }
32
33
    /**
34
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
35
     * @deprecated Use {@see getReturnClass()} instead.
36
     */
37
    public function getInterface(): string
38
    {
39
        return $this->interface;
40
    }
41
42
    /**
43 393
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
44
     * @internal
45 393
     */
46
    public function getReturnClass(): string
47
    {
48 1
        return $this->interface;
49
    }
50 1
51
    /**
52
     * @return bool Returns {@see true} if the factory is presented, and it doesn't have the {@see never} type.
53
     */
54
    public function hasFactory(): bool
55
    {
56
        return $this->hasFactory;
57
    }
58
59
    public function __toString(): string
60
    {
61
        return \sprintf('Proxy to `%s`', $this->interface);
62
    }
63
}
64