Complex classes like UniqueKeyExtractorAbstract 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 UniqueKeyExtractorAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class UniqueKeyExtractorAbstract extends DbExtractorAbstract implements JoinableInterface |
||
16 | { |
||
17 | /** |
||
18 | * the unique key name |
||
19 | * |
||
20 | * @var array|string |
||
21 | */ |
||
22 | protected $compositeKey; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $uniqueKeyName; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $uniqueKeyAlias; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $uniqueKeyValues = []; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $uniqueKeyValueBuffer = []; |
||
43 | |||
44 | /** |
||
45 | * @var callable |
||
46 | */ |
||
47 | protected $merger; |
||
48 | |||
49 | /** |
||
50 | * @var OnClauseInterface |
||
51 | */ |
||
52 | protected $onClose; |
||
53 | |||
54 | /** |
||
55 | * @var array of OnClauseInterface |
||
56 | */ |
||
57 | protected $joinerOnCloses = []; |
||
58 | |||
59 | /** |
||
60 | * the record map |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $recordMap; |
||
65 | |||
66 | /** |
||
67 | * The Joignable we may be joining against |
||
68 | * |
||
69 | * @var JoignableInterface |
||
70 | */ |
||
71 | protected $joinFrom; |
||
72 | |||
73 | /** |
||
74 | * generic extraction from tables with unique (composite) key |
||
75 | * |
||
76 | * @param string $extractQuery |
||
77 | * @param array|string $uniqueKey can be either a unique key name as |
||
78 | * string |
||
79 | * '(table.)compositeKeyName' // ('id' by default) |
||
80 | * |
||
81 | * or an array : |
||
82 | * ['(table.)compositeKey1'] // single unique key |
||
83 | * ['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key |
||
84 | * |
||
85 | * or an associative array in case you are using aliases : |
||
86 | * [ |
||
87 | * '(table.)compositeKey1' => 'aliasNameAsInRecord', |
||
88 | * ] |
||
89 | * |
||
90 | * and : |
||
91 | * [ |
||
92 | * '(table.)compositeKey1' => 'aliasNameAsInRecord1', |
||
93 | * '(table.)compositeKey2' => 'aliasNameAsInRecord2', |
||
94 | * // ... |
||
95 | * ] |
||
96 | */ |
||
97 | public function __construct($extractQuery = null, $uniqueKey = 'id') |
||
103 | |||
104 | /** |
||
105 | * get Joiner's ON clause. Only used in Join mode |
||
106 | * |
||
107 | * @return OnClauseInterface |
||
108 | */ |
||
109 | public function getOnClause() |
||
113 | |||
114 | /** |
||
115 | * Set Joiner's ON clause. Only used in Join mode |
||
116 | * |
||
117 | * @param OnClauseInterface $onClause |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setOnClause(OnClauseInterface $onClause) |
||
127 | |||
128 | /** |
||
129 | * register ON clause field mapping. Used by an eventual joiner to this |
||
130 | * |
||
131 | * @param OnClauseInterface $onClause |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function registerJoinerOnClause(OnClauseInterface $onClause) |
||
141 | |||
142 | /** |
||
143 | * @param JoinableInterface $joinFrom |
||
144 | * |
||
145 | * @throws \Exception |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setJoinFrom(JoinableInterface $joinFrom) |
||
163 | |||
164 | /** |
||
165 | * @param string $fromKeyAlias The from unique key to get the map against |
||
166 | * as exposed in the record |
||
167 | * |
||
168 | * @return array [keyValue1, keyValue2, ...] |
||
169 | */ |
||
170 | public function getRecordMap($fromKeyAlias = null) |
||
174 | |||
175 | /** |
||
176 | * @param mixed $param |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function extract($param = null) |
||
217 | |||
218 | /** |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function enforceBatchSize() |
||
259 | |||
260 | /** |
||
261 | * @param mixed $record |
||
262 | * |
||
263 | * @return mixed The result of the join |
||
264 | */ |
||
265 | public function exec($record) |
||
298 | |||
299 | /** |
||
300 | * @param array|string $uniqueKey can be either a unique key name as |
||
301 | * string |
||
302 | * '(table.)compositeKeyName' // ('id' by default) |
||
303 | * |
||
304 | * or an array : |
||
305 | * ['(table.)compositeKey1'] // single unique key |
||
306 | * ['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key |
||
307 | * |
||
308 | * or an associative array in case you are using aliases : |
||
309 | * [ |
||
310 | * '(table.)compositeKey1' => 'aliasNameAsInRecord', |
||
311 | * ] |
||
312 | * |
||
313 | * and : |
||
314 | * [ |
||
315 | * '(table.)compositeKey1' => 'aliasNameAsInRecord1', |
||
316 | * '(table.)compositeKey2' => 'aliasNameAsInRecord2', |
||
317 | * // ... |
||
318 | * ] |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | protected function configureUniqueKey($uniqueKey) |
||
348 | |||
349 | /** |
||
350 | * @param string $keyName |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | protected function cleanUpKeyName($keyName) |
||
358 | |||
359 | /** |
||
360 | * prepare record set to obey join mode eg return record = true |
||
361 | * to break branch execution when no match are found in join more |
||
362 | * or default to be later merged in left join mode |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | protected function setDefaultExtracted() |
||
383 | |||
384 | /** |
||
385 | * @return $this |
||
386 | */ |
||
387 | protected function genRecordMap() |
||
421 | } |
||
422 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..