Passed
Push — main ( 2dd0b4...7b6409 )
by Fractal
03:40 queued 12s
created

ParametersExtractor::mapProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Extractor;
6
7
use Fp\Collections\ArrayList;
8
use Fp\Collections\Entry;
9
use Fp\Collections\HashMap;
10
use FRZB\Component\DependencyInjection\Attribute\AsService;
11
use FRZB\Component\PhpDocReader\Exception\ReaderException;
12
use FRZB\Component\PhpDocReader\Reader\ReaderInterface as PhpDocReader;
13
use FRZB\Component\RequestMapper\Helper\ClassHelper;
14
use FRZB\Component\RequestMapper\Helper\ConstraintsHelper;
15
use FRZB\Component\RequestMapper\Helper\PropertyHelper;
16
use FRZB\Component\RequestMapper\Helper\SerializerHelper;
17
18
#[AsService]
19
class ParametersExtractor
20
{
21 19
    public function __construct(
22
        private readonly PhpDocReader $reader,
23
    ) {
24
    }
25
26 19
    public function extract(string $class, array $parameters): array
27
    {
28 19
        return [...$parameters, ...$this->mapProperties(PropertyHelper::getMapping($class, $parameters, $this->reader), $parameters)];
29
    }
30
31 19
    private function mapProperties(array $properties, array $parameters): array
32
    {
33 19
        return HashMap::collect($properties)
34 19
            ->map(fn (Entry $e) => match (true) {
35 19
                \is_array($e->value) => $this->mapPropertiesForArray($e->key, $e->value, $parameters),
36 19
                ClassHelper::isNotBuiltinAndExists($e->value) => $this->extract($e->value, $parameters[$e->key] ?? []),
37 19
                ClassHelper::isEnum($e->value) => $this->mapEnum($e->value, $parameters[$e->key] ?? null),
38 19
                !ClassHelper::isNotBuiltinAndExists($e->value) => $parameters[$e->key] ?? null,
39 19
                default => null,
40
            })
41 19
            ->toAssocArray()
42 19
            ->getOrElse([])
43
        ;
44
    }
45
46
    private function mapEnum(string $enumClassName, mixed $value = null): ?\BackedEnum
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
    {
48
        return match (true) {
49
            is_subclass_of($enumClassName, \IntBackedEnum::class) && \is_int($value) => $enumClassName::tryFrom($value),
0 ignored issues
show
Bug introduced by
The type IntBackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
            is_subclass_of($enumClassName, \StringBackedEnum::class) && \is_string($value) => $enumClassName::tryFrom($value),
0 ignored issues
show
Bug introduced by
The type StringBackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
            default => null,
52
        };
53
    }
54
55
    private function mapPropertiesForArray(string $key, array $value, array $parameters): array
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

55
    private function mapPropertiesForArray(string $key, /** @scrutinizer ignore-unused */ array $value, array $parameters): array

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...
56
    {
57
        return HashMap::collect($parameters[$key] ?? [])
58
            ->map(static fn (Entry $e) => $e->value[$key])
59
            ->map(fn (Entry $e) => $this->extract($e->value, $parameters[$key][$e->key] ?? []))
60
            ->toAssocArray()
61
            ->get()
62
        ;
63
    }
64
}
65