Complex classes like StepMiddlewareService 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 StepMiddlewareService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class StepMiddlewareService implements SingletonInterface |
||
37 | { |
||
38 | use SelfInstantiateTrait; |
||
39 | |||
40 | /** |
||
41 | * @var FormObject |
||
42 | */ |
||
43 | protected $formObject; |
||
44 | |||
45 | /** |
||
46 | * @var Request |
||
47 | */ |
||
48 | protected $request; |
||
49 | |||
50 | /** |
||
51 | * @var StepMiddlewareValidationService |
||
52 | */ |
||
53 | protected $validationService; |
||
54 | |||
55 | /** |
||
56 | * @var FormStepPersistence |
||
57 | */ |
||
58 | protected $persistence; |
||
59 | |||
60 | /** |
||
61 | * @param FormObject $formObject |
||
62 | * @param Request $request |
||
63 | */ |
||
64 | public function reset(FormObject $formObject, Request $request) |
||
73 | |||
74 | /** |
||
75 | * @param Step $currentStep |
||
76 | * @return StepDefinition|null |
||
77 | */ |
||
78 | public function getNextStep(Step $currentStep) |
||
100 | |||
101 | // @todo tmp-delete? |
||
102 | // /** |
||
103 | // * Saves the submitted values in the metadata, for the given step. |
||
104 | // * |
||
105 | // * @param Step $currentStep |
||
106 | // */ |
||
107 | // public function saveStepFormValues(Step $currentStep) |
||
108 | // { |
||
109 | // $this->persistence->addStepFormValues($this->getStepDefinition($currentStep), $this->getFormRawValues()); |
||
110 | // } |
||
111 | // |
||
112 | // /** |
||
113 | // * Fetches the raw values sent in the request. |
||
114 | // * |
||
115 | // * @return array |
||
116 | // * @throws InvalidArgumentTypeException |
||
117 | // */ |
||
118 | // protected function getFormRawValues() |
||
119 | // { |
||
120 | // $formName = $this->getFormObject()->getName(); |
||
121 | // $formArray = null; |
||
122 | // |
||
123 | // if ($this->request->hasArgument($formName)) { |
||
124 | // /** @var array $formArray */ |
||
125 | // $formArray = $this->request->getArgument($formName); |
||
126 | // |
||
127 | // if (false === is_array($formArray)) { |
||
128 | // throw InvalidArgumentTypeException::formArgumentNotArray($this->getFormObject(), $formArray); |
||
129 | // } |
||
130 | // } else { |
||
131 | // $formArray = []; |
||
132 | // } |
||
133 | // |
||
134 | // return $formArray; |
||
135 | // } |
||
136 | // |
||
137 | // /** |
||
138 | // * @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::markStepAsValidated() |
||
139 | // * |
||
140 | // * @param StepDefinition $stepDefinition |
||
141 | // * @param array $formValues |
||
142 | // */ |
||
143 | // public function markStepAsValidated(StepDefinition $stepDefinition, array $formValues) |
||
144 | // { |
||
145 | // $this->validationService->markStepAsValidated($stepDefinition, $formValues); |
||
146 | // } |
||
147 | |||
148 | /** |
||
149 | * @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::addValidatedFields |
||
150 | * |
||
151 | * @param array $validatedFields |
||
152 | */ |
||
153 | public function addValidatedFields(array $validatedFields) |
||
157 | |||
158 | /** |
||
159 | * @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::stepDefinitionIsValid |
||
160 | * |
||
161 | * @param StepDefinition $stepDefinition |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function stepIsValid(StepDefinition $stepDefinition) |
||
168 | |||
169 | /** |
||
170 | * @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::getFirstInvalidStep |
||
171 | * |
||
172 | * @param Step $step |
||
173 | * @return StepDefinition|null |
||
174 | */ |
||
175 | public function getFirstInvalidStep(Step $step) |
||
179 | |||
180 | /** |
||
181 | * @param StepDefinition $step |
||
182 | * @return StepDefinition |
||
183 | */ |
||
184 | public function getNextStepDefinition(StepDefinition $step) |
||
219 | |||
220 | public function getNextSubstepDefinition(SubstepDefinition $substepDefinition) |
||
253 | |||
254 | public function findSubstepDefinition(Step $step, callable $callback) |
||
258 | |||
259 | protected function findSubstepDefinitionRecursive(SubstepDefinition $substepDefinition, callable $callback) |
||
273 | |||
274 | /** |
||
275 | * @param StepDefinition $stepDefinition |
||
276 | * @param Redirect $redirect |
||
277 | */ |
||
278 | public function moveForwardToStep(StepDefinition $stepDefinition, Redirect $redirect) |
||
283 | |||
284 | /** |
||
285 | * Redirects the current request to the given step. |
||
286 | * |
||
287 | * @param Step $step |
||
288 | * @param Redirect $redirect |
||
289 | */ |
||
290 | public function redirectToStep(Step $step, Redirect $redirect) |
||
303 | |||
304 | /** |
||
305 | * @param StepDefinition $stepDefinition |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function getStepDefinitionConditionResult(StepDefinition $stepDefinition) |
||
317 | |||
318 | /** |
||
319 | * @param SubstepDefinition $substepDefinition |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function getSubstepDefinitionConditionResult(SubstepDefinition $substepDefinition) |
||
331 | |||
332 | /** |
||
333 | * @param Step $step |
||
334 | * @return StepDefinition|null |
||
335 | */ |
||
336 | public function getStepDefinition(Step $step) |
||
340 | |||
341 | /** |
||
342 | * @param Step $step |
||
343 | * @param StepDefinition $stepDefinition |
||
344 | * @return StepDefinition|null |
||
345 | */ |
||
346 | protected function findStepDefinition(Step $step, StepDefinition $stepDefinition) |
||
372 | |||
373 | /** |
||
374 | * @return FormStepPersistence |
||
375 | */ |
||
376 | public function getStepPersistence() |
||
380 | |||
381 | /** |
||
382 | * @return FormObject |
||
383 | */ |
||
384 | public function getFormObject() |
||
388 | |||
389 | /** |
||
390 | * @return StepDefinition |
||
391 | */ |
||
392 | public function getFirstStepDefinition() |
||
396 | } |
||
397 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: