Complex classes like StepChainBuilder 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 StepChainBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class StepChainBuilder |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $rootContextClass; |
||
13 | |||
14 | /** |
||
15 | * @var StepRegistration[] |
||
16 | */ |
||
17 | private $additions = []; |
||
18 | |||
19 | /** |
||
20 | * @var StepReplacement[] |
||
21 | */ |
||
22 | private $replacements = []; |
||
23 | |||
24 | /** |
||
25 | * @var StepRemoval[] |
||
26 | */ |
||
27 | private $removals = []; |
||
28 | |||
29 | /** |
||
30 | * @param string $rootContextClass |
||
31 | * @param StepRegistration[] $additions |
||
32 | * @param StepReplacement[] $replacements |
||
33 | * @param StepRemoval[] $removals |
||
34 | */ |
||
35 | 13 | public function __construct($rootContextClass, array $additions, array $replacements, array $removals) |
|
42 | |||
43 | /** |
||
44 | * @return StepRegistration[] |
||
45 | * |
||
46 | * @throws PipelineBuildingException |
||
47 | */ |
||
48 | 11 | public function build() |
|
122 | |||
123 | /** |
||
124 | * @param StepRegistration[] $registrations |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 7 | private function groupRegistrationsByStage(array $registrations) |
|
145 | |||
146 | /** |
||
147 | * @param StepRegistration[] $currentStage |
||
148 | * @param bool $isConnector |
||
149 | * |
||
150 | * @return StepRegistration[] |
||
151 | */ |
||
152 | 6 | private function getStepsFromStage(array $currentStage, $isConnector = false) |
|
167 | |||
168 | /** |
||
169 | * @param StepRegistration[] $currentStage |
||
170 | * |
||
171 | * @return StepRegistration[] |
||
172 | */ |
||
173 | 6 | private function getConnectorsFromStage(array $currentStage) |
|
177 | |||
178 | /** |
||
179 | * @param StepRegistration $stageConnector |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 4 | private function isTerminator($stageConnector) |
|
187 | |||
188 | /** |
||
189 | * @param StepRegistration[] $stageSteps |
||
190 | * |
||
191 | * @return StepRegistration[] |
||
192 | */ |
||
193 | 6 | private function sort(array $stageSteps) |
|
202 | |||
203 | /** |
||
204 | * @param string $fqcn |
||
205 | * @param string $fqin |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | 6 | private function isImplementing($fqcn, $fqin) |
|
214 | |||
215 | /** |
||
216 | * @throws PipelineBuildingException |
||
217 | */ |
||
218 | 11 | private function assertAdditionsAreUnique() |
|
232 | |||
233 | /** |
||
234 | * @param StepRegistration[] $registrations |
||
235 | * |
||
236 | * @throws PipelineBuildingException |
||
237 | */ |
||
238 | 10 | private function assertReplacementsAreValid(array $registrations) |
|
248 | |||
249 | /** |
||
250 | * @param StepRegistration[] $registrations |
||
251 | * |
||
252 | * @throws PipelineBuildingException |
||
253 | */ |
||
254 | 9 | private function assertRemovalsAreValid(array $registrations) |
|
264 | |||
265 | /** |
||
266 | * @param StepRegistration[] $registrations |
||
267 | * |
||
268 | * @throws PipelineBuildingException |
||
269 | */ |
||
270 | 8 | private function assertRemovalsDoNotAffectDependencies(array $registrations) |
|
288 | |||
289 | /** |
||
290 | * @param string $contextClass |
||
291 | * @param array $stages |
||
292 | * |
||
293 | * @throws PipelineBuildingException |
||
294 | */ |
||
295 | 7 | private function assertContextStageExists($contextClass, $stages) |
|
303 | |||
304 | /** |
||
305 | * @param string $stageName |
||
306 | * @param StepRegistration[] $stageConnectors |
||
307 | * |
||
308 | */ |
||
309 | 6 | private function assertThereIsAtMostOneConnectorPerStage($stageName, $stageConnectors) |
|
323 | |||
324 | /** |
||
325 | * @param int $stageNumber |
||
326 | * @param int $stageCount |
||
327 | * @param string $stageName |
||
328 | * @param StepRegistration[] $stageConnectors |
||
329 | * |
||
330 | */ |
||
331 | 5 | private function assertThereIsAtLeastOneConnectorPerIntermediaryStage( |
|
341 | } |
||
342 | |||
343 |