This commit is contained in:
Thomas 2025-07-08 12:55:17 +02:00
parent 4404cb5928
commit 6854089a74
29 changed files with 550 additions and 379 deletions

View File

@ -1 +1 @@
{"version":1,"defects":[],"times":{"SimpleTest::testGraph":0.027}} {"version":1,"defects":{"SimpleTest::testGraph":8},"times":[]}

File diff suppressed because it is too large Load Diff

View File

@ -24,23 +24,41 @@ class Calc
foreach ($yield as $y) { foreach ($yield as $y) {
$this->parseFormulas($y); $this->parseFormulas($y);
$y->setResult($this->math->evaluate($y->getUnparsed())); $this->parseParams($y);
var_dump($y); var_dump($y->getParsed());
$y->setResult($this->math->evaluate($y->getParsed()));
} }
} }
private function parseFormulas(Part $p): void private function parseFormulas(Part $p): void
{ {
preg_match_all('/\$F\w*\$F/', $formel->getUnparsed(), $founds); preg_match_all('/\$F\w*\$F/', $p->getUnparsed(), $founds);
if (!empty($founds[0])) { if (!empty($founds[0])) {
foreach ($founds[0] as $key => $found) { foreach ($founds[0] as $key => $found) {
$foundValue = str_replace('$F', '', $found); $foundValue = str_replace('$F', '', $found);
$foundPart = array_find($p->getChildren(), ($c) => $c->getName() == $foundValue)); $foundPart = array_find($p->getChildren(), fn($c) => $c->getName() == $foundValue);
if (count($foundPart) > 0) { if ($foundPart) {
$formel->setParsed(str_replace($found, $foundPart[0]->getResult(), $formel->getParsed())) $p->setParsed(str_replace($found, $foundPart->getResult(), $p->getParsed()));
} else { } else {
$formel->setParsed(str_replace($found, 0, $formel->getParsed())) $p->setParsed(str_replace($found, 0, $p->getParsed()));
}
}
}
}
private function parseParams(Part $p): void
{
preg_match_all('/\$P\w*\$P/', $p->getUnparsed(), $founds);
if (!empty($founds[0])) {
foreach ($founds[0] as $key => $found) {
$foundValue = str_replace('$F', '', $found);
$foundPart = array_find($p->getChildren(), fn($c) => $c->getName() == $foundValue);
if ($foundPart) {
$p->setParsed(str_replace($found, $foundPart->getResult(), $p->getParsed()));
} else {
$p->setParsed(str_replace($found, 0, $p->getParsed()));
} }
} }
} }

View File

@ -36,4 +36,9 @@ class Graph
return true; return true;
} }
public function getSum(): int
{
return array_reduce((array)$this->calcFormel, fn($sum, $item) => $sum + $item->getResult(), 0);
}
} }

View File

@ -8,8 +8,6 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\NodeTraverser; use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract; use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory; use PhpParser\ParserFactory;
use PSC\Library\Calc\Model\Formel;
use PSC\Library\Calc\Model\FormelCollection;
use PSC\Library\Calc\Model\Part; use PSC\Library\Calc\Model\Part;
use PSC\Library\Calc\Model\PartCollection; use PSC\Library\Calc\Model\PartCollection;
use PSC\Library\Calc\Model\PartType; use PSC\Library\Calc\Model\PartType;
@ -52,7 +50,7 @@ class Parser
} }
}); });
$parser = new ParserFactory()->createForNewestSupportedVersion(); $parser = (new ParserFactory())->createForNewestSupportedVersion();
try { try {
$ast = $parser->parse('<?php' . PHP_EOL . $formulas); $ast = $parser->parse('<?php' . PHP_EOL . $formulas);
$traverser->traverse($ast); $traverser->traverse($ast);
@ -74,8 +72,7 @@ class Parser
{ {
// $formel = $this->parseCalcVariables($formel, $breakValid); // $formel = $this->parseCalcVariables($formel, $breakValid);
$this->parseFormulas($formel); $this->parseFormulas($formel);
$this->parseParams($formel);
// $formel = $this->parseParameters($formel, $breakValid);
// $formel = $this->parseVariables($formel, $breakValid); // $formel = $this->parseVariables($formel, $breakValid);
} }
@ -83,6 +80,30 @@ class Parser
{ {
preg_match_all('/\$F\w*\$F/', $formel->getUnparsed(), $founds); preg_match_all('/\$F\w*\$F/', $formel->getUnparsed(), $founds);
if (!empty($founds[0])) {
foreach ($founds[0] as $key => $found) {
$foundValue = str_replace('$F', '', $found);
if ($this->internalParts->getPartByName($foundValue)) {
$part = $this->internalParts->getPartByName($foundValue);
$this->parse($part);
$formel->addChild($part);
} else {
$formel->addChild(new Part(
type: PartType::Formel,
name: $foundValue,
unParsed: $found,
parsed: 0,
result: 0,
));
}
}
}
}
private function parseParams(Part $formel): void
{
preg_match_all('/\$P\w*\$P/', $formel->getUnparsed(), $founds);
if (!empty($founds[0])) { if (!empty($founds[0])) {
foreach ($founds[0] as $key => $found) { foreach ($founds[0] as $key => $found) {
$foundValue = str_replace('$F', '', $found); $foundValue = str_replace('$F', '', $found);

View File

@ -12,7 +12,11 @@ class Part extends Node
private string $unParsed, private string $unParsed,
private string $parsed = '', private string $parsed = '',
private int $result = 0, private int $result = 0,
) {} ) {
if($parsed == '') {
$this->parsed = $unParsed;
}
}
public function getName(): string public function getName(): string
{ {
@ -24,8 +28,23 @@ class Part extends Node
return $this->unParsed; return $this->unParsed;
} }
public function getParsed(): string
{
return $this->parsed;
}
public function setParsed(string $parsed): void
{
$this->parsed = $parsed;
}
public function setResult(int $result): void public function setResult(int $result): void
{ {
$this->result = $result; $this->result = $result;
} }
public function getResult(): string
{
return $this->result;
}
} }

View File

@ -5,6 +5,7 @@ use PSC\Library\Calc\Graph\Graph;
use PSC\Library\Calc\Model\Part; use PSC\Library\Calc\Model\Part;
use PSC\Library\Calc\Model\PartType; use PSC\Library\Calc\Model\PartType;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertTrue; use function PHPUnit\Framework\assertTrue;
class SimpleTest extends TestCase class SimpleTest extends TestCase
@ -24,9 +25,10 @@ class SimpleTest extends TestCase
$this->graph->addCalcFormel(new Part( $this->graph->addCalcFormel(new Part(
type: PartType::CalcFormel, type: PartType::CalcFormel,
name: 'test1', name: 'test1',
unParsed: '$Ftest2$F+$Ftest3$F+10', unParsed: '$Ftest1$F+$Ftest2$F+10',
)); ));
assertTrue($this->graph->build()); assertTrue($this->graph->build());
assertSame(7510, $this->graph->getSum());
} }
} }

View File

@ -1,2 +1,2 @@
$test1 = '100*50'; $test1 = '100*50';
$test2 = '50*50'; $test2 = '$Ptest3$P*50';