Failed Conditions
Push — master ( ebb1ea...d49c03 )
by Bernhard
06:05
created

ConfigFileConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 0
cbo 3
dl 10
loc 66
ccs 29
cts 29
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 13 2
A fromJson() 0 20 4
A objectsToArrays() 10 10 3
B arraysToObjects() 0 11 5

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\ConfigFile;
15
use Puli\Manager\Assert\Assert;
16
use Webmozart\Json\Conversion\JsonConverter;
17
18
/**
19
 * Converts config files to JSON and back.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
class ConfigFileConverter implements JsonConverter
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function toJson($configFile, array $options = array())
31
    {
32 2
        Assert::isInstanceOf($configFile, 'Puli\Manager\Api\Config\ConfigFile');
33
34 2
        $jsonData = array();
35
36 2
        foreach ($configFile->getConfig()->toRawArray(false) as $key => $value) {
37 1
            $jsonData[$key] = $value;
38
        }
39
40
        // The topmost array is always an object, even if empty
41 2
        return (object) $this->arraysToObjects($jsonData);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 50
    public function fromJson($jsonData, array $options = array())
48
    {
49 50
        $path = isset($options['path']) ? $options['path'] : null;
50 50
        $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
51
52 50
        Assert::isInstanceOf($jsonData, 'stdClass');
53 50
        Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
54 50
        Assert::nullOrIsInstanceOf($baseConfig, 'Puli\Manager\Api\Config\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
55
56 50
        $configFile = new ConfigFile($path, $baseConfig);
57 50
        $config = $configFile->getConfig();
58
59 50
        $jsonData = $this->objectsToArrays($jsonData);
60
61 50
        foreach ($jsonData as $key => $value) {
62 1
            $config->set($key, $value);
63
        }
64
65 50
        return $configFile;
66
    }
67
68 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...
69
    {
70 50
        $data = (array) $data;
71
72 50
        foreach ($data as $key => $value) {
73 1
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
74
        }
75
76 50
        return $data;
77
    }
78
79 2
    private function arraysToObjects(array $data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
80
    {
81 2
        $intKeys = true;
82
83 2
        foreach ($data as $key => $value) {
84 1
            $intKeys = $intKeys && is_int($key);
85 1
            $data[$key] = is_array($value) ? $this->arraysToObjects($value) : $value;
86
        }
87
88 2
        return $intKeys ? $data : (object) $data;
89
    }
90
}
91