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 ViewController 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 ViewController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class ViewController extends Controller |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface |
||
| 41 | */ |
||
| 42 | protected $viewManager; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface |
||
| 46 | */ |
||
| 47 | private $authorizationChecker; |
||
| 48 | |||
| 49 | public function __construct(ViewManagerInterface $viewManager, AuthorizationCheckerInterface $authorizationChecker) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * This is the default view action or a ContentView object. |
||
| 57 | * |
||
| 58 | * It doesn't do anything by itself: the returned View object is rendered by the ViewRendererListener |
||
| 59 | * into an HttpFoundation Response. |
||
| 60 | * |
||
| 61 | * This action can be selectively replaced by a custom action by means of content_view |
||
| 62 | * configuration. Custom actions can add parameters to the view and customize the Response the View will be |
||
| 63 | * converted to. They may also bypass the ViewRenderer by returning an HttpFoundation Response. |
||
| 64 | * |
||
| 65 | * Cache is in both cases handled by the CacheViewResponseListener. |
||
| 66 | * |
||
| 67 | * @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view |
||
| 68 | * |
||
| 69 | * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView |
||
| 70 | */ |
||
| 71 | public function viewAction(ContentView $view) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Embed a content. |
||
| 78 | * Behaves mostly like viewAction(), but with specific content load permission handling. |
||
| 79 | * |
||
| 80 | * @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view |
||
| 81 | * |
||
| 82 | * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView |
||
| 83 | */ |
||
| 84 | public function embedAction(ContentView $view) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Build the response so that depending on settings it's cacheable. |
||
| 91 | * |
||
| 92 | * @param string|null $etag |
||
| 93 | * @param \DateTime|null $lastModified |
||
| 94 | * |
||
| 95 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 96 | */ |
||
| 97 | protected function buildResponse($etag = null, DateTime $lastModified = null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Main action for viewing content through a location in the repository. |
||
| 130 | * Response will be cached with HttpCache validation model (Etag). |
||
| 131 | * |
||
| 132 | * @param int $locationId |
||
| 133 | * @param string $viewType |
||
| 134 | * @param bool $layout |
||
| 135 | * @param array $params |
||
| 136 | * |
||
| 137 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
| 138 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 139 | * @throws \Exception |
||
| 140 | * |
||
| 141 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 142 | * |
||
| 143 | * @deprecated Since 6.0.0. Viewing locations is now done with ViewContent. |
||
| 144 | */ |
||
| 145 | public function viewLocation($locationId, $viewType, $layout = false, array $params = array()) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Main action for viewing embedded location. |
||
| 184 | * Response will be cached with HttpCache validation model (Etag). |
||
| 185 | * |
||
| 186 | * @param int $locationId |
||
| 187 | * @param string $viewType |
||
| 188 | * @param bool $layout |
||
| 189 | * @param array $params |
||
| 190 | * |
||
| 191 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
| 192 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 193 | * @throws \Exception |
||
| 194 | * |
||
| 195 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 196 | * |
||
| 197 | * @deprecated Since 6.0.0. Viewing locations is now done with ViewContent. |
||
| 198 | */ |
||
| 199 | public function embedLocation($locationId, $viewType, $layout = false, array $params = array()) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Main action for viewing content. |
||
| 262 | * Response will be cached with HttpCache validation model (Etag). |
||
| 263 | * |
||
| 264 | * @param int $contentId |
||
| 265 | * @param string $viewType |
||
| 266 | * @param bool $layout |
||
| 267 | * @param array $params |
||
| 268 | * |
||
| 269 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
| 270 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 271 | * @throws \Exception |
||
| 272 | * |
||
| 273 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 274 | */ |
||
| 275 | public function viewContent($contentId, $viewType, $layout = false, array $params = array()) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Main action for viewing embedded content. |
||
| 311 | * Response will be cached with HttpCache validation model (Etag). |
||
| 312 | * |
||
| 313 | * @param int $contentId |
||
| 314 | * @param string $viewType |
||
| 315 | * @param bool $layout |
||
| 316 | * @param array $params |
||
| 317 | * |
||
| 318 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
| 319 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 320 | * @throws \Exception |
||
| 321 | * |
||
| 322 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 323 | */ |
||
| 324 | public function embedContent($contentId, $viewType, $layout = false, array $params = array()) |
||
| 376 | |||
| 377 | protected function handleViewException(Response $response, $params, Exception $e, $viewType, $contentId = null, $locationId = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Creates the content to be returned when viewing a Location. |
||
| 404 | * |
||
| 405 | * @param Location $location |
||
| 406 | * @param string $viewType |
||
| 407 | * @param bool $layout |
||
| 408 | * @param array $params |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | protected function renderLocation(Location $location, $viewType, $layout = false, array $params = array()) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Creates the content to be returned when viewing a Content. |
||
| 419 | * |
||
| 420 | * @param Content $content |
||
| 421 | * @param string $viewType |
||
| 422 | * @param bool $layout |
||
| 423 | * @param array $params |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | protected function renderContent(Content $content, $viewType, $layout = false, array $params = array()) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Performs the access checks. |
||
| 434 | */ |
||
| 435 | protected function performAccessChecks() |
||
| 441 | } |
||
| 442 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: