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) |
||
159 | |||
160 | /** |
||
161 | * Loads Content with id $contentId. |
||
162 | * |
||
163 | * @param mixed $contentId |
||
164 | * |
||
165 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
166 | * |
||
167 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
168 | */ |
||
169 | private function loadContent($contentId, ?string $languageCode = null) |
||
176 | |||
177 | /** |
||
178 | * Loads the embedded content with id $contentId. |
||
179 | * Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
||
180 | * if provided. |
||
181 | * |
||
182 | * @param mixed $contentId |
||
183 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
184 | * |
||
185 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
186 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
187 | */ |
||
188 | private function loadEmbeddedContent($contentId, Location $location = null, ?string $languageCode = null) |
||
213 | |||
214 | /** |
||
215 | * Loads a visible Location. |
||
216 | * @param $locationId |
||
217 | * |
||
218 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
219 | */ |
||
220 | private function loadLocation($locationId) |
||
237 | |||
238 | /** |
||
239 | * Checks if a user can read a content, or view it as an embed. |
||
240 | * |
||
241 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
242 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
243 | * @param bool $isEmbed |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | private function canRead(Content $content, Location $location = null, bool $isEmbed = true): bool |
||
255 | |||
256 | /** |
||
257 | * Checks if the view is an embed one. |
||
258 | * Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
||
259 | * |
||
260 | * @param array $parameters The ViewBuilder parameters array. |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | private function isEmbed($parameters) |
||
275 | } |
||
276 |
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.