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

JsonDictionary::json_format()   C

Complexity

Conditions 15
Paths 16

Size

Total Lines 57
Code Lines 45

Duplication

Lines 23
Ratio 40.35 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 57
rs 6.5498
cc 15
eloc 45
nc 16
nop 1

How to fix   Long Method    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
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