Test Failed
Pull Request — main (#22)
by Fractal
13:00
created

RequestBody::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
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
#[Immutable]
12
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
13
class RequestBody
14
{
15
    public const DEFAULT_VALIDATION_GROUPS = ['Default'];
16
    public const DEFAULT_SERIALIZER_CONTEXT = [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
17
18
    public readonly array $validationGroups;
19
    public readonly array $serializerContext;
20
21
    public function __construct(
22
        public readonly ?string $requestClass = null,
23
        public readonly ?string $argumentName = null,
24
        public readonly bool $isValidationNeeded = true,
25
        array $serializerContext = [],
26
        array $validationGroups = [],
27
        bool $useDefaultValidationGroup = true,
28
        bool $useDefaultSerializerContext = true,
29
    ) {
30
        $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...
31
        $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...
32
    }
33
34
    public function equals(object $object): bool
35
    {
36
        return match (true) {
37
            $object instanceof \ReflectionParameter => PropertyHelper::getTypeName($object) === $this->requestClass && $object->getName() === $this->argumentName,
38
            $object instanceof self => $object->requestClass === $this->requestClass && $object->argumentName === $this->argumentName,
39
            default => false
40
        };
41
    }
42
}
43