Completed
Pull Request — master (#104)
by
unknown
01:55
created

JsonDictionary   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 25.27 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 0
cbo 2
dl 23
loc 91
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 22 3
C json_format() 23 57 15

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
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
use Symfony\Component\Serializer\Encoder\JsonEncoder;
7
8
class JsonDictionary extends Generator implements GeneratorInterface
9
{
10
    /**
11
     * {@parentDoc}.
12
     */
13
    public static function toString(Translations $translations)
14
    {
15
        $array = PhpArray::toArray($translations);
16
17
        //for a simple json translation dictionary, one domain is supported
18
        $values = current($array);
19
20
        // remove meta / header data
21
        if (array_key_exists('', $values)) {
22
            unset( $values[''] );
23
        }
24
25
        //map to a simple json dictionary (no plurals)
26
        return static::json_format(
27
            array_map(
28
                function ($val) {
29
                    return isset( $val[1] ) ? $val[1] : '';
30
                },
31
                $values
32
            )
33
        );
34
    }
35
36
    /*
37
     * Pretty print some JSON.
38
     *
39
     * As taken from http://php.net/manual/en/function.json-encode.php#80339
40
     */
41
    protected static function json_format($data)
42
    {
43
        $tab          = "  ";
44
        $new_json     = "";
45
        $indent_level = 0;
46
        $in_string    = false;
47
48
        $json = json_encode($data);
49
        $len  = strlen($json);
50
51
        for ($c = 0; $c < $len; $c++) {
52
            $char = $json[$c];
53
            switch ($char) {
54
                case '{':
55 View Code Duplication
                case '[':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
                    if ( ! $in_string) {
57
                        $new_json .= $char."\n".str_repeat($tab, $indent_level + 1);
58
                        $indent_level++;
59
                    } else {
60
                        $new_json .= $char;
61
                    }
62
                    break;
63
                case '}':
64 View Code Duplication
                case ']':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
                    if ( ! $in_string) {
66
                        $indent_level--;
67
                        $new_json .= "\n".str_repeat($tab, $indent_level).$char;
68
                    } else {
69
                        $new_json .= $char;
70
                    }
71
                    break;
72 View Code Duplication
                case ',':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                    if ( ! $in_string) {
74
                        $new_json .= ",\n".str_repeat($tab, $indent_level);
75
                    } else {
76
                        $new_json .= $char;
77
                    }
78
                    break;
79
                case ':':
80
                    if ( ! $in_string) {
81
                        $new_json .= ": ";
82
                    } else {
83
                        $new_json .= $char;
84
                    }
85
                    break;
86
                case '"':
87
                    if ($c > 0 && $json[$c - 1] != '\\') {
88
                        $in_string = ! $in_string;
89
                    }
90
                default:
91
                    $new_json .= $char;
92
                    break;
93
            }
94
        }
95
96
        return $new_json;
97
    }
98
}
99