Passed
Push — main ( 1bfc55...29d6ba )
by Fractal
02:09
created

CanExtractContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 24
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 18 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\MetricsPower\Logger\Traits;
17
18
use FRZB\Component\MetricsPower\Attribute\OptionsInterface;
19
use FRZB\Component\MetricsPower\Helper\ClassHelper;
20
use FRZB\Component\MetricsPower\Logger\Data\Context;
21
use Symfony\Component\Serializer\Encoder\JsonEncoder;
22
use Symfony\Component\Serializer\SerializerInterface;
23
24
trait CanExtractContext
25
{
26
    public function __construct(
27
        private readonly SerializerInterface $serializer,
28
    ) {}
29
30
    public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

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

30
    public function extract(mixed $target, ?OptionsInterface $options = null, /** @scrutinizer ignore-unused */ ?\Throwable $exception = null): Context

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $context = [
33
            'target_class' => ClassHelper::getShortName($target),
34
            'message_class' => ClassHelper::getShortName($target->getEnvelope()->getMessage()),
35
        ];
36
37
        if ($options?->isSerializable()) {
38
            $context += ['message_values' => $this->serializer->serialize($target->getEnvelope()->getMessage(), JsonEncoder::FORMAT)];
39
        }
40
41
        if ($options) {
42
            $context += [
43
                'options_class' => ClassHelper::getShortName($options),
44
            ];
45
        }
46
47
        return new Context(self::MESSAGE, $context);
0 ignored issues
show
Bug introduced by
The constant FRZB\Component\MetricsPo...ExtractContext::MESSAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
    }
49
}
50