diff --git a/.phpunit.cache/code-coverage/0c97d5489f3591195ecb2796b62d68c8ec7934a2fa3ec9ca40acdc0e5e8cdaea b/.phpunit.cache/code-coverage/0c97d5489f3591195ecb2796b62d68c8ec7934a2fa3ec9ca40acdc0e5e8cdaea new file mode 100644 index 0000000..abed17b Binary files /dev/null and b/.phpunit.cache/code-coverage/0c97d5489f3591195ecb2796b62d68c8ec7934a2fa3ec9ca40acdc0e5e8cdaea differ diff --git a/.phpunit.cache/code-coverage/19aef04741064502eecf19fb94f58846bae6b6a72a5b09c8f483d918823963eb b/.phpunit.cache/code-coverage/19aef04741064502eecf19fb94f58846bae6b6a72a5b09c8f483d918823963eb new file mode 100644 index 0000000..2633d62 Binary files /dev/null and b/.phpunit.cache/code-coverage/19aef04741064502eecf19fb94f58846bae6b6a72a5b09c8f483d918823963eb differ diff --git a/.phpunit.cache/code-coverage/282097d00aa88b0e114c2d721590e096f24c1bd3697183dc6f0d2fb7d660c50f b/.phpunit.cache/code-coverage/282097d00aa88b0e114c2d721590e096f24c1bd3697183dc6f0d2fb7d660c50f new file mode 100644 index 0000000..6c9ce5a Binary files /dev/null and b/.phpunit.cache/code-coverage/282097d00aa88b0e114c2d721590e096f24c1bd3697183dc6f0d2fb7d660c50f differ diff --git a/.phpunit.cache/code-coverage/2d0dcf35185eed61d10eb60bc284adddd4ef9a147aff2a7bdecbb7b3866e291d b/.phpunit.cache/code-coverage/2d0dcf35185eed61d10eb60bc284adddd4ef9a147aff2a7bdecbb7b3866e291d new file mode 100644 index 0000000..b119171 Binary files /dev/null and b/.phpunit.cache/code-coverage/2d0dcf35185eed61d10eb60bc284adddd4ef9a147aff2a7bdecbb7b3866e291d differ diff --git a/.phpunit.cache/code-coverage/3b956ccbeb04f9bcfe92c1456dfd6008e9d5cd143d7669104915926248235e1b b/.phpunit.cache/code-coverage/3b956ccbeb04f9bcfe92c1456dfd6008e9d5cd143d7669104915926248235e1b new file mode 100644 index 0000000..0f7e353 Binary files /dev/null and b/.phpunit.cache/code-coverage/3b956ccbeb04f9bcfe92c1456dfd6008e9d5cd143d7669104915926248235e1b differ diff --git a/.phpunit.cache/code-coverage/3e1fab3772093cb603b8aebe0427ec0b1f47eced387d0906afb5f5ff7d918d06 b/.phpunit.cache/code-coverage/3e1fab3772093cb603b8aebe0427ec0b1f47eced387d0906afb5f5ff7d918d06 new file mode 100644 index 0000000..2633d62 Binary files /dev/null and b/.phpunit.cache/code-coverage/3e1fab3772093cb603b8aebe0427ec0b1f47eced387d0906afb5f5ff7d918d06 differ diff --git a/.phpunit.cache/code-coverage/4d04fa19239d51fdaaf5cd1396400d6be6706c486279250c0744ca70adf7f003 b/.phpunit.cache/code-coverage/4d04fa19239d51fdaaf5cd1396400d6be6706c486279250c0744ca70adf7f003 new file mode 100644 index 0000000..1dafcd4 Binary files /dev/null and b/.phpunit.cache/code-coverage/4d04fa19239d51fdaaf5cd1396400d6be6706c486279250c0744ca70adf7f003 differ diff --git a/.phpunit.cache/code-coverage/5c2f8f486d7632077a6f8bef4089991f1a3ecb16990bdc728a28fd72130e1587 b/.phpunit.cache/code-coverage/5c2f8f486d7632077a6f8bef4089991f1a3ecb16990bdc728a28fd72130e1587 new file mode 100644 index 0000000..b119171 Binary files /dev/null and b/.phpunit.cache/code-coverage/5c2f8f486d7632077a6f8bef4089991f1a3ecb16990bdc728a28fd72130e1587 differ diff --git a/.phpunit.cache/code-coverage/606aa647be81a0b8cb6bf2847c864a4074a09a53dbd03558c8ac8418a40d36b8 b/.phpunit.cache/code-coverage/606aa647be81a0b8cb6bf2847c864a4074a09a53dbd03558c8ac8418a40d36b8 new file mode 100644 index 0000000..5b0044b Binary files /dev/null and b/.phpunit.cache/code-coverage/606aa647be81a0b8cb6bf2847c864a4074a09a53dbd03558c8ac8418a40d36b8 differ diff --git a/.phpunit.cache/code-coverage/7a228ff414c133cb02a1f55d19d64575ce1f76fbdb0f8ea1197bdca82a32e43e b/.phpunit.cache/code-coverage/7a228ff414c133cb02a1f55d19d64575ce1f76fbdb0f8ea1197bdca82a32e43e new file mode 100644 index 0000000..abed17b Binary files /dev/null and b/.phpunit.cache/code-coverage/7a228ff414c133cb02a1f55d19d64575ce1f76fbdb0f8ea1197bdca82a32e43e differ diff --git a/.phpunit.cache/code-coverage/8bc06688e2c1205d6dd6dd9dbf343ee24fe8c80aabbbd84983391ca8d03cfaa7 b/.phpunit.cache/code-coverage/8bc06688e2c1205d6dd6dd9dbf343ee24fe8c80aabbbd84983391ca8d03cfaa7 new file mode 100644 index 0000000..0f7e353 Binary files /dev/null and b/.phpunit.cache/code-coverage/8bc06688e2c1205d6dd6dd9dbf343ee24fe8c80aabbbd84983391ca8d03cfaa7 differ diff --git a/.phpunit.cache/code-coverage/93efb12ab3c1401460a055bc1002b566deb55d6629e0718c0ecf1bb7168d1939 b/.phpunit.cache/code-coverage/93efb12ab3c1401460a055bc1002b566deb55d6629e0718c0ecf1bb7168d1939 new file mode 100644 index 0000000..d7cc160 Binary files /dev/null and b/.phpunit.cache/code-coverage/93efb12ab3c1401460a055bc1002b566deb55d6629e0718c0ecf1bb7168d1939 differ diff --git a/.phpunit.cache/code-coverage/9858de8e1eac63deec57b00413a10aa43e3490042270a541c5d64d2186b3c07a b/.phpunit.cache/code-coverage/9858de8e1eac63deec57b00413a10aa43e3490042270a541c5d64d2186b3c07a new file mode 100644 index 0000000..a2ad54e Binary files /dev/null and b/.phpunit.cache/code-coverage/9858de8e1eac63deec57b00413a10aa43e3490042270a541c5d64d2186b3c07a differ diff --git a/.phpunit.cache/code-coverage/9d30f65d49e5cc6783e277abda81d993af06f200d4dd68e332ae8ea5faa3d8ac b/.phpunit.cache/code-coverage/9d30f65d49e5cc6783e277abda81d993af06f200d4dd68e332ae8ea5faa3d8ac new file mode 100644 index 0000000..5b0044b Binary files /dev/null and b/.phpunit.cache/code-coverage/9d30f65d49e5cc6783e277abda81d993af06f200d4dd68e332ae8ea5faa3d8ac differ diff --git a/.phpunit.cache/code-coverage/ac2d69f71673b8f25c9194c5b5f5845ec59b2d20acd811bffdd3d070c5e794de b/.phpunit.cache/code-coverage/ac2d69f71673b8f25c9194c5b5f5845ec59b2d20acd811bffdd3d070c5e794de new file mode 100644 index 0000000..0f7e353 Binary files /dev/null and b/.phpunit.cache/code-coverage/ac2d69f71673b8f25c9194c5b5f5845ec59b2d20acd811bffdd3d070c5e794de differ diff --git a/.phpunit.cache/code-coverage/c3d4f88dc384475ed76c1b4706ee97a18f9d0865c8f45340abf7262bbb5494ab b/.phpunit.cache/code-coverage/c3d4f88dc384475ed76c1b4706ee97a18f9d0865c8f45340abf7262bbb5494ab new file mode 100644 index 0000000..5b0044b Binary files /dev/null and b/.phpunit.cache/code-coverage/c3d4f88dc384475ed76c1b4706ee97a18f9d0865c8f45340abf7262bbb5494ab differ diff --git a/.phpunit.cache/code-coverage/c7b1b6a4fe2ffa285f3d4f7c8be68967a8d9edc3df0d29cf21b89337ba852443 b/.phpunit.cache/code-coverage/c7b1b6a4fe2ffa285f3d4f7c8be68967a8d9edc3df0d29cf21b89337ba852443 new file mode 100644 index 0000000..fbbae32 Binary files /dev/null and b/.phpunit.cache/code-coverage/c7b1b6a4fe2ffa285f3d4f7c8be68967a8d9edc3df0d29cf21b89337ba852443 differ diff --git a/.phpunit.cache/code-coverage/c8afcafac71e02822b42a23b129a7a0cc9bc5e9628cd267af3ee4b64908151eb b/.phpunit.cache/code-coverage/c8afcafac71e02822b42a23b129a7a0cc9bc5e9628cd267af3ee4b64908151eb new file mode 100644 index 0000000..1dafcd4 Binary files /dev/null and b/.phpunit.cache/code-coverage/c8afcafac71e02822b42a23b129a7a0cc9bc5e9628cd267af3ee4b64908151eb differ diff --git a/.phpunit.cache/code-coverage/e2a7fb2b47ef3a45700e841a563eb1a73d2a8d090c0f8abec624177e05baf608 b/.phpunit.cache/code-coverage/e2a7fb2b47ef3a45700e841a563eb1a73d2a8d090c0f8abec624177e05baf608 new file mode 100644 index 0000000..a0e4fa9 Binary files /dev/null and b/.phpunit.cache/code-coverage/e2a7fb2b47ef3a45700e841a563eb1a73d2a8d090c0f8abec624177e05baf608 differ diff --git a/.phpunit.cache/code-coverage/eeb65ee9ab7078000ceb2be8fb0f47439a1c9e19584d7801f5d8e3ed22edcc4c b/.phpunit.cache/code-coverage/eeb65ee9ab7078000ceb2be8fb0f47439a1c9e19584d7801f5d8e3ed22edcc4c new file mode 100644 index 0000000..1dafcd4 Binary files /dev/null and b/.phpunit.cache/code-coverage/eeb65ee9ab7078000ceb2be8fb0f47439a1c9e19584d7801f5d8e3ed22edcc4c differ diff --git a/.phpunit.cache/code-coverage/ff9b6d8eab48d2bd32aca1439c996b0640d909d0773c79fa4e8b05cd9f4e46c9 b/.phpunit.cache/code-coverage/ff9b6d8eab48d2bd32aca1439c996b0640d909d0773c79fa4e8b05cd9f4e46c9 new file mode 100644 index 0000000..e16027f Binary files /dev/null and b/.phpunit.cache/code-coverage/ff9b6d8eab48d2bd32aca1439c996b0640d909d0773c79fa4e8b05cd9f4e46c9 differ diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results index 9579b5b..d2ec013 100644 --- a/.phpunit.cache/test-results +++ b/.phpunit.cache/test-results @@ -1 +1 @@ -{"version":1,"defects":[],"times":{"SimpleTest::testGraph":0.027}} \ No newline at end of file +{"version":1,"defects":{"SimpleTest::testGraph":8},"times":[]} \ No newline at end of file diff --git a/cobertura.xml b/cobertura.xml index 0fc9a6a..fe4bdfa 100644 --- a/cobertura.xml +++ b/cobertura.xml @@ -1,6 +1,6 @@ - + /home/thomas/projekte/calc/src @@ -3292,44 +3292,88 @@ - + - + - + - + - + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - - + + @@ -3337,20 +3381,20 @@ - + - - - + + + - + - + @@ -3365,7 +3409,7 @@ - + @@ -3384,43 +3428,43 @@ - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + - - + + - - - - - - - - + + + + + + + + - - - - + + + + @@ -3442,7 +3486,7 @@ - + @@ -3453,14 +3497,14 @@ - + - - - - + + + + @@ -3468,46 +3512,46 @@ - - + + - - + + - - + + - - + + - - + + - + - - - - - - + + + + + + - + - + - + - - - + + + @@ -3519,9 +3563,9 @@ - + - + @@ -3536,59 +3580,59 @@ - - + + - + - + - + - - - - - - + + + + + + - + - - - - - - - - - - + + + + + + + + + + - - + + - - - + + + - + - + @@ -3603,7 +3647,7 @@ - + @@ -3612,41 +3656,41 @@ - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + - - + + - - - - - - - - + + + + + + + + - - - - + + + + @@ -3668,7 +3712,7 @@ - + @@ -3679,14 +3723,14 @@ - + - - - - + + + + @@ -3694,42 +3738,42 @@ - - + + - - + + - - + + - - + + - - - - - - - - + + + + + + + + - + - + - + - - - + + + @@ -3741,9 +3785,9 @@ - + - + @@ -3758,52 +3802,52 @@ - - + + - + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - + - + - + - - + + - + - - - - + + + + @@ -3814,162 +3858,204 @@ - - - - - - + + + + + + - + - + - + - - - + + + - + - + - + - - - - - + + + + + + + + + + - - - - - - - - - + + + + + + + + + + - + - + - + - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - + + + + - - - + - + + - + - - - - - - + + + + + + + + + - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - + + + + + + - - - - + + + + - - - - - - - - - + + + + + + + + + + + - + + + + + + + + + + + + + @@ -3999,9 +4085,9 @@ - + - + @@ -4014,11 +4100,11 @@ - + - - - + + + @@ -4037,9 +4123,9 @@ - + - + @@ -4052,9 +4138,9 @@ - + - + @@ -4145,9 +4231,9 @@ - + - + @@ -4155,9 +4241,9 @@ - - - + + + @@ -4166,14 +4252,14 @@ - + - + @@ -4214,31 +4300,31 @@ - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -4246,59 +4332,79 @@ - + - + - + - + + - + - + - + - + - + - + + + + + + + + + + + + + + + + - - - - + + + + + + + + - + - + - + - - - - + + + + - - - - + + + + diff --git a/src/Graph/Calc.php b/src/Graph/Calc.php index 2328a4e..11a7ccf 100644 --- a/src/Graph/Calc.php +++ b/src/Graph/Calc.php @@ -24,23 +24,41 @@ class Calc foreach ($yield as $y) { $this->parseFormulas($y); - $y->setResult($this->math->evaluate($y->getUnparsed())); - var_dump($y); + $this->parseParams($y); + var_dump($y->getParsed()); + $y->setResult($this->math->evaluate($y->getParsed())); } } 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])) { foreach ($founds[0] as $key => $found) { $foundValue = str_replace('$F', '', $found); - $foundPart = array_find($p->getChildren(), ($c) => $c->getName() == $foundValue)); - if (count($foundPart) > 0) { - $formel->setParsed(str_replace($found, $foundPart[0]->getResult(), $formel->getParsed())) + $foundPart = array_find($p->getChildren(), fn($c) => $c->getName() == $foundValue); + if ($foundPart) { + $p->setParsed(str_replace($found, $foundPart->getResult(), $p->getParsed())); } 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())); } } } diff --git a/src/Graph/Graph.php b/src/Graph/Graph.php index d9dcaf0..212e41a 100644 --- a/src/Graph/Graph.php +++ b/src/Graph/Graph.php @@ -36,4 +36,9 @@ class Graph return true; } + + public function getSum(): int + { + return array_reduce((array)$this->calcFormel, fn($sum, $item) => $sum + $item->getResult(), 0); + } } diff --git a/src/Graph/Parser.php b/src/Graph/Parser.php index 0b0debd..364ee7b 100644 --- a/src/Graph/Parser.php +++ b/src/Graph/Parser.php @@ -8,8 +8,6 @@ use PhpParser\Node\Expr\Assign; use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; 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\PartCollection; use PSC\Library\Calc\Model\PartType; @@ -52,7 +50,7 @@ class Parser } }); - $parser = new ParserFactory()->createForNewestSupportedVersion(); + $parser = (new ParserFactory())->createForNewestSupportedVersion(); try { $ast = $parser->parse('traverse($ast); @@ -74,8 +72,7 @@ class Parser { // $formel = $this->parseCalcVariables($formel, $breakValid); $this->parseFormulas($formel); - - // $formel = $this->parseParameters($formel, $breakValid); + $this->parseParams($formel); // $formel = $this->parseVariables($formel, $breakValid); } @@ -83,6 +80,30 @@ class Parser { 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])) { foreach ($founds[0] as $key => $found) { $foundValue = str_replace('$F', '', $found); diff --git a/src/Model/Part.php b/src/Model/Part.php index 9907648..b9be54b 100644 --- a/src/Model/Part.php +++ b/src/Model/Part.php @@ -12,7 +12,11 @@ class Part extends Node private string $unParsed, private string $parsed = '', private int $result = 0, - ) {} + ) { + if($parsed == '') { + $this->parsed = $unParsed; + } + } public function getName(): string { @@ -24,8 +28,23 @@ class Part extends Node 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 { $this->result = $result; } + + public function getResult(): string + { + return $this->result; + } } diff --git a/tests/Graph/SimpleTest.php b/tests/Graph/SimpleTest.php index 92d5a18..aef15ba 100644 --- a/tests/Graph/SimpleTest.php +++ b/tests/Graph/SimpleTest.php @@ -5,6 +5,7 @@ use PSC\Library\Calc\Graph\Graph; use PSC\Library\Calc\Model\Part; use PSC\Library\Calc\Model\PartType; +use function PHPUnit\Framework\assertSame; use function PHPUnit\Framework\assertTrue; class SimpleTest extends TestCase @@ -24,9 +25,10 @@ class SimpleTest extends TestCase $this->graph->addCalcFormel(new Part( type: PartType::CalcFormel, name: 'test1', - unParsed: '$Ftest2$F+$Ftest3$F+10', + unParsed: '$Ftest1$F+$Ftest2$F+10', )); assertTrue($this->graph->build()); + assertSame(7510, $this->graph->getSum()); } } diff --git a/tests/Graph/formel.txt b/tests/Graph/formel.txt index a5815b8..bcb04f6 100644 --- a/tests/Graph/formel.txt +++ b/tests/Graph/formel.txt @@ -1,2 +1,2 @@ $test1 = '100*50'; -$test2 = '50*50'; +$test2 = '$Ptest3$P*50';