Passed
Pull Request — master (#1102)
by Aleksei
11:41 queued 01:17
created

LoggerInjector::createInjection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 4
    public function __construct(
21
        #[Proxy]
22
        private readonly LogsInterface $factory
23
    ) {
24 4
    }
25
26
    /**
27
     * @param \ReflectionParameter|string|null $context may use extended context if possible.
28
     */
29 4
    public function createInjection(
30
        \ReflectionClass $class,
31
        \ReflectionParameter|null|string $context = null,
32
    ): LoggerInterface {
33 4
        $channel = \is_object($context) ? $this->extractChannelAttribute($context) : null;
34
35
        // always return default logger as injection
36 4
        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

36
        return $this->factory->getLogger(/** @scrutinizer ignore-type */ $channel);
Loading history...
37
    }
38
39
    /**
40
     * @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...
41
     */
42 1
    private function extractChannelAttribute(\ReflectionParameter $parameter): ?string
43
    {
44
        /** @var \ReflectionAttribute<LoggerChannel>[] $attributes */
45 1
        $attributes = $parameter->getAttributes(LoggerChannel::class);
46
47 1
        return $attributes[0]?->newInstance()->name;
48
    }
49
}
50