Complex classes like ViewHandler 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 ViewHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class ViewHandler implements ConfigurableViewHandlerInterface |
||
35 | { |
||
36 | /** |
||
37 | * Key format, value a callable that returns a Response instance. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $customHandlers = []; |
||
42 | |||
43 | /** |
||
44 | * The supported formats as keys and if the given formats |
||
45 | * uses templating is denoted by a true value. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $formats; |
||
50 | |||
51 | /** |
||
52 | * HTTP response status code for a failed validation. |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $failedValidationCode; |
||
57 | |||
58 | /** |
||
59 | * HTTP response status code when the view data is null. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $emptyContentCode; |
||
64 | |||
65 | /** |
||
66 | * Whether or not to serialize null view data. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $serializeNull; |
||
71 | |||
72 | /** |
||
73 | * If to force a redirect for the given key format, |
||
74 | * with value being the status code to use. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $forceRedirects; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $defaultEngine; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $exclusionStrategyGroups = []; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $exclusionStrategyVersion; |
||
94 | |||
95 | /** |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $serializeNullStrategy; |
||
99 | |||
100 | private $urlGenerator; |
||
101 | private $serializer; |
||
102 | private $templating; |
||
103 | private $requestStack; |
||
104 | |||
105 | private $options; |
||
106 | |||
107 | /** |
||
108 | * Constructor. |
||
109 | * |
||
110 | * @param UrlGeneratorInterface $urlGenerator The URL generator |
||
111 | * @param Serializer $serializer |
||
112 | * @param EngineInterface $templating The configured templating engine |
||
113 | * @param RequestStack $requestStack The request stack |
||
114 | * @param array $formats the supported formats as keys and if the given formats uses templating is denoted by a true value |
||
115 | * @param int $failedValidationCode The HTTP response status code for a failed validation |
||
116 | * @param int $emptyContentCode HTTP response status code when the view data is null |
||
117 | * @param bool $serializeNull Whether or not to serialize null view data |
||
118 | * @param array $forceRedirects If to force a redirect for the given key format, with value being the status code to use |
||
119 | 81 | * @param string $defaultEngine default engine (twig, php ..) |
|
120 | * @param array $options config options |
||
121 | */ |
||
122 | public function __construct( |
||
152 | |||
153 | /** |
||
154 | * Sets the default serialization groups. |
||
155 | * |
||
156 | * @param array|string $groups |
||
157 | */ |
||
158 | 7 | public function setExclusionStrategyGroups($groups) |
|
162 | |||
163 | /** |
||
164 | * Sets the default serialization version. |
||
165 | * |
||
166 | * @param string $version |
||
167 | */ |
||
168 | 25 | public function setExclusionStrategyVersion($version) |
|
172 | |||
173 | /** |
||
174 | * If nulls should be serialized. |
||
175 | * |
||
176 | 44 | * @param bool $isEnabled |
|
177 | */ |
||
178 | 44 | public function setSerializeNullStrategy($isEnabled) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | public function supports($format) |
||
190 | |||
191 | /** |
||
192 | * Registers a custom handler. |
||
193 | 16 | * |
|
194 | * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format) |
||
195 | 16 | * It can use the public methods of this class to retrieve the needed data and return a |
|
196 | 1 | * Response object ready to be sent. |
|
197 | * |
||
198 | * @param string $format |
||
199 | 15 | * @param callable $callable |
|
200 | 15 | * |
|
201 | * @throws \InvalidArgumentException |
||
202 | */ |
||
203 | public function registerHandler($format, $callable) |
||
211 | |||
212 | /** |
||
213 | * Gets a response HTTP status code from a View instance. |
||
214 | 55 | * |
|
215 | * By default it will return 200. However if there is a FormInterface stored for |
||
216 | 55 | * the key 'form' in the View's data it will return the failed_validation |
|
217 | * configuration if the form instance has errors. |
||
218 | 55 | * |
|
219 | 7 | * @param View $view |
|
220 | * @param mixed $content |
||
221 | * |
||
222 | 48 | * @return int HTTP status code |
|
223 | 48 | */ |
|
224 | 15 | protected function getStatusCode(View $view, $content = null) |
|
239 | 46 | ||
240 | /** |
||
241 | * If the given format uses the templating system for rendering. |
||
242 | * |
||
243 | * @param string $format |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function isFormatTemplating($format) |
||
251 | |||
252 | 31 | /** |
|
253 | * Gets or creates a JMS\Serializer\SerializationContext and initializes it with |
||
254 | 31 | * the view exclusion strategies, groups & versions if a new context is created. |
|
255 | 31 | * |
|
256 | 1 | * @param View $view |
|
257 | 1 | * |
|
258 | * @return Context |
||
259 | 31 | */ |
|
260 | 5 | protected function getSerializationContext(View $view) |
|
279 | |||
280 | /** |
||
281 | * Handles a request with the proper handler. |
||
282 | 40 | * |
|
283 | * Decides on which handler to use based on the request format. |
||
284 | 40 | * |
|
285 | 12 | * @param View $view |
|
286 | 12 | * @param Request $request |
|
287 | * |
||
288 | 40 | * @throws UnsupportedMediaTypeHttpException |
|
289 | * |
||
290 | 40 | * @return Response |
|
291 | 1 | */ |
|
292 | 1 | public function handle(View $view, Request $request = null) |
|
312 | |||
313 | 8 | /** |
|
314 | 8 | * Creates the Response from the view. |
|
315 | 1 | * |
|
316 | 1 | * @param View $view |
|
317 | 7 | * @param string $location |
|
318 | 7 | * @param string $format |
|
319 | 2 | * |
|
320 | 2 | * @return Response |
|
321 | 2 | */ |
|
322 | 2 | public function createRedirectResponse(View $view, $location, $format) |
|
344 | 15 | ||
345 | /** |
||
346 | 15 | * Renders the view data with the given template. |
|
347 | 15 | * |
|
348 | 2 | * @param View $view |
|
349 | * @param string $format |
||
350 | * |
||
351 | * @return string |
||
352 | 2 | */ |
|
353 | public function renderTemplate(View $view, $format) |
||
375 | 12 | ||
376 | 12 | /** |
|
377 | * Prepares view data for use by templating engine. |
||
378 | 22 | * |
|
379 | 2 | * @param View $view |
|
380 | 2 | * |
|
381 | * @return array |
||
382 | 22 | */ |
|
383 | 22 | public function prepareTemplateParameters(View $view) |
|
404 | 51 | ||
405 | 51 | /** |
|
406 | * Handles creation of a Response using either redirection or the templating/serializer service. |
||
407 | 51 | * |
|
408 | 8 | * @param View $view |
|
409 | * @param Request $request |
||
410 | * @param string $format |
||
411 | 43 | * |
|
412 | * @return Response |
||
413 | 43 | */ |
|
414 | 43 | public function createResponse(View $view, Request $request, $format) |
|
439 | |||
440 | 28 | /** |
|
441 | 28 | * Initializes a response object that represents the view and holds the view's status code. |
|
442 | * |
||
443 | 28 | * @param View $view |
|
444 | 28 | * @param string $format |
|
445 | * |
||
446 | 44 | * @return Response |
|
447 | 44 | */ |
|
448 | private function initResponse(View $view, $format) |
||
475 | 44 | ||
476 | /** |
||
477 | * Returns the form from the given view if present, false otherwise. |
||
478 | * |
||
479 | * @param View $view |
||
480 | * |
||
481 | * @return bool|FormInterface |
||
482 | */ |
||
483 | protected function getFormFromView(View $view) |
||
497 | |||
498 | /** |
||
499 | * Returns the data from a view. |
||
500 | * |
||
501 | * @param View $view |
||
502 | * |
||
503 | * @return mixed|null |
||
504 | */ |
||
505 | private function getDataFromView(View $view) |
||
515 | |||
516 | /** |
||
517 | * Resets internal object state at the end of the request. |
||
518 | */ |
||
519 | public function reset() |
||
525 | } |
||
526 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.