Passed
Push — main ( a74a35...66bb0f )
by Fractal
03:01
created

ParametersExtractor::getPropertyMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0065

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 2
dl 0
loc 26
ccs 15
cts 17
cp 0.8824
crap 2.0065
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Extractor;
6
7
use Fp\Collections\Entry;
8
use Fp\Collections\HashMap;
9
use FRZB\Component\DependencyInjection\Attribute\AsService;
10
use FRZB\Component\PhpDocReader\Reader\ReaderInterface as PhpDocReader;
11
use FRZB\Component\RequestMapper\Helper\ClassHelper;
12
use FRZB\Component\RequestMapper\Helper\PropertyHelper;
13
14
#[AsService]
15
class ParametersExtractor
16
{
17 19
    public function __construct(
18
        private readonly PhpDocReader $reader,
19
    ) {
20
    }
21
22 19
    public function extract(string $class, array $parameters): array
23
    {
24 19
        return [...$parameters, ...$this->mapProperties(PropertyHelper::getMapping($class, $parameters, $this->reader), $parameters)];
25
    }
26
27 19
    private function mapProperties(array $properties, array $parameters): array
28
    {
29 19
        return HashMap::collect($properties)
30 19
            ->map(fn (Entry $e) => match (true) {
31 19
                \is_array($e->value) => $this->mapPropertiesForArray($e->key, $e->value, $parameters),
32 19
                ClassHelper::isNotBuiltinAndExists($e->value) => $this->extract($e->value, $parameters[$e->key] ?? []),
33 19
                ClassHelper::isEnum($e->value) => $this->mapEnum($e->value, $parameters[$e->key] ?? null),
34 19
                !ClassHelper::isNotBuiltinAndExists($e->value) => $parameters[$e->key] ?? null,
35 19
                default => null,
36
            })
37 19
            ->toAssocArray()
38 19
            ->getOrElse([])
39
        ;
40
    }
41
42
    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...
43
    {
44
        return match (true) {
45
            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...
46
            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...
47
            default => null,
48
        };
49
    }
50
51
    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

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