| Total Complexity | 40 |
| Total Lines | 382 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like StructuralDBElement 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 StructuralDBElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 43 | abstract class StructuralDBElement extends AttachmentContainingDBElement |
||
| 44 | { |
||
| 45 | const ID_ROOT_ELEMENT = 0; |
||
| 46 | |||
| 47 | //This is a not standard character, so build a const, so a dev can easily use it |
||
| 48 | const PATH_DELIMITER_ARROW = ' → '; |
||
| 49 | |||
| 50 | |||
| 51 | // We can not define the mapping here or we will get an exception. Unfortunatly we have to do the mapping in the |
||
| 52 | // subclasses |
||
| 53 | /** |
||
| 54 | * @var StructuralDBElement[] |
||
| 55 | */ |
||
| 56 | protected $children; |
||
| 57 | /** |
||
| 58 | * @var StructuralDBElement |
||
| 59 | */ |
||
| 60 | protected $parent; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string The comment info for this element |
||
| 64 | * @ORM\Column(type="string", nullable=true) |
||
| 65 | */ |
||
| 66 | protected $comment; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | * @ORM\Column(type="integer", nullable=true) |
||
| 71 | */ |
||
| 72 | protected $parent_id; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $level=0; |
||
| 78 | |||
| 79 | /** @var string[] all names of all parent elements as a array of strings, |
||
| 80 | * the last array element is the name of the element itself */ |
||
| 81 | private $full_path_strings = null; |
||
| 82 | |||
| 83 | /****************************************************************************** |
||
| 84 | * StructuralDBElement constructor. |
||
| 85 | *****************************************************************************/ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if this element is a child of another element (recursive) |
||
| 89 | * |
||
| 90 | * @param StructuralDBElement $another_element the object to compare |
||
| 91 | * IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)! |
||
| 92 | * |
||
| 93 | * @return bool True, if this element is child of $another_element. |
||
| 94 | * |
||
| 95 | * @throws \InvalidArgumentException if there was an error |
||
| 96 | */ |
||
| 97 | public function isChildOf(StructuralDBElement $another_element) |
||
| 98 | { |
||
| 99 | $class_name = \get_class($this); |
||
| 100 | |||
| 101 | //Check if both elements compared, are from the same type: |
||
| 102 | if ($class_name != \get_class($another_element)) { |
||
| 103 | throw new \InvalidArgumentException('isChildOf() funktioniert nur mit Elementen des gleichen Typs!'); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($this->getID() == null) { // this is the root node |
||
| 107 | return false; |
||
| 108 | } else { |
||
| 109 | //If this' parents element, is $another_element, then we are finished |
||
| 110 | return (($this->parent->getID() == $another_element->getID()) |
||
| 111 | || $this->parent->isChildOf($another_element)); //Otherwise, check recursivley |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | /****************************************************************************** |
||
| 117 | * |
||
| 118 | * Getters |
||
| 119 | * |
||
| 120 | ******************************************************************************/ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @brief Get the parent-ID |
||
| 124 | * |
||
| 125 | * @retval integer * the ID of the parent element |
||
| 126 | * * NULL means, the parent is the root node |
||
| 127 | * * the parent ID of the root node is -1 |
||
| 128 | */ |
||
| 129 | public function getParentID() : int |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the parent of this element. |
||
| 136 | * @return StructuralDBElement|null The parent element. Null if this element, does not have a parent. |
||
| 137 | */ |
||
| 138 | public function getParent() : ?self |
||
| 139 | { |
||
| 140 | return $this->parent; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the comment of the element. |
||
| 145 | * |
||
| 146 | * @param boolean $parse_bbcode Should BBCode converted to HTML, before returning |
||
| 147 | * @return string the comment |
||
| 148 | */ |
||
| 149 | public function getComment(bool $parse_bbcode = true) : string |
||
| 150 | { |
||
| 151 | $val = htmlspecialchars($this->comment ?? ''); |
||
| 152 | if ($parse_bbcode) { |
||
| 153 | //$bbcode = new BBCodeParser(); |
||
| 154 | //$val = $bbcode->parse($val); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $val; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the level |
||
| 162 | * |
||
| 163 | * The level of the root node is -1. |
||
| 164 | * |
||
| 165 | * @return integer the level of this element (zero means a most top element |
||
| 166 | * [a subelement of the root node]) |
||
| 167 | * |
||
| 168 | */ |
||
| 169 | public function getLevel() : int |
||
| 170 | { |
||
| 171 | if ($this->level === 0) { |
||
| 172 | $element = $this->parent; |
||
| 173 | $parent_id = $element->getParentID(); |
||
| 174 | while ($parent_id > 0) { |
||
| 175 | /** @var StructuralDBElement $element */ |
||
| 176 | $element = $element->parent; |
||
| 177 | $parent_id = $element->getParentID(); |
||
| 178 | $this->level++; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return $this->level; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the full path |
||
| 187 | * |
||
| 188 | * @param string $delimeter the delimeter of the returned string |
||
| 189 | * |
||
| 190 | * @return string the full path (incl. the name of this element), delimeted by $delimeter |
||
| 191 | * |
||
| 192 | * @throws Exception if there was an error |
||
| 193 | */ |
||
| 194 | public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW) : string |
||
| 195 | { |
||
| 196 | if (! \is_array($this->full_path_strings)) { |
||
|
|
|||
| 197 | $this->full_path_strings = array(); |
||
| 198 | $this->full_path_strings[] = $this->getName(); |
||
| 199 | $element = $this; |
||
| 200 | |||
| 201 | while ($element->parent != null) { |
||
| 202 | $element = $element->parent; |
||
| 203 | $this->full_path_strings[] = $element->getName(); |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->full_path_strings = array_reverse($this->full_path_strings); |
||
| 207 | } |
||
| 208 | |||
| 209 | return implode($delimeter, $this->full_path_strings); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get all subelements of this element |
||
| 214 | * |
||
| 215 | * @param boolean $recursive if true, the search is recursive |
||
| 216 | * |
||
| 217 | * @return static[] all subelements as an array of objects (sorted by their full path) |
||
| 218 | */ |
||
| 219 | public function getSubelements(bool $recursive) : PersistentCollection |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /****************************************************************************** |
||
| 239 | * |
||
| 240 | * Setters |
||
| 241 | * |
||
| 242 | ******************************************************************************/ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Change the parent ID of this element |
||
| 246 | * |
||
| 247 | * @param integer|null $new_parent_id * the ID of the new parent element |
||
| 248 | * * NULL if the parent should be the root node |
||
| 249 | */ |
||
| 250 | public function setParentID($new_parent_id) : self |
||
| 251 | { |
||
| 252 | $this->parent_id = $new_parent_id; |
||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set the comment |
||
| 258 | * |
||
| 259 | * @param string $new_comment the new comment |
||
| 260 | * @throws Exception if there was an error |
||
| 261 | */ |
||
| 262 | public function setComment(string $new_comment) : self |
||
| 266 | } |
||
| 267 | |||
| 268 | /******************************************************************************** |
||
| 269 | * |
||
| 270 | * Tree / Table Builders |
||
| 271 | * |
||
| 272 | *********************************************************************************/ |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Build a HTML tree with all subcategories of this element |
||
| 276 | * |
||
| 277 | * This method prints a <option>-Line for every item. |
||
| 278 | * <b>The <select>-tags are not printed here, you have to print them yourself!</b> |
||
| 279 | * Deeper levels have more spaces in front. |
||
| 280 | * |
||
| 281 | * @param integer $selected_id the ID of the selected item |
||
| 282 | * @param boolean $recursive if true, the tree will be recursive |
||
| 283 | * @param boolean $show_root if true, the root node will be displayed |
||
| 284 | * @param string $root_name if the root node is the very root element, you can set its name here |
||
| 285 | * @param string $value_prefix This string is used as a prefix before the id in the value part of the option. |
||
| 286 | * |
||
| 287 | * @return string HTML string if success |
||
| 288 | * |
||
| 289 | * @throws Exception if there was an error |
||
| 290 | */ |
||
| 291 | public function buildHtmlTree( |
||
| 292 | $selected_id = null, |
||
| 293 | bool $recursive = true, |
||
| 294 | bool $show_root = true, |
||
| 295 | string $root_name = '$$', |
||
| 296 | string $value_prefix = '' |
||
| 297 | ) : string { |
||
| 298 | if ($root_name == '$$') { |
||
| 299 | $root_name = _('Oberste Ebene'); |
||
| 300 | } |
||
| 301 | |||
| 302 | $html = array(); |
||
| 303 | |||
| 304 | if ($show_root) { |
||
| 305 | $root_level = $this->getLevel(); |
||
| 306 | if ($this->getID() > 0) { |
||
| 307 | $root_name = htmlspecialchars($this->getName()); |
||
| 308 | } |
||
| 309 | |||
| 310 | $html[] = '<option value="'. $value_prefix . $this->getID() . '">' . $root_name . '</option>'; |
||
| 311 | } else { |
||
| 312 | $root_level = $this->getLevel() + 1; |
||
| 313 | } |
||
| 314 | |||
| 315 | // get all subelements |
||
| 316 | $subelements = $this->getSubelements($recursive); |
||
| 317 | |||
| 318 | foreach ($subelements as $element) { |
||
| 319 | $level = $element->getLevel() - $root_level; |
||
| 320 | $selected = ($element->getID() == $selected_id) ? 'selected' : ''; |
||
| 321 | |||
| 322 | $html[] = '<option ' . $selected . ' value="' . $value_prefix . $element->getID() . '">'; |
||
| 323 | for ($i = 0; $i < $level; $i++) { |
||
| 324 | $html[] = ' '; |
||
| 325 | } |
||
| 326 | $html[] = htmlspecialchars($element->getName()) . '</option>'; |
||
| 327 | } |
||
| 328 | |||
| 329 | return implode("\n", $html); |
||
| 330 | } |
||
| 331 | |||
| 332 | |||
| 333 | public function buildBootstrapTree( |
||
| 334 | $page, |
||
| 335 | $parameter, |
||
| 336 | $recursive = false, |
||
| 337 | $show_root = false, |
||
| 338 | $use_db_root_name = true, |
||
| 339 | $root_name = '$$' |
||
| 340 | ): array |
||
| 341 | { |
||
| 342 | if ($root_name == '$$') { |
||
| 343 | $root_name = _('Oberste Ebene'); |
||
| 344 | } |
||
| 345 | |||
| 346 | $subelements = $this->getSubelements(false); |
||
| 347 | $nodes = array(); |
||
| 348 | |||
| 349 | foreach ($subelements as $element) { |
||
| 350 | $nodes[] = $element->buildBootstrapTree($page, $parameter); |
||
| 351 | } |
||
| 352 | |||
| 353 | // if we are on root level? |
||
| 354 | if ($this->getParentID() == -1) { |
||
| 355 | if ($show_root) { |
||
| 356 | $tree = array( |
||
| 357 | array('text' => $use_db_root_name ? htmlspecialchars($this->getName()) : $root_name , |
||
| 358 | 'href' => $page . '?' . $parameter . '=' . $this->getID(), |
||
| 359 | 'nodes' => $nodes) |
||
| 360 | ); |
||
| 361 | } else { //Dont show root node |
||
| 362 | $tree = $nodes; |
||
| 363 | } |
||
| 364 | } elseif (!empty($nodes)) { |
||
| 365 | $tree = array('text' => htmlspecialchars($this->getName()), |
||
| 366 | 'href' => $page . '?' . $parameter . '=' . $this->getID(), |
||
| 367 | 'nodes' => $nodes |
||
| 368 | ); |
||
| 369 | } else { |
||
| 370 | $tree = array('text' => htmlspecialchars($this->getName()), |
||
| 371 | 'href' => $page . '?' . $parameter . '=' . $this->getID() |
||
| 372 | ); |
||
| 373 | } |
||
| 374 | |||
| 375 | |||
| 376 | return $tree; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Creates a template loop for a Breadcrumb bar, representing the structural DB element. |
||
| 381 | * @param $page string The base page, to which the breadcrumb links should be directing to. |
||
| 382 | * @param $parameter string The parameter, which selects the ID of the StructuralDBElement. |
||
| 383 | * @param bool $show_root Show the root as its own breadcrumb. |
||
| 384 | * @param string $root_name The label which should be used for the root breadcrumb. |
||
| 385 | * @return array An Loop containing multiple arrays, which contains href and caption for the breadcrumb. |
||
| 386 | */ |
||
| 387 | public function buildBreadcrumbLoop(string $page, string $parameter, bool $show_root = false, $root_name = '$$', bool $element_is_link = false) : array |
||
| 425 | } |
||
| 426 | |||
| 427 | } |