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 |
||
27 | class ContentViewBuilder implements ViewBuilder |
||
28 | { |
||
29 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
30 | private $repository; |
||
31 | |||
32 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
33 | private $permissionResolver; |
||
34 | |||
35 | /** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator */ |
||
36 | private $viewConfigurator; |
||
37 | |||
38 | /** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector */ |
||
39 | private $viewParametersInjector; |
||
40 | |||
41 | /** @var \Symfony\Component\HttpFoundation\RequestStack */ |
||
42 | private $requestStack; |
||
43 | |||
44 | /** |
||
45 | * Default templates, indexed per viewType (full, line, ...). |
||
46 | * @var array |
||
47 | */ |
||
48 | private $defaultTemplates; |
||
49 | |||
50 | /** @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader */ |
||
51 | private $locationLoader; |
||
52 | |||
53 | public function __construct( |
||
67 | |||
68 | public function matches($argument) |
||
72 | |||
73 | /** |
||
74 | * @param array $parameters |
||
75 | * |
||
76 | * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView|\eZ\Publish\Core\MVC\Symfony\View\View |
||
77 | * If both contentId and locationId parameters are missing |
||
78 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
79 | * If both contentId and locationId parameters are missing |
||
80 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
81 | */ |
||
82 | public function buildView(array $parameters) |
||
157 | |||
158 | /** |
||
159 | * Loads Content with id $contentId. |
||
160 | * |
||
161 | * @param mixed $contentId |
||
162 | * |
||
163 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
164 | * |
||
165 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
166 | */ |
||
167 | private function loadContent($contentId) |
||
171 | |||
172 | /** |
||
173 | * Loads the embedded content with id $contentId. |
||
174 | * Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
||
175 | * if provided. |
||
176 | * |
||
177 | * @param mixed $contentId |
||
178 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
179 | * |
||
180 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
181 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
182 | */ |
||
183 | private function loadEmbeddedContent($contentId, Location $location = null) |
||
208 | |||
209 | /** |
||
210 | * Loads a visible Location. |
||
211 | * @param $locationId |
||
212 | * |
||
213 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
214 | */ |
||
215 | private function loadLocation($locationId) |
||
232 | |||
233 | /** |
||
234 | * Checks if a user can read a content, or view it as an embed. |
||
235 | * |
||
236 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
237 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
238 | * @param bool $isEmbed |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | private function canRead(Content $content, Location $location = null, bool $isEmbed = true): bool |
||
250 | |||
251 | /** |
||
252 | * Checks if the view is an embed one. |
||
253 | * Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
||
254 | * |
||
255 | * @param array $parameters The ViewBuilder parameters array. |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | private function isEmbed($parameters) |
||
270 | } |
||
271 |
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.