Test Failed
Pull Request — master (#1102)
by Aleksei
10:09
created

LoggerInjector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Logger;
6
7
use Psr\Log\LoggerInterface;
8
use Spiral\Core\Attribute\Proxy;
9
use Spiral\Core\Container\InjectorInterface;
10
use Spiral\Logger\Attribute\LoggerChannel;
11
12
/**
13
 * Container injector for {@see LoggerInterface}.
14
 * Supports {@see LoggerChannel} attribute.
15
 *
16
 * @implements InjectorInterface<LoggerInterface>
17
 */
18
final class LoggerInjector implements InjectorInterface
19
{
20
    public function __construct(
21
        #[Proxy]
22
        private readonly LogsInterface $factory
23
    ) {
24
    }
25
26
    public function createInjection(
27
        \ReflectionClass $class,
28
        \ReflectionParameter|null|string $context = null,
29
    ): LoggerInterface {
30
        $channel = \is_object($context) ? $this->extractChannelAttribute($context) : null;
31
32
        // always return default logger as injection
33
        return $this->factory->getLogger($channel);
0 ignored issues
show
Bug introduced by
It seems like $channel can also be of type null; however, parameter $channel of Spiral\Logger\LogsInterface::getLogger() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        return $this->factory->getLogger(/** @scrutinizer ignore-type */ $channel);
Loading history...
34
    }
35
36
    /**
37
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
38
     */
39
    private function extractChannelAttribute(\ReflectionParameter $parameter): ?string
40
    {
41
        /** @var ReflectionAttribute<LoggerChannel>[] $attributes */
42
        $attributes = $parameter->getAttributes(LoggerChannel::class);
43
44
        return $attributes[0]?->newInstance()->name;
45
    }
46
}
47