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()) |
||
187 | |||
188 | /** |
||
189 | * Main action for viewing embedded location. |
||
190 | * Response will be cached with HttpCache validation model (Etag). |
||
191 | * |
||
192 | * @param int $locationId |
||
193 | * @param string $viewType |
||
194 | * @param bool $layout |
||
195 | * @param array $params |
||
196 | * |
||
197 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
198 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
199 | * @throws \Exception |
||
200 | * |
||
201 | * @return \Symfony\Component\HttpFoundation\Response |
||
202 | * |
||
203 | * @deprecated Since 6.0.0. Viewing locations is now done with ViewContent. |
||
204 | */ |
||
205 | public function embedLocation($locationId, $viewType, $layout = false, array $params = array()) |
||
271 | |||
272 | /** |
||
273 | * Main action for viewing content. |
||
274 | * Response will be cached with HttpCache validation model (Etag). |
||
275 | * |
||
276 | * @param int $contentId |
||
277 | * @param string $viewType |
||
278 | * @param bool $layout |
||
279 | * @param array $params |
||
280 | * |
||
281 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
282 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
283 | * @throws \Exception |
||
284 | * |
||
285 | * @return \Symfony\Component\HttpFoundation\Response |
||
286 | */ |
||
287 | public function viewContent($contentId, $viewType, $layout = false, array $params = array()) |
||
326 | |||
327 | /** |
||
328 | * Main action for viewing embedded content. |
||
329 | * Response will be cached with HttpCache validation model (Etag). |
||
330 | * |
||
331 | * @param int $contentId |
||
332 | * @param string $viewType |
||
333 | * @param bool $layout |
||
334 | * @param array $params |
||
335 | * |
||
336 | * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
||
337 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
338 | * @throws \Exception |
||
339 | * |
||
340 | * @return \Symfony\Component\HttpFoundation\Response |
||
341 | */ |
||
342 | public function embedContent($contentId, $viewType, $layout = false, array $params = array()) |
||
400 | |||
401 | protected function handleViewException(Response $response, $params, Exception $e, $viewType, $contentId = null, $locationId = null) |
||
425 | |||
426 | /** |
||
427 | * Creates the content to be returned when viewing a Location. |
||
428 | * |
||
429 | * @param Location $location |
||
430 | * @param string $viewType |
||
431 | * @param bool $layout |
||
432 | * @param array $params |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | protected function renderLocation(Location $location, $viewType, $layout = false, array $params = array()) |
||
440 | |||
441 | /** |
||
442 | * Creates the content to be returned when viewing a Content. |
||
443 | * |
||
444 | * @param Content $content |
||
445 | * @param string $viewType |
||
446 | * @param bool $layout |
||
447 | * @param array $params |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | protected function renderContent(Content $content, $viewType, $layout = false, array $params = array()) |
||
455 | |||
456 | /** |
||
457 | * Performs the access checks. |
||
458 | */ |
||
459 | protected function performAccessChecks() |
||
465 | } |
||
466 |
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: