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

Commit

Permalink
Add doc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Nov 25, 2022
1 parent 5b0335d commit b4b71fc
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symplify\PhpConfigPrinter\NodeFactory;

use Nette\Utils\Json;
use PhpParser\Comment\Doc;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
Expand Down Expand Up @@ -60,7 +61,10 @@ public function createFromYamlArray(
*/
private function createClosureStmts(array $yamlData): array
{
$yamlData = array_filter($yamlData);
$yamlData = array_filter($yamlData, function($v, $k) {
return $v || str_starts_with($k, '%comment(');
}, ARRAY_FILTER_USE_BOTH);

return $this->createStmtsFromCaseConverters($yamlData);
}

Expand All @@ -72,10 +76,24 @@ private function createStmtsFromCaseConverters(array $yamlData): array
{
$stmts = [];

$lastComments = [];
$rootLastComments = [];
foreach ($yamlData as $key => $values) {
if (str_starts_with($key, '%comment(')) {
$rootLastComments[] = substr($key, 9, -2);

continue;
}

$stmts = $this->createInitializeStmt($key, $stmts);

foreach ($values as $nestedKey => $nestedValues) {
if (str_starts_with($nestedKey, '%comment(')) {
$lastComments[] = substr($nestedKey, 9, -2);

continue;
}

$nestedNodes = $this->processNestedNodes($key, $nestedKey, $nestedValues);

if ($nestedNodes !== []) {
Expand All @@ -89,11 +107,26 @@ private function createStmtsFromCaseConverters(array $yamlData): array
}

$lastNode = end($stmts);

$node = $this->resolveExpressionWhenAtEnv($expression, $key, $lastNode);

if ($node !== null) {
foreach ($lastComments as $lastComment) {
$node->setDocComment(new Doc('// ' . $lastComment));
}
$lastComment = [];

$stmts[] = $node;
}
}

$firstNode = reset($stmts);
if ($firstNode) {
foreach ($rootLastComments as $lastComment) {
$firstNode->setDocComment(new Doc('// ' . $lastComment));
}
$rootLastComments = [];
}
}

return $stmts;
Expand Down

0 comments on commit b4b71fc

Please sign in to comment.