Complex classes like Context 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Context |
||
28 | { |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | public $macros = [ |
||
33 | DollarExpansion::SEMVAL_LHS_TYPED => '', |
||
34 | DollarExpansion::SEMVAL_LHS_UNTYPED => '', |
||
35 | DollarExpansion::SEMVAL_RHS_TYPED => '', |
||
36 | DollarExpansion::SEMVAL_RHS_UNTYPED => '', |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | public $countSymbols = 0; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | public $countTerminals = 0; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | public $countNonTerminals = 0; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $symbolHash = []; |
||
58 | |||
59 | /** |
||
60 | * @var array|Symbol[] |
||
61 | */ |
||
62 | protected $_symbols = []; |
||
63 | |||
64 | /** |
||
65 | * @var Symbol |
||
66 | */ |
||
67 | protected $_nilsymbol; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $finished = false; |
||
73 | |||
74 | /** |
||
75 | * @var array|State[] |
||
76 | */ |
||
77 | protected $_states; |
||
78 | |||
79 | /** |
||
80 | * @var int |
||
81 | */ |
||
82 | public $countStates = 0; |
||
83 | |||
84 | /** |
||
85 | * @var int |
||
86 | */ |
||
87 | public $countNonLeafStates = 0; |
||
88 | |||
89 | /** |
||
90 | * @var bool |
||
91 | */ |
||
92 | public $aflag = false; |
||
93 | |||
94 | /** |
||
95 | * @var bool |
||
96 | */ |
||
97 | public $tflag = false; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | public $className = ''; |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | */ |
||
107 | public $debug = false; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | public $filename = 'YY'; |
||
113 | |||
114 | /** |
||
115 | * @var bool |
||
116 | */ |
||
117 | public $pureFlag = false; |
||
118 | |||
119 | /** |
||
120 | * @var Symbol |
||
121 | */ |
||
122 | public $startSymbol; |
||
123 | |||
124 | /** |
||
125 | * @var int |
||
126 | */ |
||
127 | public $expected; |
||
128 | |||
129 | /** |
||
130 | * @var bool |
||
131 | */ |
||
132 | public $unioned = false; |
||
133 | |||
134 | /** |
||
135 | * @var Symbol |
||
136 | */ |
||
137 | public $eofToken; |
||
138 | |||
139 | /** |
||
140 | * @var Symbol |
||
141 | */ |
||
142 | public $errorToken; |
||
143 | |||
144 | /** |
||
145 | * @var Symbol |
||
146 | */ |
||
147 | public $startPrime; |
||
148 | |||
149 | /** |
||
150 | * @var array|Production[] |
||
151 | */ |
||
152 | protected $_grams = []; |
||
153 | |||
154 | /** |
||
155 | * @var int |
||
156 | */ |
||
157 | public $countGrams = 0; |
||
158 | |||
159 | /** |
||
160 | * @var array |
||
161 | */ |
||
162 | public $defaultAct = []; |
||
163 | |||
164 | /** |
||
165 | * @var array |
||
166 | */ |
||
167 | public $defaultGoto = []; |
||
168 | |||
169 | /** |
||
170 | * @var array |
||
171 | */ |
||
172 | public $termAction = []; |
||
173 | |||
174 | /** |
||
175 | * @var array |
||
176 | */ |
||
177 | public $classAction = []; |
||
178 | |||
179 | /** |
||
180 | * @var array |
||
181 | */ |
||
182 | public $nonTermGoto = []; |
||
183 | |||
184 | /** |
||
185 | * @var array |
||
186 | */ |
||
187 | public $classOf = []; |
||
188 | |||
189 | /** |
||
190 | * @var array |
||
191 | */ |
||
192 | public $cTermIndex = []; |
||
193 | |||
194 | /** |
||
195 | * @var array |
||
196 | */ |
||
197 | public $oTermIndex = []; |
||
198 | |||
199 | /** |
||
200 | * @var array |
||
201 | */ |
||
202 | public $frequency = []; |
||
203 | |||
204 | /** |
||
205 | * @var array |
||
206 | */ |
||
207 | public $stateImageSorted = []; |
||
208 | |||
209 | /** |
||
210 | * @var int |
||
211 | */ |
||
212 | public $countPrims = 0; |
||
213 | |||
214 | /** |
||
215 | * @var array |
||
216 | */ |
||
217 | public $prims = []; |
||
218 | |||
219 | /** |
||
220 | * @var array |
||
221 | */ |
||
222 | public $primof = []; |
||
223 | |||
224 | /** |
||
225 | * @var array |
||
226 | */ |
||
227 | public $class2nd = []; |
||
228 | |||
229 | /** |
||
230 | * @var int |
||
231 | */ |
||
232 | public $countClasses = 0; |
||
233 | |||
234 | /** |
||
235 | * @var int |
||
236 | */ |
||
237 | public $countAux = 0; |
||
238 | |||
239 | /** |
||
240 | * @var null|resource |
||
241 | */ |
||
242 | public $debugFile; |
||
243 | |||
244 | /** |
||
245 | * Context constructor. |
||
246 | * |
||
247 | * @param string $filename |
||
248 | * @param resource|null $debugFile |
||
249 | */ |
||
250 | public function __construct(string $filename = 'YY', resource $debugFile = null) |
||
255 | |||
256 | /** |
||
257 | * @param string $data |
||
258 | */ |
||
259 | public function debug(string $data) |
||
265 | |||
266 | /** |
||
267 | * @param $name |
||
268 | * |
||
269 | * @throws LogicException |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public function __get($name) |
||
286 | |||
287 | /** |
||
288 | * @param $name |
||
289 | * @param $value |
||
290 | */ |
||
291 | public function __set($name, $value) |
||
295 | |||
296 | /** |
||
297 | * @return void |
||
298 | */ |
||
299 | public function finish() |
||
324 | |||
325 | /** |
||
326 | * @return Symbol |
||
327 | */ |
||
328 | public function nilSymbol(): Symbol |
||
336 | |||
337 | /** |
||
338 | * @return \Generator |
||
339 | */ |
||
340 | public function terminals(): \Generator |
||
348 | |||
349 | /** |
||
350 | * @return \Generator |
||
351 | */ |
||
352 | public function nilSymbols(): \Generator |
||
360 | |||
361 | /** |
||
362 | * @return \Generator |
||
363 | */ |
||
364 | public function nonTerminals(): \Generator |
||
372 | |||
373 | /** |
||
374 | * @return Symbol |
||
375 | */ |
||
376 | public function genNonTerminal(): Symbol |
||
382 | |||
383 | /** |
||
384 | * @param string $s |
||
385 | * @param bool $isTerm |
||
386 | * |
||
387 | * @return Symbol |
||
388 | */ |
||
389 | public function internSymbol(string $s, bool $isTerm): Symbol |
||
414 | |||
415 | /** |
||
416 | * @param string $s |
||
417 | * |
||
418 | * @return Symbol |
||
419 | */ |
||
420 | public function intern(string $s): Symbol |
||
429 | |||
430 | /** |
||
431 | * @param Symbol $symbol |
||
432 | * |
||
433 | * @return Symbol |
||
434 | */ |
||
435 | public function addSymbol(Symbol $symbol): Symbol |
||
453 | |||
454 | /** |
||
455 | * @return array |
||
456 | */ |
||
457 | public function symbols(): array |
||
461 | |||
462 | /** |
||
463 | * @param int $code |
||
464 | * |
||
465 | * @return Symbol |
||
466 | */ |
||
467 | public function symbol(int $code): Symbol |
||
477 | |||
478 | /** |
||
479 | * @param Production $p |
||
480 | * |
||
481 | * @return Production |
||
482 | */ |
||
483 | public function addGram(Production $p) |
||
490 | |||
491 | /** |
||
492 | * @param int $i |
||
493 | * |
||
494 | * @return Production |
||
495 | */ |
||
496 | public function gram(int $i): Production |
||
502 | |||
503 | /** |
||
504 | * @param array $states |
||
505 | */ |
||
506 | public function setStates(array $states) |
||
514 | |||
515 | /** |
||
516 | * @param int $n |
||
517 | */ |
||
518 | public function setCountNonLeafStates(int $n) |
||
522 | } |
||
523 |
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.