| Conditions | 21 |
| Paths | 48 |
| Total Lines | 68 |
| Code Lines | 42 |
| 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 |
||
| 42 | function generateXmlFromXPath($xpath) |
||
| 43 | { |
||
| 44 | // split xpath to find predicates, attributes and texts |
||
| 45 | preg_match_all($this->regex, $this->replaceLineBreaks($xpath), $matches); |
||
| 46 | $i = 0; |
||
| 47 | $predicateStack = 0; |
||
| 48 | $tokenStack = 0; |
||
| 49 | $insidePredicate = false; |
||
| 50 | $predicateString = ""; |
||
| 51 | $loopStack = array(); |
||
| 52 | |||
| 53 | foreach ($matches[0] as $key => $value) { |
||
| 54 | $firstChar = substr($value, 0, 1); |
||
| 55 | |||
| 56 | if ($firstChar === '[' || $insidePredicate) { |
||
| 57 | // get corresponding bracket |
||
| 58 | $insidePredicate = true; |
||
| 59 | |||
| 60 | $predicateString .= $value; |
||
| 61 | |||
| 62 | if ($value === "]") { |
||
| 63 | $predicateStack--; |
||
| 64 | |||
| 65 | if ($predicateStack === 0) { |
||
| 66 | // corresponding bracket found |
||
| 67 | $insidePredicate = false; |
||
| 68 | |||
| 69 | if (!empty($predicateString)) { |
||
| 70 | // recursive call with predicate string |
||
| 71 | $this->generateXmlFromXPath(trim($predicateString, "[]")); |
||
| 72 | $predicateString = ""; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } else if ($value === "[") { |
||
| 76 | $predicateStack++; |
||
| 77 | } |
||
| 78 | |||
| 79 | } else if ($firstChar === '@') { |
||
| 80 | // attribute found |
||
| 81 | $this->startAttribute(substr($value, 1, strlen($value))); |
||
| 82 | $loopStack[] = 'attribute'; |
||
| 83 | |||
| 84 | } else if ($firstChar === '"' || $firstChar === "'") { |
||
| 85 | // string found |
||
| 86 | $this->setText(trim($this->undoReplaceLineBreaks($value), "'\"")); |
||
| 87 | } else if ($firstChar === ']') { |
||
| 88 | $this->endAttribute(); |
||
| 89 | array_pop($loopStack); |
||
| 90 | $predicateStack--; |
||
| 91 | } else if ($firstChar !== '=' && $firstChar !== '[' && $firstChar !== '@' && $firstChar !== '/' && $firstChar!== '.') { |
||
| 92 | $this->startToken($value); |
||
| 93 | $loopStack[] = 'token'; |
||
| 94 | if ($predicateStack > 0) { |
||
| 95 | $tokenStack++; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | $i++; |
||
| 99 | } |
||
| 100 | |||
| 101 | // end all open token and attributes in stacked order |
||
| 102 | foreach (array_reverse($loopStack) as $key => $value) { |
||
| 103 | if ($value === "attribute") { |
||
| 104 | $this->endAttribute(); |
||
| 105 | } else if ($value === "token") { |
||
| 106 | $this->endToken(); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | $this->stack = array(); |
||
| 110 | |||
| 178 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.