Total Complexity | 42 |
Total Lines | 213 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Fixer 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 Fixer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Fixer |
||
14 | { |
||
15 | use PadsJson; |
||
16 | |||
17 | /** @var array Current token stack indexed by position */ |
||
18 | protected $stack = []; |
||
19 | |||
20 | /** @var bool If current char is within a string */ |
||
21 | protected $inStr = false; |
||
22 | |||
23 | /** @var array The complementary pairs */ |
||
24 | protected $pairs = [ |
||
25 | '{' => '}', |
||
26 | '[' => ']', |
||
27 | '"' => '"', |
||
28 | ]; |
||
29 | |||
30 | /** @var int The last seen object `{` type position */ |
||
31 | protected $objectPos = -1; |
||
32 | |||
33 | /** @var int The last seen array `[` type position */ |
||
34 | protected $arrayPos = -1; |
||
35 | |||
36 | /** @var string Missing value. (Options: true, false, null) */ |
||
37 | protected $missingValue = 'true'; |
||
38 | |||
39 | /** |
||
40 | * Fix the truncated JSON. |
||
41 | * |
||
42 | * @param string $json The JSON string to fix. |
||
43 | * @param bool $silent If silent, doesnt throw when fixing fails. |
||
44 | * @param string $missingValue Missing value constructor. (Options: true, false, null). |
||
45 | * |
||
46 | * @throws \RuntimeExcaption When fixing fails. |
||
47 | * |
||
48 | * @return string Fixed JSON. If failed with silent then original JSON. |
||
49 | */ |
||
50 | public function fix($json, $silent = false, $missingValue = 'null') |
||
51 | { |
||
52 | list($head, $json, $tail) = $this->trim($json); |
||
53 | |||
54 | if (empty($json) || $this->isValid($json)) { |
||
55 | return $json; |
||
56 | } |
||
57 | |||
58 | if (null !== $tmpJson = $this->quickFix($json)) { |
||
59 | return $tmpJson; |
||
60 | } |
||
61 | |||
62 | $this->reset($missingValue); |
||
63 | |||
64 | return $head . $this->doFix(\rtrim($json), $silent) . $tail; |
||
65 | } |
||
66 | |||
67 | public function trim($json) |
||
68 | { |
||
69 | \preg_match('/^(\s*)([^\s]+)(\s*)$/', $json, $match); |
||
70 | |||
71 | $match += ['', '', \trim($json), '']; |
||
72 | |||
73 | \array_shift($match); |
||
74 | |||
75 | return $match; |
||
76 | } |
||
77 | |||
78 | protected function isValid($json) |
||
83 | } |
||
84 | |||
85 | public function quickFix($json) |
||
86 | { |
||
87 | if (\strlen($json) === 1 && isset($this->pairs[$json])) { |
||
88 | return $json . $this->pairs[$json]; |
||
89 | } |
||
90 | |||
91 | if ($json[0] !== '"') { |
||
92 | return $this->maybeLiteral($json); |
||
93 | } |
||
94 | |||
95 | return $this->padString($json); |
||
96 | } |
||
97 | |||
98 | protected function maybeLiteral($json) |
||
99 | { |
||
100 | if (!\in_array($json[0], ['t', 'f', 'n'])) { |
||
101 | return null; |
||
102 | } |
||
103 | |||
104 | foreach (['true', 'false', 'null'] as $literal) { |
||
105 | if (\strpos($literal, $json) === 0) { |
||
106 | return $literal; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // @codeCoverageIgnoreStart |
||
111 | return null; |
||
112 | // @codeCoverageIgnoreEnd |
||
113 | } |
||
114 | |||
115 | protected function reset($missingValue = 'null') |
||
116 | { |
||
117 | $this->stack = []; |
||
118 | $this->inStr = false; |
||
119 | |||
120 | $this->objectPos = -1; |
||
121 | $this->arrayPos = -1; |
||
122 | |||
123 | $this->missingValue = $missingValue; |
||
124 | } |
||
125 | |||
126 | protected function doFix($json, $silent = false) |
||
141 | } |
||
142 | |||
143 | protected function stack($prev, $char, $index, $next) |
||
|
|||
144 | { |
||
160 | } |
||
161 | |||
162 | protected function lastToken() |
||
165 | } |
||
166 | |||
167 | protected function popToken($token = null) |
||
168 | { |
||
169 | // Last one |
||
170 | if (null === $token) { |
||
171 | return \array_pop($this->stack); |
||
172 | } |
||
173 | |||
174 | $keys = \array_reverse(\array_keys($this->stack)); |
||
175 | foreach ($keys as $key) { |
||
176 | if ($this->stack[$key] === $token) { |
||
177 | unset($this->stack[$key]); |
||
178 | break; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | protected function maybeStr($prev, $char, $index) |
||
184 | { |
||
185 | if ($prev !== '\\' && $char === '"') { |
||
186 | $this->inStr = !$this->inStr; |
||
187 | } |
||
188 | |||
189 | if ($this->inStr && $this->lastToken() !== '"') { |
||
190 | $this->stack[$index] = '"'; |
||
191 | } |
||
192 | |||
193 | return $this->inStr; |
||
194 | } |
||
195 | |||
196 | protected function updatePos($char, $index) |
||
197 | { |
||
198 | if ($char === '{') { |
||
199 | $this->objectPos = $index; |
||
200 | } elseif ($char === '}') { |
||
201 | $this->popToken('{'); |
||
202 | $this->objectPos = -1; |
||
203 | } elseif ($char === '[') { |
||
204 | $this->arrayPos = $index; |
||
205 | } elseif ($char === ']') { |
||
206 | $this->popToken('['); |
||
207 | $this->arrayPos = -1; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | protected function fixOrFail($json, $silent) |
||
226 | ); |
||
227 | } |
||
228 | } |
||
229 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.