Failed Conditions
Pull Request — master (#47)
by Mateusz
24:21
created

ConfigJsonSerializer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 47.83 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 0
cbo 5
dl 33
loc 69
ccs 32
cts 32
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeConfigFile() 0 10 2
A unserializeConfigFile() 0 13 2
A encode() 9 9 1
A decode() 14 14 3
A objectsToArrays() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Config;
13
14
use Puli\Manager\Api\Config\Config;
15
use Puli\Manager\Api\Config\ConfigFile;
16
use Puli\Manager\Api\Config\ConfigFileSerializer;
17
use Puli\Manager\Api\InvalidConfigException;
18
use stdClass;
19
use Webmozart\Json\DecodingFailedException;
20
use Webmozart\Json\JsonDecoder;
21
use Webmozart\Json\JsonEncoder;
22
23
/**
24
 * Serializes and unserializes to/from JSON.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class ConfigJsonSerializer implements ConfigFileSerializer
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function serializeConfigFile(ConfigFile $configFile)
36
    {
37 2
        $jsonData = new stdClass();
38
39 2
        foreach ($configFile->getConfig()->toRawArray(false) as $key => $value) {
40 1
            $jsonData->$key = $value;
41
        }
42
43 2
        return $this->encode($jsonData);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 51
    public function unserializeConfigFile($serialized, $path = null, Config $baseConfig = null)
50
    {
51 51
        $configFile = new ConfigFile($path, $baseConfig);
52 51
        $config = $configFile->getConfig();
53
54 51
        $jsonData = $this->objectsToArrays($this->decode($serialized, $path));
55
56 50
        foreach ($jsonData as $key => $value) {
57 2
            $config->set($key, $value);
58
        }
59
60 50
        return $configFile;
61
    }
62
63 2 View Code Duplication
    private function encode($jsonData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65 2
        $encoder = new JsonEncoder();
66 2
        $encoder->setPrettyPrinting(true);
67 2
        $encoder->setEscapeSlash(false);
68 2
        $encoder->setTerminateWithLineFeed(true);
69
70 2
        return $encoder->encode($jsonData);
71
    }
72
73 51 View Code Duplication
    private function decode($json, $path = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75 51
        $decoder = new JsonDecoder();
76
77
        try {
78 51
            return $decoder->decode($json);
79 1
        } catch (DecodingFailedException $e) {
80 1
            throw new InvalidConfigException(sprintf(
81 1
                "The configuration%s could not be decoded:\n%s",
82 1
                $path ? ' in '.$path : '',
83 1
                $e->getMessage()
84 1
            ), $e->getCode(), $e);
85
        }
86
    }
87
88 50 View Code Duplication
    private function objectsToArrays($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 50
        $data = (array) $data;
91
92 50
        foreach ($data as $key => $value) {
93 2
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
94
        }
95
96 50
        return $data;
97
    }
98
}
99