| Conditions | 18 |
| Paths | 644 |
| Total Lines | 83 |
| Code Lines | 48 |
| Lines | 6 |
| Ratio | 7.23 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 134 | private function buildHTMLTreeFromNode(DOMDocument $document, Node $node, array $options) { |
||
| 135 | //Create the first wrapping DOMElement. |
||
| 136 | $domElementA = $this->buildElement($document, $options[self::MAIN_TAG_KEY][0]); |
||
| 137 | |||
| 138 | //Merge font size and font family to add inside the second wrapping DOMElement. |
||
| 139 | View Code Duplication | if(!array_key_exists(self::MAIN_TAG_MERGE_STYLE, $options) || !is_bool($options[self::MAIN_TAG_MERGE_STYLE])) { |
|
|
1 ignored issue
–
show
|
|||
| 140 | $options[self::MAIN_TAG_MERGE_STYLE] = true; |
||
| 141 | } |
||
| 142 | if($options[self::MAIN_TAG_MERGE_STYLE]) { |
||
| 143 | $options[self::MAIN_TAG_KEY][1][self::ATTRIBUTES_KEY] = array_merge( |
||
| 144 | $options[self::MAIN_TAG_KEY][1][self::ATTRIBUTES_KEY], |
||
| 145 | [ |
||
| 146 | 'style' => 'color:'.$node->getColor().';'. |
||
| 147 | ($node->getFontName() ? 'font-family:'.$node->getFontName().';' : ''). |
||
| 148 | ($node->getFontSize() ? 'font-size:'.$node->getFontSize().';' : ''), |
||
| 149 | ] |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | $options[self::MAIN_TAG_KEY][1][self::ATTRIBUTES_KEY] = array_merge($options[self::MAIN_TAG_KEY][1][self::ATTRIBUTES_KEY], ['id' => $node->getId(),]); |
||
| 153 | //Create the second wrapping DOMElement whom will be append to the first one |
||
| 154 | $domElementB = $this->buildElement($document, $options[self::MAIN_TAG_KEY][1]); |
||
| 155 | |||
| 156 | //Set the icon in an img tag whom will be wrapped in the second DOMElement. |
||
| 157 | if(!empty($node->getIcon()) && $options[self::MAIN_ICON_KEY][self::DISPLAY_ICON_KEY]) { |
||
| 158 | $icon = $node->getIcon(); |
||
| 159 | $domElementImg = $document->createElement('img'); |
||
| 160 | |||
| 161 | $iconUri = $icon->getShortUri(); |
||
| 162 | if(array_key_exists(self::PATH_ICON_KEY, $options[self::MAIN_ICON_KEY])) { |
||
| 163 | if(!is_callable($callback = $options[self::MAIN_ICON_KEY][self::PATH_ICON_KEY][self::CALLBACK_PATH_ICON_KEY])) { |
||
| 164 | throw new InvalidArgumentException('The argument with the key "'.self::CALLBACK_PATH_ICON_KEY.'" (self::CALLBACK_PATH_ICON_KEY) must be a valid callback.'); |
||
| 165 | } |
||
| 166 | |||
| 167 | $callBackoptions = array_key_exists(self::OPTIONS_PATH_ICON_KEY, $options[self::MAIN_ICON_KEY][self::PATH_ICON_KEY]) ? |
||
| 168 | $options[self::MAIN_ICON_KEY][self::PATH_ICON_KEY][self::OPTIONS_PATH_ICON_KEY] : |
||
| 169 | null; |
||
| 170 | |||
| 171 | $iconUri = $callback($icon->getFullName(), $callBackoptions); |
||
| 172 | } |
||
| 173 | |||
| 174 | $domElementImg->setAttribute('src', $iconUri); |
||
| 175 | $domElementImg->setAttribute('alt', $icon->getName()); |
||
| 176 | $domElementB->appendChild($domElementImg); |
||
| 177 | } |
||
| 178 | |||
| 179 | //Define the TextNode to append to the current wrapping elements. |
||
| 180 | $text = $document->createTextNode($node->getText()); |
||
| 181 | |||
| 182 | //Bold an italic in old HTML way in order not to spread it to all children. |
||
| 183 | View Code Duplication | if(!array_key_exists(self::MAIN_TAG_MERGE_DECORATION, $options) || !is_bool($options[self::MAIN_TAG_MERGE_DECORATION])) { |
|
|
1 ignored issue
–
show
|
|||
| 184 | $options[self::MAIN_TAG_MERGE_DECORATION] = true; |
||
| 185 | } |
||
| 186 | if($options[self::MAIN_TAG_MERGE_DECORATION]) { |
||
| 187 | if($node->isBold()) { |
||
| 188 | $bTag = $document->createElement('b'); |
||
|
1 ignored issue
–
show
|
|||
| 189 | $bTag->appendChild($text); |
||
| 190 | $text = $bTag; |
||
| 191 | } |
||
| 192 | if($node->isItalic()) { |
||
| 193 | $iTag = $document->createElement('i'); |
||
|
1 ignored issue
–
show
|
|||
| 194 | $iTag->appendChild($text); |
||
| 195 | $text = $iTag; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | //Append text node (real one or the one already wrapped inside <b> and/or <i> tags) |
||
| 200 | if(isset($options[self::MAIN_TAG_KEY][2])) { |
||
| 201 | $domElementC = $this->buildElement($document, $options[self::MAIN_TAG_KEY][2]); |
||
| 202 | $domElementC->appendChild($text); |
||
| 203 | $domElementB->appendChild($domElementC); |
||
| 204 | } |
||
| 205 | else { |
||
| 206 | $domElementB->appendChild($text); |
||
| 207 | } |
||
| 208 | |||
| 209 | $domElementA->appendChild($domElementB); |
||
| 210 | |||
| 211 | foreach($node->getChildren() as $child) { |
||
| 212 | $domElementB->appendChild($this->buildHTMLTreeFromNode($document, $child, $options)); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $domElementA; |
||
| 216 | } |
||
| 217 | |||
| 234 | } |