Passed
Push — master ( 980804...08b834 )
by Mihail
09:48
created

SerializerFactory::new()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 20
c 4
b 0
f 2
dl 0
loc 35
ccs 7
cts 7
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
17
final class SerializerFactory
18
{
19
    /**
20
     * Factory that creates a new instance of Serializer.
21
     *
22
     * @param string $name The name of the supported serializer.
23
     *                     Provide a FQCN for custom serializers
24
     * @param        $args [optional] Optional arguments for the serializer class
25
     *
26
     * @return Serializer
27
     * @throws SerializerException
28
     */
29
    public static function new(string $name, ...$args): Serializer
30
    {
31 5
        switch ($name) {
32
            case Serializer::JSON:
33 5
                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

33
                return new JsonSerializer(/** @scrutinizer ignore-type */ ...$args);
Loading history...
34
35 1
            case Serializer::PHP:
36
                return new PhpSerializer;
37
38 1
            case Serializer::XML:
39
                empty($args) && $args = [null];
40
                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

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