Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 35 | class ViewHandler implements ConfigurableViewHandlerInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Key format, value a callable that returns a Response instance. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $customHandlers = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The supported formats as keys and if the given formats |
||
| 46 | * uses templating is denoted by a true value. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $formats; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * HTTP response status code for a failed validation. |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $failedValidationCode; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * HTTP response status code when the view data is null. |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $emptyContentCode; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Whether or not to serialize null view data. |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $serializeNull; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * If to force a redirect for the given key format, |
||
| 75 | * with value being the status code to use. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $forceRedirects; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $defaultEngine; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $exclusionStrategyGroups = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $exclusionStrategyVersion; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | */ |
||
| 99 | protected $serializeNullStrategy; |
||
| 100 | |||
| 101 | private $urlGenerator; |
||
| 102 | private $serializer; |
||
| 103 | private $templating; |
||
| 104 | private $requestStack; |
||
| 105 | |||
| 106 | private $options; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Constructor. |
||
| 110 | * |
||
| 111 | * @param UrlGeneratorInterface $urlGenerator The URL generator |
||
| 112 | * @param Serializer $serializer |
||
| 113 | * @param EngineInterface|Environment $templating The configured templating engine |
||
| 114 | * @param RequestStack $requestStack The request stack |
||
| 115 | * @param array $formats the supported formats as keys and if the given formats uses templating is denoted by a true value |
||
| 116 | * @param int $failedValidationCode The HTTP response status code for a failed validation |
||
| 117 | * @param int $emptyContentCode HTTP response status code when the view data is null |
||
| 118 | * @param bool $serializeNull Whether or not to serialize null view data |
||
| 119 | 81 | * @param array $forceRedirects If to force a redirect for the given key format, with value being the status code to use |
|
| 120 | * @param string $defaultEngine default engine (twig, php ..) |
||
| 121 | * @param array $options config options |
||
| 122 | */ |
||
| 123 | public function __construct( |
||
| 161 | 7 | ||
| 162 | public static function create( |
||
| 175 | |||
| 176 | 44 | /** |
|
| 177 | * Sets the default serialization groups. |
||
| 178 | 44 | * |
|
| 179 | * @param array|string $groups |
||
| 180 | */ |
||
| 181 | public function setExclusionStrategyGroups($groups) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets the default serialization version. |
||
| 188 | * |
||
| 189 | * @param string $version |
||
| 190 | */ |
||
| 191 | public function setExclusionStrategyVersion($version) |
||
| 195 | 16 | ||
| 196 | 1 | /** |
|
| 197 | * If nulls should be serialized. |
||
| 198 | * |
||
| 199 | 15 | * @param bool $isEnabled |
|
| 200 | 15 | */ |
|
| 201 | public function setSerializeNullStrategy($isEnabled) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function supports($format) |
||
| 213 | |||
| 214 | 55 | /** |
|
| 215 | * Registers a custom handler. |
||
| 216 | 55 | * |
|
| 217 | * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format) |
||
| 218 | 55 | * It can use the public methods of this class to retrieve the needed data and return a |
|
| 219 | 7 | * Response object ready to be sent. |
|
| 220 | * |
||
| 221 | * @param string $format |
||
| 222 | 48 | * @param callable $callable |
|
| 223 | 48 | * |
|
| 224 | 15 | * @throws \InvalidArgumentException |
|
| 225 | */ |
||
| 226 | public function registerHandler($format, $callable) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Gets a response HTTP status code from a View instance. |
||
| 237 | 46 | * |
|
| 238 | * By default it will return 200. However if there is a FormInterface stored for |
||
| 239 | 46 | * the key 'form' in the View's data it will return the failed_validation |
|
| 240 | * configuration if the form instance has errors. |
||
| 241 | * |
||
| 242 | * @param View $view |
||
| 243 | * @param mixed $content |
||
| 244 | * |
||
| 245 | * @return int HTTP status code |
||
| 246 | */ |
||
| 247 | protected function getStatusCode(View $view, $content = null) |
||
| 262 | |||
| 263 | 31 | /** |
|
| 264 | 18 | * If the given format uses the templating system for rendering. |
|
| 265 | 18 | * |
|
| 266 | * @deprecated since 2.8 |
||
| 267 | 31 | * |
|
| 268 | * @param string $format |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | View Code Duplication | public function isFormatTemplating($format) |
|
| 280 | |||
| 281 | /** |
||
| 282 | 40 | * Gets or creates a JMS\Serializer\SerializationContext and initializes it with |
|
| 283 | * the view exclusion strategies, groups & versions if a new context is created. |
||
| 284 | 40 | * |
|
| 285 | 12 | * @param View $view |
|
| 286 | 12 | * |
|
| 287 | * @return Context |
||
| 288 | 40 | */ |
|
| 289 | protected function getSerializationContext(View $view) |
||
| 312 | |||
| 313 | 8 | /** |
|
| 314 | 8 | * Handles a request with the proper handler. |
|
| 315 | 1 | * |
|
| 316 | 1 | * Decides on which handler to use based on the request format. |
|
| 317 | 7 | * |
|
| 318 | 7 | * @param View $view |
|
| 319 | 2 | * @param Request $request |
|
| 320 | 2 | * |
|
| 321 | 2 | * @throws UnsupportedMediaTypeHttpException |
|
| 322 | 2 | * |
|
| 323 | * @return Response |
||
| 324 | */ |
||
| 325 | 8 | public function handle(View $view, Request $request = null) |
|
| 345 | |||
| 346 | 15 | /** |
|
| 347 | 15 | * Creates the Response from the view. |
|
| 348 | 2 | * |
|
| 349 | * @param View $view |
||
| 350 | * @param string $location |
||
| 351 | * @param string $format |
||
| 352 | 2 | * |
|
| 353 | * @return Response |
||
| 354 | */ |
||
| 355 | public function createRedirectResponse(View $view, $location, $format) |
||
| 377 | |||
| 378 | 22 | /** |
|
| 379 | 2 | * Renders the view data with the given template. |
|
| 380 | 2 | * |
|
| 381 | * @deprecated since 2.8 |
||
| 382 | 22 | * |
|
| 383 | 22 | * @param View $view |
|
| 384 | 2 | * @param string $format |
|
| 385 | 2 | * |
|
| 386 | * @return string |
||
| 387 | 22 | */ |
|
| 388 | public function renderTemplate(View $view, $format) |
||
| 414 | 43 | ||
| 415 | 43 | /** |
|
| 416 | * Prepares view data for use by templating engine. |
||
| 417 | 43 | * |
|
| 418 | * @deprecated since 2.8 |
||
| 419 | * |
||
| 420 | * @param View $view |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function prepareTemplateParameters(View $view) |
||
| 449 | 44 | ||
| 450 | 39 | /** |
|
| 451 | 39 | * Handles creation of a Response using either redirection or the templating/serializer service. |
|
| 452 | * |
||
| 453 | 44 | * @param View $view |
|
| 454 | * @param Request $request |
||
| 455 | * @param string $format |
||
| 456 | * |
||
| 457 | * @return Response |
||
| 458 | */ |
||
| 459 | public function createResponse(View $view, Request $request, $format) |
||
| 484 | |||
| 485 | 28 | /** |
|
| 486 | * Initializes a response object that represents the view and holds the view's status code. |
||
| 487 | 28 | * |
|
| 488 | * @param View $view |
||
| 489 | 28 | * @param string $format |
|
| 490 | 22 | * |
|
| 491 | * @return Response |
||
| 492 | */ |
||
| 493 | 6 | private function initResponse(View $view, $format) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Returns the form from the given view if present, false otherwise. |
||
| 523 | * |
||
| 524 | * @param View $view |
||
| 525 | * |
||
| 526 | * @return bool|FormInterface |
||
| 527 | */ |
||
| 528 | protected function getFormFromView(View $view) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns the data from a view. |
||
| 545 | * |
||
| 546 | * @param View $view |
||
| 547 | * |
||
| 548 | * @return mixed|null |
||
| 549 | */ |
||
| 550 | private function getDataFromView(View $view) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Resets internal object state at the end of the request. |
||
| 563 | */ |
||
| 564 | public function reset() |
||
| 570 | } |
||
| 571 |
If you suppress an error, we recommend checking for the error condition explicitly: