Conditions | 13 |
Paths | 31 |
Total Lines | 56 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |
||
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 {} ? |
||
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 | } |
||
140 |