From eac68f418597ee5a8ab722a8a549ffa56069eb25 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 31 Jul 2021 17:20:50 +0200 Subject: [PATCH] bug fixes and template adjustments --- DataStorage/Database/DataMapperAbstract.php | 6 ++- Message/Console/ConsoleResponse.php | 2 +- Message/Socket/SocketResponse.php | 2 +- Stdlib/Graph/Graph.php | 56 +++++++++++++++++---- Utils/RnG/Text.php | 46 ++--------------- composer.json | 2 +- tests/Utils/RnG/TextTest.php | 2 - 7 files changed, 56 insertions(+), 60 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 9486658dc..81838f28e 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -784,7 +784,9 @@ class DataMapperAbstract implements DataMapperInterface /** @var self $mapper */ $mapper = static::$hasMany[$propertyName]['mapper']; - $internalName = isset($mapper::$columns[static::$hasMany[$propertyName]['self']]) ? $mapper::$columns[static::$hasMany[$propertyName]['self']]['internal'] : 'ERROR'; + $internalName = isset($mapper::$columns[static::$hasMany[$propertyName]['self']]) + ? $mapper::$columns[static::$hasMany[$propertyName]['self']]['internal'] + : 'ERROR'; if (\is_object($values)) { // conditionals @@ -2037,7 +2039,7 @@ class DataMapperAbstract implements DataMapperInterface if (!$refProp->isPublic()) { $refProp->setAccessible(true); // @todo: \is_array($values) is weird, was necessary for the itemmanagement list at some point, but only suddenly????!!!! - $refProp->setValue($obj, !\is_array($objects) && (!isset(static::$hasMany[$member]['conditional']) || \is_array($values)) + $refProp->setValue($obj, !\is_array($objects) && (!isset(static::$hasMany[$member]['conditional']) || (\is_array($values) && \count($values) > 1)) ? [$mapper::getObjectId($objects) => $objects] : $objects ); diff --git a/Message/Console/ConsoleResponse.php b/Message/Console/ConsoleResponse.php index c954e77c5..0cc881ea6 100644 --- a/Message/Console/ConsoleResponse.php +++ b/Message/Console/ConsoleResponse.php @@ -98,7 +98,7 @@ final class ConsoleResponse extends ResponseAbstract implements RenderableInterf /** * Generate response based on header. * - * @param mixed $data Data passt to render function. (0 => bool: $optimize) + * @param mixed ...$data Data passt to render function. (0 => bool: $optimize) * * @return string * diff --git a/Message/Socket/SocketResponse.php b/Message/Socket/SocketResponse.php index 24c8571b9..9329b9bfb 100644 --- a/Message/Socket/SocketResponse.php +++ b/Message/Socket/SocketResponse.php @@ -84,7 +84,7 @@ final class SocketResponse extends ResponseAbstract implements RenderableInterfa /** * Generate response based on header. * - * @param mixed $data Data passt to render function. (0 => bool: $optimize) + * @param mixed ...$data Data passt to render function. (0 => bool: $optimize) * * @return string * diff --git a/Stdlib/Graph/Graph.php b/Stdlib/Graph/Graph.php index 7fc582fc8..efe174229 100644 --- a/Stdlib/Graph/Graph.php +++ b/Stdlib/Graph/Graph.php @@ -490,24 +490,27 @@ class Graph /** * Perform depth first traversal * - * @param int|string|Node $node1 Graph node - * @param int|string|Node $node2 Graph node + * @param Node $node1 Graph node + * @param Node $node2 Graph node + * @param array $visited Is the node already visited + * @param array $path Array of nodes (a path through the graph = connected nodes) + * @param array $paths Array of paths (all identified paths) * - * @return array + * @return void * * @since 1.0.0 */ private function depthFirstTraversal( - int | string | Node $node1, - int | string | Node $node2 = null, + Node $node1, + Node $node2 = null, array &$visited, array &$path, array &$paths - ) : array + ) : void { $visited[$node1->getId()] = true; - if ($node1->isEquals($node2)) { + if ($node2 !== null && $node1->isEqual($node2)) { $paths[] = $path; } @@ -515,7 +518,7 @@ class Graph foreach ($neighbors as $neighbor) { if (!isset($visited[$neighbor->getId()]) || !$visited[$neighbor->getId()]) { $path[] = $neighbor; - $this->depthFirstTraversal($neighbor, $node2, $visited, $path); + $this->depthFirstTraversal($neighbor, $node2, $visited, $path, $paths); \array_pop($path); } } @@ -540,6 +543,10 @@ class Graph $node1 = $this->getNode($node1); } + if ($node1 === null) { + return $nodes; + } + $visited = []; $stack = []; $stack[] = $node1; @@ -647,6 +654,18 @@ class Graph */ public function longestPathBetweenNodes(int | string | Node $node1, int | string | Node $node2) : array { + if (!($node1 instanceof Node)) { + $node1 = $this->getNode($node1); + } + + if (!($node2 instanceof Node)) { + $node2 = $this->getNode($node2); + } + + if ($node1 === null || $node2 === null) { + return []; + } + $paths = $this->getAllPathsBetweenNodes($node1, $node2); if (empty($paths)) { @@ -656,7 +675,7 @@ class Graph foreach ($paths as $key => $path) { $edges[$key] = 0; foreach ($path as $node) { - $edges[$key] += $node->getEdgeByNeighbor()->getWeight(); + $edges[$key] += $node1->getEdgeByNeighbor($node)->getWeight(); } } @@ -677,15 +696,32 @@ class Graph */ public function shortestPathBetweenNodes(int | string | Node $node1, int | string | Node $node2) : array { + if (!($node1 instanceof Node)) { + $node1 = $this->getNode($node1); + } + + if (!($node2 instanceof Node)) { + $node2 = $this->getNode($node2); + } + + if ($node1 === null || $node2 === null) { + return []; + } + $paths = $this->getAllPathsBetweenNodes($node1, $node2); + $edges = []; foreach ($paths as $key => $path) { $edges[$key] = 0; foreach ($path as $node) { - $edges[$key] += $node->getEdgeByNeighbor()->getWeight(); + $edges[$key] += $node1->getEdgeByNeighbor($node)->getWeight(); } } + if ($edges === []) { + return []; + } + \asort($edges); return $paths[\array_key_first($edges)]; diff --git a/Utils/RnG/Text.php b/Utils/RnG/Text.php index 286fbea9e..0e8674155 100644 --- a/Utils/RnG/Text.php +++ b/Utils/RnG/Text.php @@ -55,7 +55,7 @@ class Text * @var bool * @since 1.0.0 */ - private $hasFormatting = false; + public bool $hasFormatting = false; /** * Text has paragraphs. @@ -63,7 +63,7 @@ class Text * @var bool * @since 1.0.0 */ - private $hasParagraphs = false; + public bool $hasParagraphs = false; /** * Amount of sentences of the last generated text. @@ -71,7 +71,7 @@ class Text * @var int * @since 1.0.0 */ - private $sentences = 0; + public int $sentences = 0; /** * Constructor @@ -82,51 +82,11 @@ class Text * @since 1.0.0 */ public function __construct(bool $hasFormatting = false, bool $hasParagraphs = false) - { - $this->setFormatting($hasFormatting); - $this->setParagraphs($hasParagraphs); - } - - /** - * Set if the text should have formatting. - * - * @param bool $hasFormatting text has formatting - * - * @return void - * - * @since 1.0.0 - */ - public function setFormatting(bool $hasFormatting) : void { $this->hasFormatting = $hasFormatting; - } - - /** - * Set if the text should have paragraphs. - * - * @param bool $hasParagraphs Text has paragraphs - * - * @return void - * - * @since 1.0.0 - */ - public function setParagraphs(bool $hasParagraphs) : void - { $this->hasParagraphs = $hasParagraphs; } - /** - * Amount of sentences of the last generated text. - * - * @return int - * - * @since 1.0.0 - */ - public function getSentences() : int - { - return $this->sentences; - } - /** * Get a random string. * diff --git a/composer.json b/composer.json index c243018af..6b3c0007e 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "require-dev": { "phpunit/phpunit": ">=9.4", "friendsofphp/php-cs-fixer": ">=3.0", - "squizlabs/php_codesniffer": ">=3.5", + "squizlabs/php_codesniffer": ">=3.6", "phpmd/phpmd": ">=2.9", "phpstan/phpstan": ">=0.12.58", "phan/phan": ">=3.2.6" diff --git a/tests/Utils/RnG/TextTest.php b/tests/Utils/RnG/TextTest.php index 25e8d8975..d62fa1898 100644 --- a/tests/Utils/RnG/TextTest.php +++ b/tests/Utils/RnG/TextTest.php @@ -38,7 +38,5 @@ class TextTest extends \PHPUnit\Framework\TestCase $text->generateText(300), $text->generateText(300) ); - - self::assertGreaterThan(0, $text->getSentences()); } }