From: joshuaruesweg Date: Thu, 2 Sep 2021 09:05:36 +0000 (+0200) Subject: Apply suggestions from code review X-Git-Tag: 5.5.0_Alpha_1~456^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=eb5490168a969c5d256bed61c2229b56506627a0;p=GitHub%2FWoltLab%2FWCF.git Apply suggestions from code review --- diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.5_boxCondtions.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.5_boxCondtions.php index b1fcef9874..8681cc79a2 100644 --- a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.5_boxCondtions.php +++ b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.5_boxCondtions.php @@ -1,7 +1,7 @@ pageIDs = $this->visibleEverywhere = null; - foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) { - foreach ($groupedObjectTypes as $objectTypes) { - if (\is_array($objectTypes)) { - foreach ($objectTypes as $objectType) { - $objectType->getProcessor()->readFormParameters(); - if ($objectType->objectTypeID == $pageConditionObjectTypeID) { - \assert($objectTypes->getProcessor() instanceof MultiPageCondition); - - $data = $objectType->getProcessor()->getData(); - if ($data !== null) { - $this->pageIDs = $data['pageIDs']; - $this->visibleEverywhere = $data['pageIDs_reverseLogic'] ? 1 : 0; - } else { - $this->pageIDs = []; - $this->visibleEverywhere = 1; - } - } - } + foreach ($this->toFlatList($this->groupedConditionObjectTypes) as $objectType) { + $objectType->getProcessor()->readFormParameters(); + if ($objectType->objectTypeID == $pageConditionObjectTypeID) { + \assert($objectType->getProcessor() instanceof MultiPageCondition); + + $data = $objectType->getProcessor()->getData(); + if ($data !== null) { + $this->pageIDs = $data['pageIDs']; + $this->visibleEverywhere = $data['pageIDs_reverseLogic'] ? 1 : 0; } else { - $objectTypes->getProcessor()->readFormParameters(); - if ($objectTypes->objectTypeID == $pageConditionObjectTypeID) { - \assert($objectTypes->getProcessor() instanceof MultiPageCondition); - - $data = $objectTypes->getProcessor()->getData(); - if ($data !== null) { - $this->pageIDs = $data['pageIDs']; - $this->visibleEverywhere = $data['pageIDs_reverseLogic'] ? 1 : 0; - } else { - $this->pageIDs = []; - $this->visibleEverywhere = 1; - } - } + $this->pageIDs = []; + $this->visibleEverywhere = 1; } } } @@ -590,16 +570,8 @@ class BoxAddForm extends AbstractForm $this->pageIDs = $statement->fetchAll(\PDO::FETCH_COLUMN); } - foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) { - foreach ($groupedObjectTypes as $objectTypes) { - if (\is_array($objectTypes)) { - foreach ($objectTypes as $objectType) { - $objectType->getProcessor()->validate(); - } - } else { - $objectTypes->getProcessor()->validate(); - } - } + foreach ($this->toFlatList($this->groupedConditionObjectTypes) as $objectType) { + $objectType->getProcessor()->validate(); } } @@ -706,18 +678,10 @@ class BoxAddForm extends AbstractForm // save acl SimpleAclHandler::getInstance()->setValues('com.woltlab.wcf.box', $box->boxID, $this->aclValues); - // transform conditions array into one-dimensional array - $conditions = []; - foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) { - foreach ($groupedObjectTypes as $objectTypes) { - if (\is_array($objectTypes)) { - $conditions = \array_merge($conditions, $objectTypes); - } else { - $conditions[] = $objectTypes; - } - } - } - ConditionHandler::getInstance()->createConditions($box->boxID, $conditions); + ConditionHandler::getInstance()->createConditions( + $box->boxID, + $this->toFlatList($this->groupedConditionObjectTypes) + ); // call saved event $this->saved(); @@ -742,7 +706,7 @@ class BoxAddForm extends AbstractForm $this->linkPageID = 0; $this->linkPageObjectID = 0; - foreach ($conditions as $condition) { + foreach ($this->toFlatList($this->groupedConditionObjectTypes) as $condition) { $condition->getProcessor()->reset(); } } @@ -826,6 +790,27 @@ class BoxAddForm extends AbstractForm } } + /** + * This is a helper method to convert groupedConditionObjectTypes to a flat list. + * This method should not be used for any other purpose! + * + * @since 5.5 + */ + protected function toFlatList(array $array): array + { + $returnList = []; + + foreach ($array as $element) { + if (\is_array($element)) { + $returnList = \array_merge($returnList, $this->toFlatList($element)); + } else { + $returnList[] = $element; + } + } + + return $returnList; + } + /** * @inheritDoc */ diff --git a/wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php b/wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php index c5e6387671..f7b8ed8157 100644 --- a/wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php @@ -166,22 +166,10 @@ class BoxEditForm extends BoxAddForm SimpleAclHandler::getInstance()->setValues('com.woltlab.wcf.box', $this->box->boxID, $this->aclValues); - // transform conditions array into one-dimensional array - $conditions = []; - foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) { - foreach ($groupedObjectTypes as $objectTypes) { - if (\is_array($objectTypes)) { - $conditions = \array_merge($conditions, $objectTypes); - } else { - $conditions[] = $objectTypes; - } - } - } - ConditionHandler::getInstance()->updateConditions( $this->box->boxID, $this->box->getVisibilityConditions(), - $conditions + $this->toFlatList($this->groupedConditionObjectTypes) ); // call saved event @@ -264,17 +252,9 @@ class BoxEditForm extends BoxAddForm $conditionsByObjectTypeID[$condition->objectTypeID] = $condition; } - foreach ($this->groupedConditionObjectTypes as $objectTypes1) { - foreach ($objectTypes1 as $objectTypes2) { - if (\is_array($objectTypes2)) { - foreach ($objectTypes2 as $objectType) { - if (isset($conditionsByObjectTypeID[$objectType->objectTypeID])) { - $conditionsByObjectTypeID[$objectType->objectTypeID]->getObjectType()->getProcessor()->setData($conditionsByObjectTypeID[$objectType->objectTypeID]); - } - } - } elseif (isset($conditionsByObjectTypeID[$objectTypes2->objectTypeID])) { - $conditionsByObjectTypeID[$objectTypes2->objectTypeID]->getObjectType()->getProcessor()->setData($conditionsByObjectTypeID[$objectTypes2->objectTypeID]); - } + foreach ($this->toFlatList($this->groupedConditionObjectTypes) as $objectType) { + if (isset($conditionsByObjectTypeID[$objectType->objectTypeID])) { + $conditionsByObjectTypeID[$objectType->objectTypeID]->getObjectType()->getProcessor()->setData($conditionsByObjectTypeID[$objectType->objectTypeID]); } } } diff --git a/wcfsetup/install/files/lib/data/box/Box.class.php b/wcfsetup/install/files/lib/data/box/Box.class.php index 424d4e640f..dce62c6b6f 100644 --- a/wcfsetup/install/files/lib/data/box/Box.class.php +++ b/wcfsetup/install/files/lib/data/box/Box.class.php @@ -538,7 +538,7 @@ class Box extends DatabaseObject * Returns the conditions for the box controller. * * @return Condition[] - * @deprecated since 5.5 - use self::getControllerConditions() instead + * @deprecated 5.5 - use self::getControllerConditions() instead */ public function getConditions() { @@ -552,7 +552,7 @@ class Box extends DatabaseObject * @return Condition[] * @since 5.5 */ - public function getControllerConditions() + public function getControllerConditions(): array { /** @noinspection PhpUndefinedMethodInspection */ if ($this->boxType === 'system' && $this->getController() instanceof IConditionBoxController && $this->getController()->getConditionDefinition()) {