From 624d6b517ed5460ffe7b2ccc49935303de31dd28 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 15 Oct 2023 12:45:18 +0000 Subject: [PATCH] code analysis fixes --- Algorithm/Graph/MarkovChain.php | 29 +++++++ Business/BusinessHelper.php | 2 +- DataStorage/Database/GrammarAbstract.php | 2 +- Localization/RegionEnum.php | 2 +- Math/Geometry/ConvexHull/GrahamScan.php | 29 ++++--- Stdlib/Base/SmartDateTime.php | 42 ++++++++++ Stdlib/Tree/BinarySearchTree.php | 100 +++++++++++++++++++---- Stdlib/Tree/Node.php | 57 ++++++++++++- 8 files changed, 234 insertions(+), 29 deletions(-) diff --git a/Algorithm/Graph/MarkovChain.php b/Algorithm/Graph/MarkovChain.php index 19a3ef10a..dde713a45 100644 --- a/Algorithm/Graph/MarkovChain.php +++ b/Algorithm/Graph/MarkovChain.php @@ -107,6 +107,16 @@ final class MarkovChain $this->data = $values; } + /** + * Generate a markov chain based on the training data. + * + * @param int $length Length of the markov chain + * @param array $start Start values of the markov chain + * + * @return array + * + * @since 1.0.0 + */ public function generate(int $length, array $start = null) : array { $orderKeys = \array_keys($this->data); @@ -148,6 +158,15 @@ final class MarkovChain return $output; } + /** + * Calculate the probability for a certain markov chain. + * + * @param array $path Markov chain + * + * @return float + * + * @since 1.0.0 + */ public function pathProbability(array $path) : float { $length = \count($path); @@ -168,6 +187,16 @@ final class MarkovChain return $prob; } + /** + * Calculate the probability for a certain state change in a markov chain + * + * @param array $state Current state of the markov chain + * @param mixed $next Next markov state + * + * @return float + * + * @since 1.0.0 + */ public function stepProbability(array $state, mixed $next) : float { if (\count($state) !== $this->order) { diff --git a/Business/BusinessHelper.php b/Business/BusinessHelper.php index b15bffff5..6b3abff03 100644 --- a/Business/BusinessHelper.php +++ b/Business/BusinessHelper.php @@ -24,4 +24,4 @@ namespace phpOMS\Business; */ final class BusinessHelper { -} \ No newline at end of file +} diff --git a/DataStorage/Database/GrammarAbstract.php b/DataStorage/Database/GrammarAbstract.php index 3bf1d8c43..4ca66d527 100755 --- a/DataStorage/Database/GrammarAbstract.php +++ b/DataStorage/Database/GrammarAbstract.php @@ -204,7 +204,7 @@ abstract class GrammarAbstract $expression .= $element->toSql() . (\is_string($key) ? ' as ' . $key : '') . ', '; } elseif (\is_int($element)) { $expression .= $element . ', '; - }else { + } else { throw new \InvalidArgumentException(); } } diff --git a/Localization/RegionEnum.php b/Localization/RegionEnum.php index 86d4eef34..24b6e4001 100644 --- a/Localization/RegionEnum.php +++ b/Localization/RegionEnum.php @@ -89,4 +89,4 @@ class RegionEnum extends Enum public const ANTARCTICA = 'Antarctica'; public const CONTINENTS = 'Continents'; -} \ No newline at end of file +} diff --git a/Math/Geometry/ConvexHull/GrahamScan.php b/Math/Geometry/ConvexHull/GrahamScan.php index e1a98ec27..2e8aa6afc 100644 --- a/Math/Geometry/ConvexHull/GrahamScan.php +++ b/Math/Geometry/ConvexHull/GrahamScan.php @@ -60,18 +60,16 @@ final class GrahamScan } } - $temp = $points[1]; - $points[1] = $points[$min]; + $temp = $points[1]; + $points[1] = $points[$min]; $points[$min] = $temp; $c = $points[1]; $subpoints = \array_slice($points, 2, $count); - \usort($subpoints, function (array $a, array $b) use ($c) : bool - { - return atan2($a['y'] - $c['y'], $a['x'] - $c['x']) < atan2( $b['y'] - $c['y'], $b['x'] - $c['x']); - } - ); + \usort($subpoints, function (array $a, array $b) use ($c) : bool { + return atan2($a['y'] - $c['y'], $a['x'] - $c['x']) < atan2( $b['y'] - $c['y'], $b['x'] - $c['x']); + }); $points = \array_merge([$points[0], $points[1]], $subpoints); $points[0] = $points[$count]; @@ -88,9 +86,9 @@ final class GrahamScan } } - $temp = $points[$i]; + $temp = $points[$i]; $points[$size + 1] = $points[$i]; - $points[$i] = $points[$size + 1]; + $points[$i] = $points[$size + 1]; ++$size; } @@ -102,7 +100,18 @@ final class GrahamScan return $hull; } - public static function ccw(array $a, array $b, array $c) + /** + * Counterclockwise rotation + * + * @param float[] $a Vector + * @param float[] $b Vector + * @param float[] $c Vector + * + * @return int|float + * + * @since 1.0.0 + */ + public static function ccw(array $a, array $b, array $c) : int|float { return (($b['x'] - $a['x']) * ($c['y'] - $a['y']) - ($b['y'] - $a['y']) * ($c['x'] - $a['x'])); } diff --git a/Stdlib/Base/SmartDateTime.php b/Stdlib/Base/SmartDateTime.php index 686b347a6..30b456207 100755 --- a/Stdlib/Base/SmartDateTime.php +++ b/Stdlib/Base/SmartDateTime.php @@ -360,26 +360,68 @@ class SmartDateTime extends \DateTime return $days; } + /** + * Get the start of the year based on a custom starting month + * + * @param int $month Start of the year (i.e. fiscal year) + * + * @return \DateTime + * + * @since 1.0.0 + */ public static function startOfYear(int $month = 1) : \DateTime { return new \DateTime(\date('Y') . '-' . \sprintf('%02d', $month) . '-01'); } + /** + * Get the end of the year based on a custom starting month + * + * @param int $month Start of the year (i.e. fiscal year) + * + * @return \DateTime + * + * @since 1.0.0 + */ public static function endOfYear(int $month = 1) : \DateTime { return new \DateTime(\date('Y') . '-' . self::calculateMonthIndex(13 - $month, $month) . '-31'); } + /** + * Get the start of the month + * + * @return \DateTime + * + * @since 1.0.0 + */ public static function startOfMonth() : \DateTime { return new \DateTime(\date('Y-m') . '-01'); } + /** + * Get the end of the month + * + * @return \DateTime + * + * @since 1.0.0 + */ public static function endOfMonth() : \DateTime { return new \DateTime(\date('Y-m-t')); } + /** + * Calculate the difference in months between two dates + * + * @param \DateTime $d1 First datetime + * @param \DateTime $d2 Second datetime + * + * @return int + * + * @since 1.0.0 + */ public static function monthDiff(\DateTime $d1, \DateTime $d2) : int { $interval = $d1->diff($d2); diff --git a/Stdlib/Tree/BinarySearchTree.php b/Stdlib/Tree/BinarySearchTree.php index 703483185..0bff1b872 100644 --- a/Stdlib/Tree/BinarySearchTree.php +++ b/Stdlib/Tree/BinarySearchTree.php @@ -24,13 +24,35 @@ namespace phpOMS\Stdlib\Tree; */ class BinarySearchTree { + /** + * Root node + * + * @param null|Node + * @since 1.0.0 + */ public ?Node $root = null; + /** + * Constructor. + * + * @param null|Node $root Root node + * + * @since 1.0.0 + */ public function __construct(Node $root = null) { $this->root = $root; } + /** + * Search node by data + * + * @param mixed $data Data to search for + * + * @return null|Node + * + * @since 1.0.0 + */ public function search(mixed $data) : ?Node { if ($this->root === null) { @@ -48,6 +70,13 @@ class BinarySearchTree return $this->root; } + /** + * Find the smallest node + * + * @return null|Node + * + * @since 1.0.0 + */ public function minimum() : ?Node { if ($this->root === null) { @@ -61,6 +90,13 @@ class BinarySearchTree return $this->root->left->minimum(); } + /** + * Find the largest node + * + * @return null|Node + * + * @since 1.0.0 + */ public function maximum() : ?Node { if ($this->root === null) { @@ -74,6 +110,15 @@ class BinarySearchTree return $this->root->right->minimum(); } + /** + * Find the predecessor of a node + * + * @param Node $node Node + * + * @return null|Node + * + * @since 1.0.0 + */ public function predecessor(Node $node) : ?Node { if ($node->left !== null) { @@ -83,12 +128,21 @@ class BinarySearchTree $top = $node->parent; while ($top !== Null && $top->compare($node->data)) { $node = $top; - $top = $top->parent; + $top = $top->parent; } return $top; } + /** + * Find the successor of a node + * + * @param Node $node Node + * + * @return null|Node + * + * @since 1.0.0 + */ public function successor(Node $node) : ?Node { if ($node->right !== null) { @@ -98,18 +152,27 @@ class BinarySearchTree $top = $node->parent; while ($top !== null && $top->compare($node->data)) { $node = $top; - $top = $top->parent; + $top = $top->parent; } return $top; } + /** + * Insert a node + * + * @param Node $node Node + * + * @return void + * + * @since 1.0.0 + */ public function insert(Node $node) : void { if ($this->root === null) { - $new = new Node($node->key, $node->data); + $new = new Node($node->key, $node->data); $new->parent = null; - $new->tree = $this; + $new->tree = $this; $this->root = $new; @@ -122,24 +185,24 @@ class BinarySearchTree if ($comparison < 0) { if ($current->left === null) { - $BST = new BinarySearchTree(); - $new = new Node($node->key, $node->data); + $BST = new BinarySearchTree(); + $new = new Node($node->key, $node->data); $new->parent = $current; - $new->tree = $BST; + $new->tree = $BST; - $BST->root = $new; + $BST->root = $new; $current->left = $BST; } else { $current = $current->left->root; } } elseif ($comparison > 0) { if ($current->right === null) { - $BST = new BinarySearchTree(); - $new = new Node($node->key, $node->data); + $BST = new BinarySearchTree(); + $new = new Node($node->key, $node->data); $new->parent = $current; - $new->tree = $BST; + $new->tree = $BST; - $BST->root = $new; + $BST->root = $new; $current->right = $BST; } else { $current = $current->right->root; @@ -150,6 +213,15 @@ class BinarySearchTree } } + /** + * Delete a node + * + * @param Node $node Node + * + * @return void + * + * @since 1.0.0 + */ public function delete(Node &$node) : void { if ($node->left === null && $node->right === null) { @@ -201,10 +273,10 @@ class BinarySearchTree return; } else { $temp = $this->successor($node); - $node->key = $temp->key; + $node->key = $temp->key; $node->data = $temp->data; $this->delete($temp); } } -} \ No newline at end of file +} diff --git a/Stdlib/Tree/Node.php b/Stdlib/Tree/Node.php index 9d2620d91..3d8c0ce83 100644 --- a/Stdlib/Tree/Node.php +++ b/Stdlib/Tree/Node.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace phpOMS\Stdlib\Tree; /** - * Priority queue class. + * Tree node class. * * @package phpOMS\Stdlib\Tree * @license OMS License 2.0 @@ -24,26 +24,79 @@ namespace phpOMS\Stdlib\Tree; */ class Node { + /** + * Key of the node + * + * @var string + * @since 1.0.0 + */ public string $key = ''; + /** + * Data of the node + * + * @var mixed + * @since 1.0.0 + */ public mixed $data = null; + /** + * Sub-tree to the left + * + * @var null|BinarySearchTree + * @since 1.0.0 + */ public ?BinarySearchTree $left = null; + /** + * Sub-tree to the right + * + * @var null|BinarySearchTree + * @since 1.0.0 + */ public ?BinarySearchTree $right = null; + /** + * Parent node + * + * @var null|Node + * @since 1.0.0 + */ public ?self $parent = null; + /** + * Parent tree + * + * @var null|BinarySearchTree + * @since 1.0.0 + */ public ?BinarySearchTree $tree = null; + /** + * Constructor. + * + * @param string $key Node key + * @param mixed $data Node data + * + * @since 1.0.0 + */ public function __construct(string $key, mixed $data = null) { $this->key = $key; $this->data = $data; } + /** + * Compare node data + * + * @param mixed $data Node data to compare with + * + * @return -1|0|1 + * + * @since 1.0.0 + */ public function compare(mixed $data) : int { return $this->data <=> $data; } -} \ No newline at end of file +}