Complex classes like ContentViewBuilder 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 ContentViewBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ContentViewBuilder implements ViewBuilder |
||
29 | { |
||
30 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
31 | private $repository; |
||
32 | |||
33 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
34 | private $permissionResolver; |
||
35 | |||
36 | /** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator */ |
||
37 | private $viewConfigurator; |
||
38 | |||
39 | /** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector */ |
||
40 | private $viewParametersInjector; |
||
41 | |||
42 | /** @var \Symfony\Component\HttpFoundation\RequestStack */ |
||
43 | private $requestStack; |
||
44 | |||
45 | /** |
||
46 | * Default templates, indexed per viewType (full, line, ...). |
||
47 | * @var array |
||
48 | */ |
||
49 | private $defaultTemplates; |
||
50 | |||
51 | /** @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader */ |
||
52 | private $locationLoader; |
||
53 | |||
54 | public function __construct( |
||
68 | |||
69 | public function matches($argument) |
||
73 | |||
74 | /** |
||
75 | * @param array $parameters |
||
76 | * |
||
77 | * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView|\eZ\Publish\Core\MVC\Symfony\View\View |
||
78 | * If both contentId and locationId parameters are missing |
||
79 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
80 | * If both contentId and locationId parameters are missing |
||
81 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
82 | */ |
||
83 | public function buildView(array $parameters) |
||
167 | |||
168 | /** |
||
169 | * Loads Content with id $contentId. |
||
170 | * |
||
171 | * @param mixed $contentId |
||
172 | * |
||
173 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
174 | * |
||
175 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
176 | */ |
||
177 | private function loadContent($contentId) |
||
181 | |||
182 | /** |
||
183 | * Loads the embedded content with id $contentId. |
||
184 | * Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
||
185 | * if provided. |
||
186 | * |
||
187 | * @param mixed $contentId |
||
188 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
189 | * |
||
190 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
191 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
192 | */ |
||
193 | private function loadEmbeddedContent($contentId, Location $location = null) |
||
218 | |||
219 | /** |
||
220 | * Loads a visible Location. |
||
221 | * @param $locationId |
||
222 | * |
||
223 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
224 | */ |
||
225 | private function loadLocation($locationId) |
||
242 | |||
243 | /** |
||
244 | * Checks if a user can read a content, or view it as an embed. |
||
245 | * |
||
246 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
247 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
248 | * @param bool $isEmbed |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | private function canRead(Content $content, Location $location = null, bool $isEmbed = true): bool |
||
260 | |||
261 | /** |
||
262 | * Checks if the view is an embed one. |
||
263 | * Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
||
264 | * |
||
265 | * @param array $parameters The ViewBuilder parameters array. |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | private function isEmbed($parameters) |
||
280 | } |
||
281 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.