Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

Commit

Permalink
restructure structureCode method
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Feb 22, 2017
1 parent f08dc53 commit 59a1160
Showing 1 changed file with 68 additions and 44 deletions.
112 changes: 68 additions & 44 deletions src/Generator/NodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,6 @@ public function generateByJson($query): string
return $this->structureCode($code);
}

/**
* @param string $code
* @return string
*/
private function structureCode(string $code): string
{
$codeWithLinebreaks = str_replace('->add', "\n->add", substr($code, 0, -1));

$lines = explode("\n", $codeWithLinebreaks);

$position = 0;

$structuredLines = [];

foreach ($lines as $i => $line) {
$lastStructuredLine = $structuredLines[count($structuredLines) - 1] ?? '';
if (0 === strpos($line, '->add') &&
false === strpos($lastStructuredLine, ' )') &&
false === strpos($lastStructuredLine, 'ScalarNode')) {
$position++;
}
$lineLength = strlen($line);
$braceCount = 0;
while (')' === $line[--$lineLength]) {
$braceCount++;
}
$prefix = str_pad('', $position * 4);
if ($braceCount > 2) {
$structuredLines[] = $prefix . substr($line, 0, - ($braceCount - 2));
} else {
$structuredLines[] = $prefix . $line;
}

while ($braceCount-- > 2) {
$position--;
$structuredLines[] = str_pad('', $position * 4) . ')';
}
}

$structuredLines[count($structuredLines) - 1] .= ';';

return implode("\n", $structuredLines);
}

/**
* @return Expr
*/
Expand Down Expand Up @@ -192,4 +148,72 @@ private function appendChildrenToObjectNode(\stdClass $data)

return $expr;
}

/**
* @param string $code
* @return string
*/
private function structureCode(string $code): string
{
$lines = $this->getLinesByCode($code);

$position = 0;

$structuredLines = [];

foreach ($lines as $i => $line) {
$lastStructuredLine = $structuredLines[count($structuredLines) - 1] ?? '';
$this->structuredLine($line, $lastStructuredLine, $position, $structuredLines);
}

$structuredLines[count($structuredLines) - 1] .= ';';

return implode("\n", $structuredLines);
}

/**
* @param string $code
* @return array
*/
private function getLinesByCode(string $code): array
{
$codeWithLinebreaks = str_replace('->add', "\n->add", substr($code, 0, -1));

return explode("\n", $codeWithLinebreaks);
}

/**
* @param string $line
* @param string $lastStructuredLine
* @param int $position
* @param array $structuredLines
*/
private function structuredLine(string $line, string $lastStructuredLine, int &$position, array &$structuredLines)
{
if (0 === strpos($line, '->add') &&
false === strpos($lastStructuredLine, ' )') &&
false === strpos($lastStructuredLine, 'ScalarNode')) {
$position++;
}

$lineLength = strlen($line);
$braceCount = 0;

while (')' === $line[--$lineLength]) {
$braceCount++;
}

$prefix = str_pad('', $position * 4);

if ($braceCount > 2) {
$structuredLines[] = $prefix . substr($line, 0, - ($braceCount - 2));
} else {
$structuredLines[] = $prefix . $line;
}

while ($braceCount-- > 2) {
$position--;
$structuredLines[] = str_pad('', $position * 4) . ')';
}
}
}

0 comments on commit 59a1160

Please sign in to comment.