SerializerFactory::new()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10

Importance

Changes 5
Bugs 0 Features 2
Metric Value
eloc 20
c 5
b 0
f 2
dl 0
loc 33
ccs 8
cts 8
cp 1
rs 7.6666
cc 10
nc 10
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 */
11
12
namespace Koded\Stdlib\Serializer;
13
14
use Koded\Exceptions\SerializerException;
15
use Koded\Stdlib\Serializer;
16
use function extension_loaded;
17
use function is_a;
18
19
final class SerializerFactory
20
{
21
    /**
22
     * Factory that creates a new instance of Serializer.
23
     *
24
     * @param string $name The name of the supported serializer.
25
     *                     Provide a FQCN for custom serializers
26
     * @param        $args [optional] Optional arguments for the serializer class
27
     *
28
     * @return Serializer
29
     * @throws SerializerException
30
     */
31 2
    public static function new(string $name, ...$args): Serializer
32
    {
33
        switch ($name) {
34
            case Serializer::JSON:
35 2
                return new JsonSerializer(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $options of Koded\Stdlib\Serializer\...rializer::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
                return new JsonSerializer(/** @scrutinizer ignore-type */ ...$args);
Loading history...
36
37
            case Serializer::PHP:
38 1
                return new PhpSerializer;
39
40
            case Serializer::XML:
41 2
                empty($args) && $args = [null];
42 2
                return new XmlSerializer(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $root of Koded\Stdlib\Serializer\...rializer::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                return new XmlSerializer(/** @scrutinizer ignore-type */ ...$args);
Loading history...
43
44
            case Serializer::IGBINARY:
45
                // @codeCoverageIgnoreStart
46
                if (false === extension_loaded('igbinary')) {
47
                    throw SerializerException::forMissingModule(Serializer::MSGPACK);
48
                }
49
                return new IgbinarySerializer;
50
            // @codeCoverageIgnoreEnd
51
52
            case Serializer::MSGPACK:
53
                // @codeCoverageIgnoreStart
54
                if (false === extension_loaded('msgpack')) {
55
                    throw SerializerException::forMissingModule(Serializer::MSGPACK);
56
                }
57
                return new MsgpackSerializer;
58
            // @codeCoverageIgnoreEnd
59
        }
60 2
        if (is_a($name, Serializer::class, true)) {
61 1
            return new $name(...$args);
62
        }
63 1
        throw SerializerException::forCreateSerializer($name);
64
    }
65
}
66