| Conditions | 20 |
| Paths | 48 |
| Total Lines | 69 |
| 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 |
||
| 34 | function loop($xpath) |
||
|
|
|||
| 35 | { |
||
| 36 | // split xpath to find predicates, attributes and texts |
||
| 37 | preg_match_all($this->regex, $xpath, $matches); |
||
| 38 | $i = 0; |
||
| 39 | $predicateStack = 0; |
||
| 40 | $tokenStack = 0; |
||
| 41 | $insidePredicate = false; |
||
| 42 | $predicateString = ""; |
||
| 43 | $loopStack = array(); |
||
| 44 | |||
| 45 | foreach ($matches[0] as $key => $value) { |
||
| 46 | $firstChar = substr($value, 0, 1); |
||
| 47 | |||
| 48 | if ($firstChar === '[' || $insidePredicate) { |
||
| 49 | // get corresponding bracket |
||
| 50 | $insidePredicate = true; |
||
| 51 | |||
| 52 | $predicateString .= $value; |
||
| 53 | |||
| 54 | if ($value === "]") { |
||
| 55 | $predicateStack--; |
||
| 56 | |||
| 57 | if ($predicateStack === 0) { |
||
| 58 | // corresponding bracket found |
||
| 59 | $insidePredicate = false; |
||
| 60 | |||
| 61 | if (!empty($predicateString)) { |
||
| 62 | // recursive call with predicate string |
||
| 63 | $this->loop(trim($predicateString, "[]")); |
||
| 64 | $predicateString = ""; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } else if ($value === "[") { |
||
| 68 | $predicateStack++; |
||
| 69 | } |
||
| 70 | |||
| 71 | } else if ($firstChar === '@') { |
||
| 72 | // attribute found |
||
| 73 | $this->startAttribute(substr($value, 1, strlen($value))); |
||
| 74 | $loopStack[] = 'attribute'; |
||
| 75 | |||
| 76 | } else if ($firstChar === '"' || $firstChar === "'") { |
||
| 77 | // string found |
||
| 78 | $this->setText(trim($value, "'\"")); |
||
| 79 | } else if ($firstChar === ']') { |
||
| 80 | $this->endAttribute(); |
||
| 81 | array_pop($loopStack); |
||
| 82 | $predicateStack--; |
||
| 83 | |||
| 84 | } else if ($firstChar !== '=' && $firstChar !== '[' && $firstChar !== '@' && $firstChar !== '/') { |
||
| 85 | $this->startToken($value); |
||
| 86 | $loopStack[] = 'token'; |
||
| 87 | if ($predicateStack > 0) { |
||
| 88 | $tokenStack++; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | $i++; |
||
| 92 | } |
||
| 93 | |||
| 94 | // end all open token and attributes in stacked order |
||
| 95 | foreach (array_reverse($loopStack) as $key => $value) { |
||
| 96 | if ($value === "attribute") { |
||
| 97 | $this->endAttribute(); |
||
| 98 | } else if ($value === "token") { |
||
| 99 | $this->endToken(); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | $this->stack = array(); |
||
| 103 | |||
| 171 |
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.