Passed
Push — main ( a74a35...8a07cd )
by Fractal
03:04
created

ClassMapper::setMapperLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Mapper;
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
use FRZB\Component\RequestMapper\Mapper\MapperLocatorInterface as MapperLocator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FRZB\Component\RequestMapper\Mapper\MapperLocator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
15
16
#[AutoconfigureTag(MapperInterface::class)]
17
#[AsService(calls: ['setMapperLocator' => [MapperLocator::class]])]
18
class ClassMapper implements MapperInterface
19
{
20
    private readonly MapperLocator $mapperLocator;
21
22 19
    public function __construct(
23
        private readonly PhpDocReader $reader,
24
    ) {
25
    }
26
27 19
    public function __invoke(string $typeName, mixed $value, array $parameters = []): mixed
28
    {
29 19
        return HashMap::collect(PropertyHelper::getMapping($typeName, $parameters, $this->reader))
30 19
            ->map(fn (Entry $e) => match (true) {
31 19
                \is_array($e->value) => $this->mapperLocator->get($e->value, $value)($e->value, $e->value, $parameters),
32 19
                default => $this->mapperLocator->get($e->value, $value[$e->key] ?? null)($e->value, $value[$e->key] ?? null, $parameters)
33
            })
34 19
            ->toAssocArray()
35 19
            ->getOrElse([])
36
        ;
37
    }
38
39 19
    public function canMap(string $typeName, mixed $value = null): bool
40
    {
41 19
        return \is_array($value) && ClassHelper::isNotBuiltinAndExists($typeName);
42
    }
43
44 1
    public static function getPriority(): int
45
    {
46 1
        return 3;
47
    }
48
49 19
    public function setMapperLocator(MapperLocator $mapperLocator): void
50
    {
51 19
        $this->mapperLocator = $mapperLocator;
0 ignored issues
show
Bug introduced by
The property mapperLocator is declared read-only in FRZB\Component\RequestMapper\Mapper\ClassMapper.
Loading history...
52
    }
53
}
54