Skip to content

Commit

Permalink
Extract the length out of the loop and reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Oct 17, 2016
1 parent 39e3309 commit 12e09bf
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Selector/Xpath/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,47 @@ private function splitUnionParts($xpath)
$inDoubleQuotedString = false;
$openedBrackets = 0;
$lastUnion = 0;
for ($i = 0; $i < strlen($xpath); $i++) {
$xpathLength = strlen($xpath);

for ($i = 0; $i < $xpathLength; $i++) {
$char = $xpath[$i];

if ($char === "'" && !$inDoubleQuotedString) {
$inSingleQuotedString = !$inSingleQuotedString;
} elseif ($char === '"' && !$inSingleQuotedString) {

continue;
}

if ($char === '"' && !$inSingleQuotedString) {
$inDoubleQuotedString = !$inDoubleQuotedString;
} elseif (!$inSingleQuotedString && !$inDoubleQuotedString) {
if ($char === '[') {
$openedBrackets++;
} elseif ($char === ']') {
$openedBrackets--;
} elseif ($char === '|' && $openedBrackets === 0) {
$unionParts[] = substr($xpath, $lastUnion, $i - $lastUnion);
$lastUnion = $i + 1;
}

continue;
}

if ($inSingleQuotedString || $inDoubleQuotedString) {
continue;
}

if ($char === '[') {
$openedBrackets++;

continue;
}

if ($char === ']') {
$openedBrackets--;

continue;
}

if ($char === '|' && $openedBrackets === 0) {
$unionParts[] = substr($xpath, $lastUnion, $i - $lastUnion);
$lastUnion = $i + 1;
}
}

$unionParts[] = substr($xpath, $lastUnion);

return $unionParts;
}

Expand Down

0 comments on commit 12e09bf

Please sign in to comment.