Test Failed
Push — main ( ab57df...39c21d )
by Fractal
03:48
created

RequestBody::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 4
nop 7
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Attribute;
6
7
use FRZB\Component\RequestMapper\Helper\PropertyHelper;
8
use JetBrains\PhpStorm\Immutable;
9
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
10
11
/** @final */
12
#[Immutable]
13
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
14
class RequestBody
15
{
16
    public const DEFAULT_VALIDATION_GROUPS = ['Default'];
17
    public const DEFAULT_SERIALIZER_CONTEXT = [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
18
19
    public readonly array $validationGroups;
20
    public readonly array $serializerContext;
21
22
    public function __construct(
23
        public readonly ?string $requestClass = null,
24
        public readonly ?string $argumentName = null,
25
        public readonly bool $isValidationNeeded = true,
26
        array $serializerContext = [],
27
        array $validationGroups = [],
28
        bool $useDefaultValidationGroup = true,
29
        bool $useDefaultSerializerContext = true,
30
    ) {
31
        $this->validationGroups = $useDefaultValidationGroup ? [...$validationGroups, ...self::DEFAULT_VALIDATION_GROUPS] : $validationGroups;
0 ignored issues
show
Bug introduced by
The property validationGroups is declared read-only in FRZB\Component\RequestMapper\Attribute\RequestBody.
Loading history...
32
        $this->serializerContext = $useDefaultSerializerContext ? [...$serializerContext, ...self::DEFAULT_SERIALIZER_CONTEXT] : $serializerContext;
0 ignored issues
show
Bug introduced by
The property serializerContext is declared read-only in FRZB\Component\RequestMapper\Attribute\RequestBody.
Loading history...
33
    }
34
35
    public function equals(object $object): bool
36
    {
37
        return match (true) {
38
            $object instanceof \ReflectionParameter => PropertyHelper::getTypeName($object) === $this->requestClass && $object->getName() === $this->argumentName,
39
            $object instanceof self => $object->requestClass === $this->requestClass && $object->argumentName === $this->argumentName,
40
            default => false
41
        };
42
    }
43
}
44