Issues (590)

src/Serializer/PrimeCollectionNormalizer.php (1 issue)

1
<?php
2
3
namespace Bdf\Prime\Serializer;
4
5
use Bdf\Prime\Collection\CollectionFactory;
6
use Bdf\Prime\Collection\CollectionInterface;
7
use Bdf\Prime\ServiceLocator;
8
use Bdf\Serializer\Context\DenormalizationContext;
9
use Bdf\Serializer\Normalizer\AutoRegisterInterface;
10
use Bdf\Serializer\Normalizer\NormalizerLoaderInterface;
11
use Bdf\Serializer\Normalizer\TraversableNormalizer;
12
use Bdf\Serializer\Type\Type;
13
use Bdf\Serializer\Type\TypeFactory;
14
15
/**
16
 * Class PrimeCollectionNormalizer
17
 */
18
class PrimeCollectionNormalizer extends TraversableNormalizer implements AutoRegisterInterface
19
{
20
    /**
21
     * @var ServiceLocator
22
     */
23
    private $prime;
24
25
26
    /**
27
     * PrimeCollectionNormalizer constructor.
28
     *
29
     * @param ServiceLocator $prime
30
     */
31 414
    public function __construct(ServiceLocator $prime)
32
    {
33 414
        $this->prime = $prime;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 12
    public function denormalize($data, Type $type, DenormalizationContext $context)
40
    {
41 12
        foreach ($data as $key => $value) {
42 12
            $data[$key] = $context->root()->denormalize(
43 12
                $value,
44 12
                $type->isParametrized() ? $type->subType() : TypeFactory::mixedType(),
45 12
                $context
46 12
            );
47
        }
48
49 12
        if ($type->isParametrized() && $repository = $this->prime->repository($type->subType()->name())) {
50 2
            $factory = $repository->collectionFactory();
51
        } else {
52 10
            $factory = CollectionFactory::forDbal();
53
        }
54
55 12
        return $factory->wrap($data, $type->name());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $factory->wrap($data, $type->name()) returns the type Bdf\Prime\Collection\CollectionInterface which is incompatible with the return type mandated by Bdf\Serializer\Normalize...nterface::denormalize() of Bdf\Serializer\Normalizer\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function supports(string $className): bool
62
    {
63 1
        return is_subclass_of($className, CollectionInterface::class);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 413
    public function registerTo(NormalizerLoaderInterface $loader): void
70
    {
71 413
        foreach (CollectionFactory::collections() as $collection) {
72 413
            $loader->associate($collection, $this);
73
        }
74
    }
75
}
76