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) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Loads Content with id $contentId. |
||
| 161 | * |
||
| 162 | * @param mixed $contentId |
||
| 163 | * |
||
| 164 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 165 | * |
||
| 166 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
| 167 | */ |
||
| 168 | private function loadContent($contentId) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Loads the embedded content with id $contentId. |
||
| 175 | * Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
||
| 176 | * if provided. |
||
| 177 | * |
||
| 178 | * @param mixed $contentId |
||
| 179 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 180 | * |
||
| 181 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 182 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
| 183 | */ |
||
| 184 | private function loadEmbeddedContent($contentId, Location $location = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Loads a visible Location. |
||
| 212 | * @param $locationId |
||
| 213 | * |
||
| 214 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 215 | */ |
||
| 216 | private function loadLocation($locationId) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Checks if a user can read a content, or view it as an embed. |
||
| 236 | * |
||
| 237 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 238 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 239 | * @param bool $isEmbed |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | private function canRead(Content $content, Location $location = null, bool $isEmbed = true): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Checks if the view is an embed one. |
||
| 254 | * Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
||
| 255 | * |
||
| 256 | * @param array $parameters The ViewBuilder parameters array. |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | private function isEmbed($parameters) |
||
| 271 | } |
||
| 272 |
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.