Issues (218)

src/Handler/SymfonyUidHandler.php (7 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\DeserializationContext;
8
use JMS\Serializer\Exception\InvalidArgumentException;
9
use JMS\Serializer\GraphNavigatorInterface;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
12
use JMS\Serializer\Visitor\SerializationVisitorInterface;
13
use JMS\Serializer\XmlSerializationVisitor;
14
use Symfony\Component\Uid\AbstractUid;
15
use Symfony\Component\Uid\Ulid;
16
use Symfony\Component\Uid\Uuid;
17
use Symfony\Component\Uid\UuidV1;
18
use Symfony\Component\Uid\UuidV3;
19
use Symfony\Component\Uid\UuidV4;
20
use Symfony\Component\Uid\UuidV5;
21
use Symfony\Component\Uid\UuidV6;
22
use Symfony\Component\Uid\UuidV7;
0 ignored issues
show
The type Symfony\Component\Uid\UuidV7 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...
23
use Symfony\Component\Uid\UuidV8;
0 ignored issues
show
The type Symfony\Component\Uid\UuidV8 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...
24
25
final class SymfonyUidHandler implements SubscribingHandlerInterface
26
{
27
    public const FORMAT_BASE32    = 'base32';
28
    public const FORMAT_BASE58    = 'base58';
29
    public const FORMAT_CANONICAL = 'canonical';
30
    public const FORMAT_RFC4122   = 'rfc4122';
31
32
    private const UID_CLASSES = [
33
        Ulid::class,
34
        Uuid::class,
35
        UuidV1::class,
36
        UuidV3::class,
37
        UuidV4::class,
38
        UuidV5::class,
39
        UuidV6::class,
40
        UuidV7::class,
41
        UuidV8::class,
42
    ];
43
44
    /**
45
     * @var string
46
     * @phpstan-var self::FORMAT_*
47
     */
48
    private $defaultFormat;
49
50
    /**
51
     * @var bool
52
     */
53
    private $xmlCData;
54
55
    public function __construct(string $defaultFormat = self::FORMAT_CANONICAL, bool $xmlCData = true)
56
    {
57
        $this->defaultFormat = $defaultFormat;
58
        $this->xmlCData = $xmlCData;
59
    }
60
61
    public static function getSubscribingMethods(): array
62
    {
63
        $methods = [];
64
        $formats = ['json', 'xml'];
65
66
        foreach ($formats as $format) {
67
            foreach (self::UID_CLASSES as $class) {
68
                $methods[] = [
69
                    'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
70
                    'format'    => $format,
71
                    'type'      => $class,
72
                    'method'    => 'serializeUid',
73
                ];
74
75
                $methods[] = [
76
                    'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
77
                    'format'    => $format,
78
                    'type'      => $class,
79
                    'method'    => 'deserializeUidFrom' . ucfirst($format),
80
                ];
81
            }
82
        }
83
84
        return $methods;
85
    }
86
87
    /**
88
     * @phpstan-param array{name: class-string<AbstractUid>, params: array} $type
89
     */
90
    public function deserializeUidFromJson(DeserializationVisitorInterface $visitor, ?string $data, array $type, DeserializationContext $context): ?AbstractUid
0 ignored issues
show
The parameter $visitor 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

90
    public function deserializeUidFromJson(/** @scrutinizer ignore-unused */ DeserializationVisitorInterface $visitor, ?string $data, array $type, DeserializationContext $context): ?AbstractUid

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...
The parameter $context 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

90
    public function deserializeUidFromJson(DeserializationVisitorInterface $visitor, ?string $data, array $type, /** @scrutinizer ignore-unused */ DeserializationContext $context): ?AbstractUid

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...
91
    {
92
        if (null === $data) {
93
            return null;
94
        }
95
96
        return $this->deserializeUid($data, $type);
97
    }
98
99
    /**
100
     * @phpstan-param array{name: class-string<AbstractUid>, params: array} $type
101
     */
102
    public function deserializeUidFromXml(DeserializationVisitorInterface $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context): ?AbstractUid
0 ignored issues
show
The parameter $context 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

102
    public function deserializeUidFromXml(DeserializationVisitorInterface $visitor, \SimpleXMLElement $data, array $type, /** @scrutinizer ignore-unused */ DeserializationContext $context): ?AbstractUid

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...
The parameter $visitor 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

102
    public function deserializeUidFromXml(/** @scrutinizer ignore-unused */ DeserializationVisitorInterface $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context): ?AbstractUid

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...
103
    {
104
        if ($this->isDataXmlNull($data)) {
105
            return null;
106
        }
107
108
        return $this->deserializeUid((string) $data, $type);
109
    }
110
111
    /**
112
     * @phpstan-param array{name: class-string<AbstractUid>, params: array} $type
113
     */
114
    private function deserializeUid(string $data, array $type): ?AbstractUid
115
    {
116
        /** @var class-string<AbstractUid> $uidClass */
117
        $uidClass = $type['name'];
118
119
        try {
120
            return $uidClass::fromString($data);
121
        } catch (\InvalidArgumentException | \TypeError $exception) {
122
            throw new InvalidArgumentException(sprintf('"%s" is not a valid UID string.', $data), 0, $exception);
123
        }
124
    }
125
126
    /**
127
     * @return \DOMText|string
128
     *
129
     * @phpstan-param array{name: class-string<AbstractUid>, params: array} $type
130
     */
131
    public function serializeUid(SerializationVisitorInterface $visitor, AbstractUid $uid, array $type, SerializationContext $context)
0 ignored issues
show
The parameter $context 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

131
    public function serializeUid(SerializationVisitorInterface $visitor, AbstractUid $uid, array $type, /** @scrutinizer ignore-unused */ SerializationContext $context)

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...
132
    {
133
        /** @phpstan-var self::FORMAT_* $format */
134
        $format = $type['params'][0]['name'] ?? $this->defaultFormat;
135
136
        switch ($format) {
137
            case self::FORMAT_BASE32:
138
                $serialized = $uid->toBase32();
139
                break;
140
141
            case self::FORMAT_BASE58:
142
                $serialized = $uid->toBase58();
143
                break;
144
145
            case self::FORMAT_CANONICAL:
146
                $serialized = (string) $uid;
147
                break;
148
149
            case self::FORMAT_RFC4122:
150
                $serialized = $uid->toRfc4122();
151
                break;
152
153
            default:
154
                throw new InvalidArgumentException(sprintf('The "%s" format is not valid.', $format));
155
        }
156
157
        if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) {
158
            return $visitor->visitSimpleString($serialized, $type);
159
        }
160
161
        return $visitor->visitString($serialized, $type);
162
    }
163
164
    /**
165
     * @param mixed $data
166
     */
167
    private function isDataXmlNull($data): bool
168
    {
169
        $attributes = $data->attributes('xsi', true);
170
171
        return isset($attributes['nil'][0]) && 'true' === (string) $attributes['nil'][0];
172
    }
173
}
174