| Conditions | 9 |
| Paths | 60 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 82 | public function add(Node $node) |
||
| 83 | { |
||
| 84 | $wrapperElement = null; |
||
| 85 | |||
| 86 | if ($node->schemaDefinition && $node->globalSchemaLocation) { |
||
| 87 | $this->setSchemaDefinition( |
||
| 88 | $node->getSchemaLocation() |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $nodeElement = $this->document->createElement($node->getNodeName()); |
||
| 93 | $this->setAttr($nodeElement, $node->getAttr()); |
||
| 94 | |||
| 95 | foreach ($node->element->childNodes as $child) { |
||
| 96 | $nodeElement->appendChild( |
||
| 97 | $this->document->importNode($child, true) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($wrapperName = $node->getWrapperNodeName()) { |
||
| 102 | $wrapperElement = $this->getDirectChildElementByName( |
||
| 103 | $this->element->childNodes, |
||
| 104 | $wrapperName |
||
| 105 | ); |
||
| 106 | |||
| 107 | if (!$wrapperElement) { |
||
| 108 | $wrapperElement = $this->document->createElement($wrapperName); |
||
| 109 | $this->element->appendChild($wrapperElement); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->setAttr($wrapperElement, $node->getAttr('wrapper')); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($parentName = $node->getParentNodeName()) { |
||
| 116 | $currentElement = ($wrapperElement) ? $wrapperElement : $this->element; |
||
| 117 | |||
| 118 | $parentNode = $this->getDirectChildElementByName( |
||
| 119 | $currentElement->childNodes, |
||
| 120 | $parentName |
||
| 121 | ); |
||
| 122 | |||
| 123 | if (!$parentNode) { |
||
| 124 | $parentElement = $this->document->createElement($parentName); |
||
| 125 | $currentElement->appendChild($parentElement); |
||
| 126 | $parentElement->appendChild($nodeElement); |
||
| 127 | $this->setAttr($parentElement, $node->getAttr('parent')); |
||
| 128 | } else { |
||
| 129 | $parentNode->appendChild($nodeElement); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $this->element->appendChild($nodeElement); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 258 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: