| Total Complexity | 40 |
| Total Lines | 502 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Page 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.
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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Page extends Base |
||
| 41 | { |
||
| 42 | const BTN_CONTENT_PUBLISH_AND_EDIT = 'website:publishAndEdit'; |
||
| 43 | const BTN_CONTENT_SAVE_AS_DRAFT_AND_EDIT = 'website:saveAsDraftAndEdit'; |
||
| 44 | |||
| 45 | const BTN_ID_DRAFT = 'draft-btn'; |
||
| 46 | const BTN_ID_PUBLISH = 'publish-btn'; |
||
| 47 | |||
| 48 | /** @var PageCategoryRepo */ |
||
| 49 | protected $categoryRepo; |
||
| 50 | |||
| 51 | /** @var PageLayoutRepo */ |
||
| 52 | protected $layoutRepo; |
||
| 53 | |||
| 54 | /** @var MetaFactory */ |
||
| 55 | protected $metaFactory; |
||
| 56 | |||
| 57 | /** @var AssetsFactory */ |
||
| 58 | protected $assetsFactory; |
||
| 59 | |||
| 60 | /** @var Enforcer */ |
||
| 61 | protected $enforcer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Page constructor. |
||
| 65 | * |
||
| 66 | * @param ISession $session |
||
| 67 | * @param ITranslator $translator |
||
| 68 | * @param PageCategoryRepo $categoryRepo |
||
| 69 | * @param PageLayoutRepo $layoutRepo |
||
| 70 | * @param MetaFactory $metaFactory |
||
| 71 | * @param AssetsFactory $assetsFactory |
||
| 72 | * @param Enforcer $enforcer |
||
| 73 | */ |
||
| 74 | public function __construct( |
||
| 75 | ISession $session, |
||
| 76 | ITranslator $translator, |
||
| 77 | PageCategoryRepo $categoryRepo, |
||
| 78 | PageLayoutRepo $layoutRepo, |
||
| 79 | MetaFactory $metaFactory, |
||
| 80 | AssetsFactory $assetsFactory, |
||
| 81 | Enforcer $enforcer |
||
| 82 | ) { |
||
| 83 | parent::__construct($session, $translator); |
||
| 84 | |||
| 85 | $this->categoryRepo = $categoryRepo; |
||
| 86 | $this->layoutRepo = $layoutRepo; |
||
| 87 | $this->metaFactory = $metaFactory; |
||
| 88 | $this->assetsFactory = $assetsFactory; |
||
| 89 | $this->enforcer = $enforcer; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $action |
||
| 94 | * @param string $method |
||
| 95 | * @param string $showUrl |
||
| 96 | * @param IEntity|null $entity |
||
| 97 | * |
||
| 98 | * @return IForm |
||
| 99 | */ |
||
| 100 | public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm |
||
| 101 | { |
||
| 102 | assert($entity instanceof Entity, new \InvalidArgumentException()); |
||
| 103 | |||
| 104 | $username = $this->session->get(Session::USERNAME); |
||
| 105 | $advancedAllowed = $this->enforcer->enforce( |
||
| 106 | $username, |
||
| 107 | Authorization::RESOURCE_PAGES, |
||
| 108 | Authorization::ROLE_ADVANCED_WRITE |
||
| 109 | ); |
||
| 110 | |||
| 111 | $this->createForm($action, $method) |
||
| 112 | ->addDefaultElements() |
||
| 113 | ->addTitle($entity) |
||
| 114 | ->addIdentifier($entity) |
||
| 115 | ->addDescription($entity) |
||
| 116 | ->addMeta($entity) |
||
| 117 | ->addLede($entity) |
||
| 118 | ->addBody($entity) |
||
| 119 | ->addCategoryId($entity) |
||
| 120 | ->addLayoutId($entity, $advancedAllowed) |
||
| 121 | ->addLayout($entity, $advancedAllowed) |
||
| 122 | ->addAssets($entity, $advancedAllowed) |
||
| 123 | ->addIsDraft($entity) |
||
| 124 | ->addCustomButtons($showUrl); |
||
| 125 | |||
| 126 | $form = $this->form; |
||
| 127 | |||
| 128 | $this->form = null; |
||
| 129 | |||
| 130 | return $form; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Entity $entity |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | protected function addTitle(Entity $entity): Page |
||
| 139 | { |
||
| 140 | $input = new Input('title', 'title', $entity->getTitle()); |
||
| 141 | $label = new Label('title', 'website:pageTitle'); |
||
| 142 | |||
| 143 | $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]); |
||
| 144 | |||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param Entity $entity |
||
| 150 | * |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | protected function addIdentifier(Entity $entity): Page |
||
| 154 | { |
||
| 155 | $input = new Input( |
||
| 156 | 'identifier', |
||
| 157 | 'identifier', |
||
| 158 | $entity->getIdentifier(), |
||
| 159 | [], |
||
| 160 | [Html5::ATTR_CLASS => 'semi-auto'] |
||
| 161 | ); |
||
| 162 | $label = new Label('identifier', 'website:pageIdentifier'); |
||
| 163 | $help = new Help('website:pageIdentifierHelp'); |
||
| 164 | |||
| 165 | $this->form[] = new FormGroup($input, $label, $help); |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param Entity $entity |
||
| 172 | * |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | protected function addDescription(Entity $entity): Page |
||
| 176 | { |
||
| 177 | $input = new Textarea('description', 'description', $entity->getMeta()->getDescription()); |
||
| 178 | $label = new Countable('description', 'website:pageDescription', Countable::DEFAULT_SIZE); |
||
| 179 | $help = new Help('website:pageDescriptionHelp'); |
||
| 180 | |||
| 181 | $this->form[] = new FormGroup( |
||
| 182 | $input, |
||
| 183 | $label, |
||
| 184 | $help, |
||
| 185 | [], |
||
| 186 | [Html5::ATTR_CLASS => FormGroup::CLASS_COUNTABLE] |
||
| 187 | ); |
||
| 188 | |||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param Entity $entity |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | protected function addMeta(Entity $entity): Page |
||
| 198 | { |
||
| 199 | $hideable = new Hideable($this->translator->translate('website:pageMetaBtn')); |
||
| 200 | foreach ($this->metaFactory->create($entity) as $component) { |
||
| 201 | $hideable[] = $component; |
||
| 202 | } |
||
| 203 | |||
| 204 | $this->form[] = $hideable; |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param Entity $entity |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | protected function addLede(Entity $entity): Page |
||
| 215 | { |
||
| 216 | $attribs = [Html5::ATTR_ROWS => '10']; |
||
| 217 | $input = new Textarea('lede', 'lede', $entity->getLede(), [], $attribs); |
||
| 218 | $label = new Label('lede', 'website:pageLede'); |
||
| 219 | $help = new Help('website:pageLedeHelp'); |
||
| 220 | |||
| 221 | $this->form[] = new FormGroup($input, $label, $help); |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Entity $entity |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | protected function addBody(Entity $entity): Page |
||
| 232 | { |
||
| 233 | $attribs = [Html5::ATTR_CLASS => 'wysiwyg', Html5::ATTR_ROWS => '15']; |
||
| 234 | $input = new Textarea('body', 'body', $entity->getBody(), [], $attribs); |
||
| 235 | $label = new Label('body', 'website:pageBody'); |
||
| 236 | |||
| 237 | $this->form[] = new FormGroup($input, $label); |
||
| 238 | |||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param Entity $entity |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | protected function addCategoryId(Entity $entity): Page |
||
| 248 | { |
||
| 249 | $allCategories = $this->getAllCategories(); |
||
| 250 | $categoryId = $entity->getCategory() ? $entity->getCategory()->getId() : null; |
||
| 251 | |||
| 252 | $options = $this->createCategoryIdOptions($allCategories, $categoryId); |
||
| 253 | |||
| 254 | $this->form[] = new FormGroup( |
||
| 255 | $this->createCategoryIdSelect($options), |
||
| 256 | $this->createCategoryIdLabel() |
||
| 257 | ); |
||
| 258 | |||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param Option[] $options |
||
| 264 | * |
||
| 265 | * @return Select |
||
| 266 | */ |
||
| 267 | protected function createCategoryIdSelect(array $options): Select |
||
| 268 | { |
||
| 269 | $select = new Select('category_id', 'category_id'); |
||
| 270 | |||
| 271 | foreach ($options as $option) { |
||
| 272 | $select[] = $option; |
||
| 273 | } |
||
| 274 | |||
| 275 | return $select; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return Label |
||
| 280 | */ |
||
| 281 | protected function createCategoryIdLabel(): Label |
||
| 282 | { |
||
| 283 | return new Label('category_id', 'website:pageCategoryIdLabel'); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return PageCategory[] |
||
| 288 | */ |
||
| 289 | protected function getAllCategories(): array |
||
| 290 | { |
||
| 291 | return $this->categoryRepo->getAll(); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param PageCategory[] $allCategories |
||
| 296 | * @param string|null $categoryId |
||
| 297 | * |
||
| 298 | * @return Option[] |
||
| 299 | */ |
||
| 300 | protected function createCategoryIdOptions(array $allCategories, ?string $categoryId): array |
||
| 301 | { |
||
| 302 | $options = []; |
||
| 303 | $options[] = new Option('', 'framework:none', false); |
||
| 304 | foreach ($allCategories as $category) { |
||
| 305 | $isSelected = $category->getId() === $categoryId; |
||
| 306 | $options[] = new Option($category->getId(), $category->getName(), $isSelected); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $options; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param Entity $entity |
||
| 314 | * @param bool $advancedAllowed |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | protected function addLayoutId(Entity $entity, bool $advancedAllowed): Page |
||
| 319 | { |
||
| 320 | if (!$advancedAllowed && $entity->getId() && !$entity->getLayoutId()) { |
||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | $allLayouts = $this->getAllLayouts(); |
||
| 325 | $layoutId = $entity->getLayoutId(); |
||
| 326 | |||
| 327 | $options = $this->createLayoutIdOptions($allLayouts, $layoutId, $advancedAllowed); |
||
| 328 | |||
| 329 | $this->form[] = new FormGroup( |
||
| 330 | $this->createLayoutIdSelect($options), |
||
| 331 | $this->createLayoutIdLabel() |
||
| 332 | ); |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return PageLayout[] |
||
| 339 | */ |
||
| 340 | protected function getAllLayouts(): array |
||
| 341 | { |
||
| 342 | return $this->layoutRepo->getAll(); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param PageLayout[] $allLayouts |
||
| 347 | * @param string|null $layoutId |
||
| 348 | * @param bool $advancedAllowed |
||
| 349 | * |
||
| 350 | * @return Option[] |
||
| 351 | */ |
||
| 352 | protected function createLayoutIdOptions(array $allLayouts, ?string $layoutId, bool $advancedAllowed): array |
||
| 353 | { |
||
| 354 | $options = []; |
||
| 355 | if ($advancedAllowed) { |
||
| 356 | $options[] = new Option('', 'framework:none', false); |
||
| 357 | } |
||
| 358 | foreach ($allLayouts as $layout) { |
||
| 359 | $isSelected = $layout->getId() === $layoutId; |
||
| 360 | $options[] = new Option($layout->getId(), $layout->getName(), $isSelected); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $options; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param Option[] $options |
||
| 368 | * |
||
| 369 | * @return Select |
||
| 370 | */ |
||
| 371 | protected function createLayoutIdSelect(array $options): Select |
||
| 372 | { |
||
| 373 | $select = new Select('layout_id', 'layout_id'); |
||
| 374 | |||
| 375 | foreach ($options as $option) { |
||
| 376 | $select[] = $option; |
||
| 377 | } |
||
| 378 | |||
| 379 | return $select; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return Label |
||
| 384 | */ |
||
| 385 | protected function createLayoutIdLabel(): Label |
||
| 386 | { |
||
| 387 | return new Label('layout_id', 'website:pageLayoutIdLabel'); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param Entity $entity |
||
| 392 | * @param bool $advancedAllowed |
||
| 393 | * |
||
| 394 | * @return Page |
||
| 395 | */ |
||
| 396 | protected function addLayout(Entity $entity, bool $advancedAllowed): Page |
||
| 397 | { |
||
| 398 | if (!$advancedAllowed) { |
||
| 399 | return $this; |
||
| 400 | } |
||
| 401 | |||
| 402 | return $this->addLayoutTextarea($entity); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param Entity $entity |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | protected function addLayoutTextarea(Entity $entity): Page |
||
| 411 | { |
||
| 412 | $input = new Textarea('layout', 'layout', $entity->getLayout(), [], [Html5::ATTR_ROWS => '15']); |
||
| 413 | $label = new Label('layout', 'website:pageLayoutLabel'); |
||
| 414 | |||
| 415 | $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_ID => 'layout-div']); |
||
| 416 | |||
| 417 | return $this; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param Entity $entity |
||
| 422 | * @param bool $advancedAllowed |
||
| 423 | * |
||
| 424 | * @return $this |
||
| 425 | */ |
||
| 426 | protected function addAssets(Entity $entity, bool $advancedAllowed): Page |
||
| 427 | { |
||
| 428 | if (!$advancedAllowed) { |
||
| 429 | return $this; |
||
| 430 | } |
||
| 431 | |||
| 432 | $nodes = $this->assetsFactory->create($entity); |
||
| 433 | if (empty($nodes)) { |
||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | $container = new Hideable($this->translator->translate('website:pageAssetsBtn')); |
||
| 438 | foreach ($nodes as $node) { |
||
| 439 | $container[] = $node; |
||
| 440 | } |
||
| 441 | |||
| 442 | $this->form[] = $container; |
||
| 443 | |||
| 444 | return $this; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param Entity $entity |
||
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | protected function addIsDraft(Entity $entity): Page |
||
| 453 | { |
||
| 454 | $attributes = [Html5::ATTR_TYPE => Input::TYPE_CHECKBOX]; |
||
| 455 | if ($entity->isDraft()) { |
||
| 456 | $attributes[Html5::ATTR_CHECKED] = null; |
||
| 457 | } |
||
| 458 | $input = new Input( |
||
| 459 | 'is_draft', |
||
| 460 | 'is_draft', |
||
| 461 | '1', |
||
| 462 | [], |
||
| 463 | $attributes |
||
| 464 | ); |
||
| 465 | $label = new Label('is_draft', 'website:pageIsDraft'); |
||
| 466 | $help = new Component('website:pageIsDraft'); |
||
| 467 | |||
| 468 | $this->form[] = new CheckboxGroup($input, $label, $help, [], [Html5::ATTR_ID => 'is-draft-container']); |
||
| 469 | |||
| 470 | return $this; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param string $showUrl |
||
| 475 | * |
||
| 476 | * @return Base |
||
| 477 | */ |
||
| 478 | protected function addCustomButtons(string $showUrl): Base |
||
| 495 | } |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @param DefaultButtons $buttons |
||
| 500 | * |
||
| 501 | * @return DefaultButtons |
||
| 502 | */ |
||
| 503 | public function addPublishAndEdit(DefaultButtons $buttons): DefaultButtons |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param DefaultButtons $buttons |
||
| 523 | * |
||
| 524 | * @return DefaultButtons |
||
| 525 | */ |
||
| 526 | public function addSaveAsDraftAndBack(DefaultButtons $buttons): DefaultButtons |
||
| 542 | } |
||
| 543 | } |
||
| 544 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: