Total Complexity | 50 |
Total Lines | 260 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RandomToFile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RandomToFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class RandomToFile extends RandomArray |
||
16 | { |
||
17 | /** |
||
18 | * Function for processing data and output to a file |
||
19 | * |
||
20 | * @var callable |
||
21 | */ |
||
22 | public $fn_file_output; |
||
23 | |||
24 | /** |
||
25 | * File name for data output |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | public $full_file_name; |
||
30 | |||
31 | /** |
||
32 | * Handler of file opened for write |
||
33 | * |
||
34 | * @var resource|null |
||
35 | */ |
||
36 | public $file_handler; |
||
37 | |||
38 | /** |
||
39 | * Default extention for make file names |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $default_ext = '.json'; |
||
44 | |||
45 | /** |
||
46 | * Main parameter of this class - function for processing and write data |
||
47 | * By default, this hook is set as a function 'writeFileOutputExample', |
||
48 | * which does the following function: output PHP code for assigning array. |
||
49 | * By analogy, write the necessary output functions for the desired format |
||
50 | * |
||
51 | * @param callable|null $fn_write |
||
52 | */ |
||
53 | public function __construct(callable $fn_write = null) |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public function writeFileOutputExample($parr) |
||
65 | { |
||
66 | static $keys = []; |
||
67 | |||
68 | //extracting following work variables: |
||
69 | \extract($parr); //$signal, $k, $v, $lim_depth, $root |
||
70 | |||
71 | //begin formin output string |
||
72 | $out_str = '$x'; |
||
73 | |||
74 | switch ($signal) { |
||
75 | //siglan 'next' - when output next scalar element of array [$k]=>$v |
||
76 | case 'next': |
||
77 | if (!\is_numeric($k)) { |
||
78 | $k = "'" . \addcslashes($k, "'\\") . "'"; |
||
79 | } |
||
80 | if (!\is_numeric($v)) { |
||
81 | $v = "'" . \addcslashes($v, "'\\") . "'"; |
||
82 | } |
||
83 | $out_str .= (count($keys) ? |
||
84 | '[' . implode('][', $keys) . ']' |
||
85 | : |
||
86 | '' |
||
87 | ) . '[' . $k . ']=' . $v . ";\r\n"; |
||
88 | |||
89 | break; |
||
90 | |||
91 | //signal 'open' - when root or nested array beginning |
||
92 | case 'open': |
||
93 | if (count($keys) || !empty($root)) { |
||
94 | //nested array beginned |
||
95 | if (!is_numeric($root)) { |
||
96 | $root = "'" . \addcslashes($root, "'\\") . "'"; |
||
97 | } |
||
98 | array_push($keys, $root); |
||
99 | $out_str .= '[' . implode('][', $keys) . ']'; |
||
100 | $out_str .= "=[]; /* Create sub-array in key $out_str */\r\n"; |
||
101 | } else { |
||
102 | //root array beginned |
||
103 | $out_str .= "=[]; /* CREATE ROOT OF ARRAY */\r\n"; |
||
104 | $out_str = '<' . "?php\n" . $out_str; |
||
105 | } |
||
106 | break; |
||
107 | |||
108 | //signal 'close' - when root or nested array ended |
||
109 | case 'close': |
||
110 | if (count($keys)) { |
||
111 | //nested array ended |
||
112 | $out_str = "/* end $out_str" |
||
113 | .'[' . \implode('][', $keys) . ']' |
||
114 | . "*/\r\n"; |
||
115 | \array_pop($keys); |
||
116 | } else { |
||
117 | //root array ended |
||
118 | $out_str = " /* END OF ARRAY */\r\n"; |
||
119 | } |
||
120 | break; |
||
121 | |||
122 | //signal 'init' - when file open for write |
||
123 | case 'init': |
||
124 | $keys = []; |
||
125 | $out_str = ''; |
||
126 | } |
||
127 | //write formed string to output file |
||
128 | \fwrite($fh, $out_str); |
||
129 | } |
||
130 | |||
131 | public function genRandomToFile( |
||
228 | } |
||
229 | |||
230 | public function genBigRange($total_elements) |
||
231 | { |
||
232 | for ($k = 1; $k <= $total_elements; $k++) { |
||
233 | $v = \mt_rand(0, 65535); |
||
234 | yield $k => $v; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | public function genTempFileName($ext = null, $test_exception = false) |
||
249 | } |
||
250 | |||
251 | public function setOutputFile($file_name = null, $ext = null) |
||
260 | } |
||
261 | |||
262 | public function openOutputFile() |
||
268 | } |
||
269 | |||
270 | public function closeOutputFile() |
||
275 | } |
||
276 | } |
||
278 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.