Completed
Push — master ( 6497b0...658530 )
by Alexander
03:10
created

RandomJson::writeFileRandomJson()   C

Complexity

Conditions 13
Paths 31

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 35
nc 31
nop 1
dl 0
loc 56
rs 6.6843
c 0
b 0
f 0

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 ierusalim\Random;
4
5
/**
6
 * This class RandomJson is intended for generating random json-files
7
 *
8
 * PHP Version 5.6
9
 *
10
 * @package    ierusalim\RandomJson
11
 * @author     Alexander Jer <[email protected]>
12
 * @copyright  2017, Ierusalim
13
 * @license    https://opensource.org/licenses/Apache-2.0 Apache-2.0
14
 *
15
 * Example of use:
16
 * $g = new RandomJson();
17
 * $g->setKeysModel();
18
 * $g->setValuesModel();
19
 * $file_name = $g->genRandomJson(10, 10, 32768, 3, 100, 65535);
20
 * $json_raw = file_get_contents($file_name);
21
 * $arr = json_decode($json_raw,true);
22
 * print_r($arr);
23
 *
24
 * //Example for ASCII chars and UTF-8 multibyte values:
25
 * $g->setKeysModel(5,8,implode(range('a','z')));
26
 * $g->setValuesModel(1,10, "神會貓性少女 迪克和陰部", true);
27
 * $file_name = $g->genRandomJson(10, 10, 32768, 3, 100, 0);
28
 * $json_raw = file_get_contents($file_name);
29
 * $arr = json_decode($json_raw,true);
30
 * print_r($arr);
31
 */
32
class RandomJson extends RandomToFile
33
{
34
35
    public $threshold_obj = 32768;
36
37
    public function __construct($file_name = null)
38
    {
39
        parent::__construct([$this, 'writeFileRandomJson']);
40
        $file_name = $this->setOutputFile($file_name);
0 ignored issues
show
Unused Code introduced by
The assignment to $file_name is dead and can be removed.
Loading history...
41
    }
42
43
    public function genRandomJson(
44
        $min_elem_cnt = 3,
45
        $max_elem_cnt = 10,
46
        $threshold_nesting = 32768,
47
        $lim_depth = 3,
48
        $lim_elements = 100000,
49
        $threshold_obj = null
50
    ) {
51
        if (!\is_null($threshold_obj)) {
52
            $this->threshold_obj = $threshold_obj;
53
        }
54
        if (!$this->genRandomToFile(
55
                $min_elem_cnt,
56
                $max_elem_cnt,
57
                $threshold_nesting,
58
                $lim_depth,
59
                $lim_elements
60
            )
61
        ) {
62
            return false;
63
        }
64
        return $this->full_file_name;
65
    }
66
67
    public function makeNextValueStr($k, $v, $is_obj, &$need_div) {
68
        if ($is_obj) {
69
            $out_str = json_encode([$k => $v]);
70
            $out_str = substr($out_str, 1, -1);
71
        } else {
72
            $out_str = json_encode($v);
73
        }
74
        if ($need_div) {
75
            $out_str = ',' . $out_str;
76
        } else {
77
            $need_div = true;
78
        }
79
       return $out_str;
80
    }
81
82
    public function writeFileRandomJson($parr)
83
    {
84
        static $keys = [];
85
        static $key_is_obj = [];
86
87
        static $need_div = 0;
88
89
        //extracting following work variables:
90
        \extract($parr); //$signal, $k, $v, $lim_depth, $root
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92
        switch ($signal) {
93
            //siglan 'next' - output next scalar element of array [$k]=>$v
94
            case 'next':
95
                $is_obj = $key_is_obj[count($keys)];
96
                $out_str = $this->makeNextValueStr($k, $v, $is_obj, $need_div);
97
                break;
98
99
            //signal 'open' - root or nested array beginning
100
            case 'open':
101
                //Generate [] array or {} ?
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
                $is_obj = (\mt_rand(0, 65535) >= $this->threshold_obj);
103
                $c = \count($keys);
104
                if ($c || !empty($root)) {
105
                    //nested array beginned
106
                    $prev_is_obj = isset($key_is_obj[$c]) ? $key_is_obj[$c] : 0;
107
                    $root = substr(json_encode([$root => '']), 1, -4);
108
                    array_push($keys, $root);
109
                }
110
                $key_is_obj[count($keys)] = $is_obj;
111
                $out_str = ($need_div) ? ',' : '';
112
                if (!empty($prev_is_obj)) {
113
                    $out_str .= $root . ":";
114
                }
115
                $out_str .= ($is_obj ? '{' : '[');
116
                $need_div = false;
117
                break;
118
119
            //signal 'close' - root or nested array ended
120
            case 'close':
121
                $is_obj = $key_is_obj[count($keys)];
122
                if (count($keys)) {
123
                    //nested array ended
124
                    \array_pop($keys);
125
                }
126
                $out_str = ($is_obj ? '}' : ']');
127
                break;
128
129
            //signal 'init' - when file open for write
130
            case 'init':
131
                $keys = [];
132
                $key_is_obj = [];
133
                $need_div = 0;
134
                $out_str = '';
135
        }
136
        //write formed string to output file
137
        \fwrite($fh, $out_str);
138
    }
139
}
140