Skip to content

Commit

Permalink
Switched to replace nodes rather than update in place for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jul 2, 2021
1 parent 7156fec commit 4e98e46
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/Config/ConfigFile.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Winter\Storm\Config;

use PhpParser\Error;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -96,36 +97,18 @@ public function set(): ConfigFile
$valueType = gettype($value);
$class = get_class($target->value);

if ($valueType !== $this->getScalaFromNode($class)) {
$target->value = $this->makeAstNode($valueType, $value);
if ($class === FuncCall::class) {
if ($target->value->name->parts[0] !== 'env' || !isset($target->value->args[0])) {
return $this;
}
if (isset($target->value->args[0]) && !isset($target->value->args[1])) {
$target->value->args[1] = new Arg(new String_(''));
}
$target->value->args[1]->value->value = $value;
return $this;
}

switch ($class) {
case String_::class:
$target->value->value = $value;
break;
case FuncCall::class:
if ($target->value->name->parts[0] !== 'env' || !isset($target->value->args[0])) {
break;
}
if (isset($target->value->args[0]) && !isset($target->value->args[1])) {
$target->value->args[1] = clone $target->value->args[0];
}
$target->value->args[1]->value->value = $value;
break;
case ConstFetch::class:
if (isset($target->name->parts[0])) {
$target->name->parts[0] = $value;
}
break;
case LNumber::class:
if (isset($target->value->value)) {
$target->value->value = $value;
}
break;
}

$target->value = $this->makeAstNode($valueType, $value);
return $this;
}

Expand All @@ -147,16 +130,6 @@ protected function makeAstNode(string $type, $value)
}
}

public function getScalaFromNode(string $class): string
{
return [
String_::class => 'string',
FuncCall::class => 'function',
ConstFetch::class => 'const|boolean',
LNumber::class => 'int'
][$class] ?? 'unknown';
}

/**
* @param array $path
* @param $pointer
Expand Down

0 comments on commit 4e98e46

Please sign in to comment.