phpstan fixes

This commit is contained in:
Dennis Eichhorn 2019-10-07 22:22:41 +02:00
parent 921703a5b5
commit 0422411c89
31 changed files with 354 additions and 130 deletions

View File

@ -14,10 +14,10 @@
declare(strict_types=1);
namespace phpOMS\Algorithm\Backpack;
namespace phpOMS\Algorithm\Knappsack;
/**
* Matching a value with a set of coins
* Backpack for the Knappsack problem
*
* @package phpOMS\Algorithm\Knappsack
* @license OMS License 1.0
@ -26,38 +26,99 @@ namespace phpOMS\Algorithm\Backpack;
*/
class Backpack
{
private $maxCost = 0.0;
/**
* Maximum amount of cost this backpack can hold
*
* @var float
* @since 1.0.0
*/
private float $maxCost = 0.0;
private $value = 0.0;
/**
* Current value
*
* @var float
* @since 1.0.0
*/
private float $value = 0.0;
private $cost = 0.0;
/**
* Current cost
*
* @var float
* @since 1.0.0
*/
private float $cost = 0.0;
/**
* Items inside the backpack
*
* @var Item[]
* @since 1.0.0
*/
private array $items = [];
public function __construct($maxCost)
/**
* Constructor.
*
* @param float $maxCost Maximum amount of costs the backpack can hold
*
* @since 1.0.0
*/
public function __construct(float $maxCost)
{
$this->maxCost = $maxCost;
}
public function getValue()
/**
* Get the value of the stored items
*
* @return float
*
* @since 1.0.0
*/
public function getValue() : float
{
return $this->value;
}
public function getCost()
/**
* Get the cost of the stored items
*
* @return float
*
* @since 1.0.0
*/
public function getCost() : float
{
return $this->cost;
}
/**
* Get items
*
* @return array
*
* @since 1.0.0
*/
public function getItems() : array
{
return $this->items;
}
/**
* Add item to backpack
*
* @param Item $item Item
*
* @return void
*
* @since 1.0.0
*/
public function addItem(Item $item) : void
{
$this->items[] = $item;
$this->value += $item->getValue();
$this->cost += $item->getCost();
$this->value += $item->getValue();
$this->cost += $item->getCost();
}
}

View File

@ -14,6 +14,8 @@ declare(strict_types=1);
namespace phpOMS\Algorithm\PathFinding;
use phpOMS\Stdlib\Base\Heap;
/**
* Perform path finding.
*
@ -24,14 +26,19 @@ namespace phpOMS\Algorithm\PathFinding;
*/
class JumpPointSearch implements PathFinderInterface
{
/**
* {@inheritdoc}
*/
public static function findPath(
int $startX, int $startY,
int $endX, int $endY,
Grid $grid,
int $heuristic, int $movement
) : Path {
/** @var null|AStarNode $startNode */
$startNode = $grid->getNode($startX, $startY);
$endNode = $grid->getNode($endX, $endY);
/** @var null|AStarNode $endNode */
$endNode = $grid->getNode($endX, $endY);
if ($startNode === null || $endNode === null) {
return new Path($grid);
@ -41,8 +48,9 @@ class JumpPointSearch implements PathFinderInterface
$startNode->setF(0.0);
$startNode->setOpened(true);
$openList = new Heap(function($node1, $node2) { return $node1->getF() - $node2->getF(); });
$openList = new Heap(function(AStarNode $node1, AStarNode $node2) { return $node1->getF() - $node2->getF(); });
$openList->push($startNode);
$node = null;
while (!$openList->isEmpty()) {

View File

@ -51,7 +51,7 @@ class Grid
public function getNode(int $x, int $y) : ?Node
{
if (!isset($this->nodes[$y]) || $this->nodes[$y][$x]) {
if (!isset($this->nodes[$y]) || !isset($this->nodes[$y][$x])) {
// todo: add null node to grid because we need to modify some properties later on and remember them!
return null;
}
@ -59,6 +59,15 @@ class Grid
return $this->nodes[$y][$x];
}
public function isWalkable(int $x, int $y) : bool
{
if (!isset($this->nodes[$y]) || !isset($this->nodes[$y][$x]) || !$this->nodes[$y][$x]->isWalkable()) {
return false;
}
return true;
}
public function getNeighbors(Node $node, int $movement) : array
{
$x = $node->getX();
@ -75,22 +84,22 @@ class Grid
$d3 = false;
// todo: check $x and $y because original implementation is flipped!!!
if ($this->getNode($x, $y - 1)->isWalkable()) {
if ($this->isWalkable($x, $y - 1)) {
$neighbors[] = $this->getNode($x, $y - 1);
$s0 = true;
}
if ($this->getNode($x + 1, $y)->isWalkable()) {
if ($this->isWalkable($x + 1, $y)) {
$neighbors[] = $this->getNode($x + 1, $y);
$s1 = true;
}
if ($this->getNode($x, $y + 1)->isWalkable()) {
if ($this->isWalkable($x, $y + 1)) {
$neighbors[] = $this->getNode($x, $y + 1);
$s2 = true;
}
if ($this->getNode($x - 1, $y)->isWalkable()) {
if ($this->isWalkable($x - 1, $y)) {
$neighbors[] = $this->getNode($x - 1, $y);
$s3 = true;
}
@ -116,19 +125,19 @@ class Grid
$d3 = true;
}
if ($d0 && $this->getNode($x - 1, $y - 1)->isWalkable()) {
if ($d0 && $this->isWalkable($x - 1, $y - 1)) {
$neighbors[] = $this->getNode($x - 1, $y - 1);
}
if ($d1 && $this->getNode($x + 1, $y - 1)->isWalkable()) {
if ($d1 && $this->isWalkable($x + 1, $y - 1)) {
$neighbors[] = $this->getNode($x + 1, $y - 1);
}
if ($d2 && $this->getNode($x + 1, $y + 1)->isWalkable()) {
if ($d2 && $this->isWalkable($x + 1, $y + 1)) {
$neighbors[] = $this->getNode($x + 1, $y + 1);
}
if ($d3 && $this->getNode($x - 1, $y + 1)->isWalkable()) {
if ($d3 && $this->isWalkable($x - 1, $y + 1)) {
$neighbors[] = $this->getNode($x - 1, $y + 1);
}

View File

@ -14,6 +14,8 @@ declare(strict_types=1);
namespace phpOMS\Algorithm\PathFinding;
use phpOMS\Stdlib\Base\Heap;
/**
* Perform path finding.
*
@ -24,14 +26,19 @@ namespace phpOMS\Algorithm\PathFinding;
*/
class JumpPointSearch implements PathFinderInterface
{
/**
* {@inheritdoc}
*/
public static function findPath(
int $startX, int $startY,
int $endX, int $endY,
Grid $grid,
int $heuristic, int $movement
) : Path {
/** @var null|JumpPointNode $startNode */
$startNode = $grid->getNode($startX, $startY);
$endNode = $grid->getNode($endX, $endY);
/** @var null|JumpPointNode $endNode */
$endNode = $grid->getNode($endX, $endY);
if ($startNode === null || $endNode === null) {
return new Path($grid);
@ -66,7 +73,7 @@ class JumpPointSearch implements PathFinderInterface
return $path;
}
public static function identifySuccessors(Node $node, Grid $grid, int $heuristic, int $movement, Node $endNode, Heap $openList) : Heap
public static function identifySuccessors(JumpPointNode $node, Grid $grid, int $heuristic, int $movement, JumpPointNode $endNode, Heap $openList) : Heap
{
$neighbors = self::findNeighbors($node, $movement, $grid);
$neighborsLength = \count($neighbors);
@ -84,7 +91,7 @@ class JumpPointSearch implements PathFinderInterface
if (!$jumpPoint->isOpened() || $ng < $jumpPoint->getG()) {
$jumpPoint->setG($ng);
$jumpPoint->setH($jumpPoint->getH() ?? Heuristic::metric($jumpPoint->getCoordinates(), $endNode->getCoordinates(), $metric));
$jumpPoint->setH($jumpPoint->getH() ?? Heuristic::metric($jumpPoint->getCoordinates(), $endNode->getCoordinates(), $heuristic));
$jumpPoint->setF($jumpPoint->getG() + $jumpPoint->getH());
$jumpPoint->setParent($node);
@ -100,7 +107,7 @@ class JumpPointSearch implements PathFinderInterface
return $openList;
}
private static function findNeighbors(Node $node, int $movement, Grid $grid) : array
private static function findNeighbors(JumpPointNode $node, int $movement, Grid $grid) : array
{
if ($movement === MovementType::STRAIGHT) {
return self::findNeighborsStraight($node, $grid);
@ -113,7 +120,7 @@ class JumpPointSearch implements PathFinderInterface
return self::findNeighborsDiagonalNoObstacle($node, $grid);
}
private static function findNeighborsStraight(Node $node, Grid $grid) : array
private static function findNeighborsStraight(JumpPointNode $node, Grid $grid) : array
{
if ($node->getParent() === null) {
return $grid->getNeighbors($node, MovementType::STRAIGHT);
@ -125,32 +132,34 @@ class JumpPointSearch implements PathFinderInterface
$px = $node->getParent()->getX();
$py = $node->getParent()->getY();
/** @var int $dx */
$dx = ($x - $px) / \max(\abs($x - $px), 1);
/** @var int $dy */
$dy = ($y - $py) / \may(\abs($y - $py), 1);
$neighbors = [];
if ($dx !== 0) {
if ($grid->getNode($x, $y - 1)->isWalkable()) {
if ($grid->isWalkable($x, $y - 1)) {
$neighbors[] = $grid->getNode($x, $y - 1);
}
if ($grid->getNode($x, $y + 1)->isWalkable()) {
if ($grid->isWalkable($x, $y + 1)) {
$neighbors[] = $grid->getNode($x, $y + 1);
}
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
}
} elseif ($dy !== 0) {
if ($grid->getNode($x - 1, $y)->isWalkable()) {
if ($grid->isWalkable($x - 1, $y)) {
$neighbors[] = $grid->getNode($x - 1, $y);
}
if ($grid->getNode($x + 1, $y)->isWalkable()) {
if ($grid->isWalkable($x + 1, $y)) {
$neighbors[] = $grid->getNode($x + 1, $y);
}
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
}
}
@ -158,7 +167,7 @@ class JumpPointSearch implements PathFinderInterface
return $neighbors;
}
private static function findNeighborsDiagonal(Node $node, Grid $grid) : array
private static function findNeighborsDiagonal(JumpPointNode $node, Grid $grid) : array
{
if ($node->getParent() === null) {
return $grid->getNeighbors($node, MovementType::DIAGONAL);
@ -170,52 +179,54 @@ class JumpPointSearch implements PathFinderInterface
$px = $node->getParent()->getX();
$py = $node->getParent()->getY();
/** @var int $dx */
$dx = ($x - $px) / \max(\abs($x - $px), 1);
/** @var int $dy */
$dy = ($y - $py) / \may(\abs($y - $py), 1);
$neighbors = [];
if ($dx !== 0 && $dy !== 0) {
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
}
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
}
if ($grid->getNode($x + $dx, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y + $dy)) {
$neighbors[] = $grid->getNode($x + $dx, $y + $dy);
}
if (!$grid->getNode($x - $dx, $y)->isWalkable()) {
if (!$grid->isWalkable($x - $dx, $y)) {
$neighbors[] = $grid->getNode($x - $dx, $y + $dy);
}
if (!$grid->getNode($x, $y - $dy)->isWalkable()) {
if (!$grid->isWalkable($x, $y - $dy)) {
$neighbors[] = $grid->getNode($x + $dx, $y - $dy);
}
} elseif ($dx === 0) {
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
}
if (!$grid->getNode($x + 1, $y)->isWalkable()) {
if (!$grid->isWalkable($x + 1, $y)) {
$neighbors[] = $grid->getNode($x + 1, $y + $dy);
}
if (!$grid->getNode($x - 1, $y)->isWalkable()) {
if (!$grid->isWalkable($x - 1, $y)) {
$neighbors[] = $grid->getNode($x - 1, $y + $dy);
}
} else {
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
}
if (!$grid->getNode($x, $y + 1)->isWalkable()) {
if (!$grid->isWalkable($x, $y + 1)) {
$neighbors[] = $grid->getNode($x + $dx, $y + 1);
}
if (!$grid->getNode($x, $y - 1)->isWalkable()) {
if (!$grid->isWalkable($x, $y - 1)) {
$neighbors[] = $grid->getNode($x + $dx, $y - 1);
}
}
@ -223,7 +234,7 @@ class JumpPointSearch implements PathFinderInterface
return $neighbors;
}
private static function findNeighborsDiagonalOneObstacle(Node $node, Grid $grid) : array
private static function findNeighborsDiagonalOneObstacle(JumpPointNode $node, Grid $grid) : array
{
if ($node->getParent() === null) {
return $grid->getNeighbors($node, MovementType::DIAGONAL_ONE_OBSTACLE);
@ -235,47 +246,49 @@ class JumpPointSearch implements PathFinderInterface
$px = $node->getParent()->getX();
$py = $node->getParent()->getY();
/** @var int $dx */
$dx = ($x - $px) / \max(\abs($x - $px), 1);
/** @var int $dy */
$dy = ($y - $py) / \may(\abs($y - $py), 1);
$neighbors = [];
if ($dx !== 0 && $dy !== 0) {
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
}
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
}
if ($grid->getNode($x, $y + $dy) || $grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy) || $grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y + $dy);
}
if (!$grid->getNode($x - $dx, $y) && $grid->getNode($x, $y + $dy)->isWalkable()) {
if (!$grid->getNode($x - $dx, $y) && $grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x - $dx, $y + $dy);
}
if (!$grid->getNode($x, $y - $dy) && $grid->getNode($x + $dx, $y)->isWalkable()) {
if (!$grid->getNode($x, $y - $dy) && $grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y - $dy);
}
} elseif ($dx === 0) {
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
if (!$grid->getNode($x + 1, $y)->isWalkable()) {
if (!$grid->isWalkable($x + 1, $y)) {
$neighbors[] = $grid->getNode($x + 1, $y + $dy);
}
if (!$grid->getNode($x - 1, $y)->isWalkable()) {
if (!$grid->isWalkable($x - 1, $y)) {
$neighbors[] = $grid->getNode($x - 1, $y + $dy);
}
}
} else {
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
if (!$grid->getNode($x, $y + 1)->isWalkable()) {
if (!$grid->isWalkable($x, $y + 1)) {
$neighbors[] = $grid->getNode($x + $dx, $y + 1);
}
if (!$grid->getNode($x, $y - 1)->isWalkable()) {
if (!$grid->isWalkable($x, $y - 1)) {
$neighbors[] = $grid->getNode($x + $dx, $y - 1);
}
}
@ -284,7 +297,7 @@ class JumpPointSearch implements PathFinderInterface
return $neighbors;
}
private static function findNeighborsDiagonalNoObstacle(Node $node, Grid $grid) : array
private static function findNeighborsDiagonalNoObstacle(JumpPointNode $node, Grid $grid) : array
{
if ($node->getParent() === null) {
return $grid->getNeighbors($node, MovementType::DIAGONAL_NO_OBSTACLE);
@ -296,26 +309,28 @@ class JumpPointSearch implements PathFinderInterface
$px = $node->getParent()->getX();
$py = $node->getParent()->getY();
/** @var int $dx */
$dx = ($x - $px) / \max(\abs($x - $px), 1);
/** @var int $dy */
$dy = ($y - $py) / \may(\abs($y - $py), 1);
$neighbors = [];
if ($dx !== 0 && $dy !== 0) {
if ($grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy)) {
$neighbors[] = $grid->getNode($x, $y + $dy);
}
if ($grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y);
}
if ($grid->getNode($x, $y + $dy) || $grid->getNode($x + $dx, $y)->isWalkable()) {
if ($grid->isWalkable($x, $y + $dy) || $grid->isWalkable($x + $dx, $y)) {
$neighbors[] = $grid->getNode($x + $dx, $y + $dy);
}
} elseif ($dx !== 0 && $dy === 0) {
$isNextWalkable = $grid->getNode($x + $dx, $y)->isWalkable();
$isTopWalkable = $grid->getNode($x, $y + 1)->isWalkable();
$isBottomWalkable = $grid->getNode($x, $y - 1)->isWalkable();
$isNextWalkable = $grid->isWalkable($x + $dx, $y);
$isTopWalkable = $grid->isWalkable($x, $y + 1);
$isBottomWalkable = $grid->isWalkable($x, $y - 1);
if ($isNextWalkable) {
$neighbors[] = $grid->getNode($x + $dx, $y);
@ -336,9 +351,9 @@ class JumpPointSearch implements PathFinderInterface
$neighbors[] = $grid->getNode($x, $y - 1);
}
} elseif ($dx === 0 && $dy !== 0) {
$isNextWalkable = $grid->getNode($x, $y + $dy)->isWalkable();
$isRightWalkable = $grid->getNode($x + 1, $y)->isWalkable();
$isLeftWalkable = $grid->getNode($x - 1, $y)->isWalkable();
$isNextWalkable = $grid->isWalkable($x, $y + $dy);
$isRightWalkable = $grid->isWalkable($x + 1, $y);
$isLeftWalkable = $grid->isWalkable($x - 1, $y);
if ($isNextWalkable) {
$neighbors[] = $grid->getNode($x, $y + $dy);
@ -363,7 +378,7 @@ class JumpPointSearch implements PathFinderInterface
return $neighbors;
}
private static function jump(Node $node, Node $endNode, int $movement, Grid $grid) : ?Node
private static function jump(JumpPointNode $node, JumpPointNode $endNode, int $movement, Grid $grid) : ?JumpPointNode
{
if ($movement === MovementType::STRAIGHT) {
return self::jumpStraight($node, $endNode, $grid);
@ -376,7 +391,7 @@ class JumpPointSearch implements PathFinderInterface
return self::jumpDiagonalNoObstacle($node, $endNode, $grid);
}
private static function jumpStraight(Node $node, Node $endNode, Grid $grid) : ?Node
private static function jumpStraight(JumpPointNode $node, JumpPointNode $endNode, Grid $grid) : ?JumpPointNode
{
$x = $node->getX();
$y = $node->getY();
@ -396,14 +411,14 @@ class JumpPointSearch implements PathFinderInterface
}
if ($dx !== 0) {
if (($grid->getNode($x, $y - 1)->isWalkable() && !$grid->getNode($x - $dx, $y - 1)->isWalkable())
|| ($grid->getNode($x, $y + 1)->isWalkable() && !$grid->getNode($x - $dx, $y + 1)->isWalkable())
if (($grid->isWalkable($x, $y - 1) && !$grid->isWalkable($x - $dx, $y - 1))
|| ($grid->isWalkable($x, $y + 1) && !$grid->isWalkable($x - $dx, $y + 1))
) {
return $node;
}
} elseif ($dy !== 0) {
if (($grid->getNode($x - 1, $y)->isWalkable() && !$grid->getNode($x - 1, $y - $dy)->isWalkable())
|| ($grid->getNode($x + 1, $y)->isWalkable() && !$grid->getNode($x + 1, $y - $dy)->isWalkable())
if (($grid->isWalkable($x - 1, $y) && !$grid->isWalkable($x - 1, $y - $dy))
|| ($grid->isWalkable($x + 1, $y) && !$grid->isWalkable($x + 1, $y - $dy))
) {
return $node;
}
@ -420,7 +435,7 @@ class JumpPointSearch implements PathFinderInterface
return self::jumpStraight($grid->getNode($x + $dx, $y + $dy), $node, $grid);
}
private static function jumpDiagonal(Node $node, Node $endNode, Grid $grid) : ?Node
private static function jumpDiagonal(JumpPointNode $node, JumpPointNode $endNode, Grid $grid) : ?JumpPointNode
{
$x = $node->getX();
$y = $node->getY();
@ -440,8 +455,8 @@ class JumpPointSearch implements PathFinderInterface
}
if ($dx !== 0 && $dy !== 0) {
if (($grid->getNode($x - $dx, $y + $dy)->isWalkable() && !$grid->getNode($x - $dx, $y)->isWalkable())
|| ($grid->getNode($x + $dx, $y - $dy)->isWalkable() && !$grid->getNode($x, $y - $dy)->isWalkable())
if (($grid->isWalkable($x - $dx, $y + $dy) && !$grid->isWalkable($x - $dx, $y))
|| ($grid->isWalkable($x + $dx, $y - $dy) && !$grid->isWalkable($x, $y - $dy))
) {
return $node;
}
@ -452,14 +467,14 @@ class JumpPointSearch implements PathFinderInterface
return $node;
}
} elseif ($dx !== 0 && $dy === 0) {
if (($grid->getNode($x + $dx, $y + 1)->isWalkable() && !$grid->getNode($x, $y + 1)->isWalkable())
|| ($grid->getNode($x + $dx, $y - 1)->isWalkable() && !$grid->getNode($x, $y - 1)->isWalkable())
if (($grid->isWalkable($x + $dx, $y + 1) && !$grid->isWalkable($x, $y + 1))
|| ($grid->isWalkable($x + $dx, $y - 1) && !$grid->isWalkable($x, $y - 1))
) {
return $node;
}
} else {
if (($grid->getNode($x + 1, $y + $dy)->isWalkable() && !$grid->getNode($x + 1, $y)->isWalkable())
|| ($grid->getNode($x - 1, $y + $dy)->isWalkable() && !$grid->getNode($x - 1, $y)->isWalkable())
if (($grid->isWalkable($x + 1, $y + $dy) && !$grid->isWalkable($x + 1, $y))
|| ($grid->isWalkable($x - 1, $y + $dy) && !$grid->isWalkable($x - 1, $y))
) {
return $node;
}
@ -468,7 +483,7 @@ class JumpPointSearch implements PathFinderInterface
return self::jumpDiagonal($grid->getNode($x + $dx, $y + $dy), $node, $grid);
}
private static function jumpDiagonalOneObstacle(Node $node, Node $endNode, Grid $grid) : ?Node
private static function jumpDiagonalOneObstacle(JumpPointNode $node, JumpPointNode $endNode, Grid $grid) : ?JumpPointNode
{
$x = $node->getX();
$y = $node->getY();
@ -488,8 +503,8 @@ class JumpPointSearch implements PathFinderInterface
}
if ($dx !== 0 && $dy !== 0) {
if (($grid->getNode($x - $dx, $y + $dy)->isWalkable() && !$grid->getNode($x - $dx, $y)->isWalkable())
|| ($grid->getNode($x + $dx, $y - $dy)->isWalkable() && !$grid->getNode($x, $y - $dy)->isWalkable())
if (($grid->isWalkable($x - $dx, $y + $dy) && !$grid->isWalkable($x - $dx, $y))
|| ($grid->isWalkable($x + $dx, $y - $dy) && !$grid->isWalkable($x, $y - $dy))
) {
return $node;
}
@ -500,27 +515,27 @@ class JumpPointSearch implements PathFinderInterface
return $node;
}
} elseif ($dx !== 0 && $dy === 0) {
if (($grid->getNode($x + $dx, $y + 1)->isWalkable() && !$grid->getNode($x, $y + 1)->isWalkable())
|| ($grid->getNode($x + $dx, $y - 1)->isWalkable() && !$grid->getNode($x, $y - 1)->isWalkable())
if (($grid->isWalkable($x + $dx, $y + 1) && !$grid->isWalkable($x, $y + 1))
|| ($grid->isWalkable($x + $dx, $y - 1) && !$grid->isWalkable($x, $y - 1))
) {
return $node;
}
} else {
if (($grid->getNode($x + 1, $y + $dy)->isWalkable() && !$grid->getNode($x + 1, $y)->isWalkable())
|| ($grid->getNode($x - 1, $y + $dy)->isWalkable() && !$grid->getNode($x - 1, $y)->isWalkable())
if (($grid->isWalkable($x + 1, $y + $dy) && !$grid->isWalkable($x + 1, $y))
|| ($grid->isWalkable($x - 1, $y + $dy) && !$grid->isWalkable($x - 1, $y))
) {
return $node;
}
}
if ($grid->getNode($x + $dx, $y)->isWalkable() || $grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y) || $grid->isWalkable($x, $y + $dy)) {
return self::jumpDiagonalOneObstacle($grid->getNode($x + $dx, $y + $dy), $node, $grid);
}
return null;
}
private static function jumpDiagonalNoObstacle(Node $node, Node $endNode, Grid $grid) : ?Node
private static function jumpDiagonalNoObstacle(JumpPointNode $node, JumpPointNode $endNode, Grid $grid) : ?JumpPointNode
{
$x = $node->getX();
$y = $node->getY();
@ -546,20 +561,20 @@ class JumpPointSearch implements PathFinderInterface
return $node;
}
} elseif ($dx !== 0 && $dy === 0) {
if (($grid->getNode($x, $y - 1)->isWalkable() && !$grid->getNode($x - $dx, $y - 1)->isWalkable())
|| ($grid->getNode($x, $y + 1)->isWalkable() && !$grid->getNode($x - $dx, $y + 1)->isWalkable())
if (($grid->isWalkable($x, $y - 1) && !$grid->isWalkable($x - $dx, $y - 1))
|| ($grid->isWalkable($x, $y + 1) && !$grid->isWalkable($x - $dx, $y + 1))
) {
return $node;
}
} elseif ($dx === 0 && $dy !== 0) {
if (($grid->getNode($x - 1, $y)->isWalkable() && !$grid->getNode($x - 1, $y - $dy)->isWalkable())
|| ($grid->getNode($x + 1, $y)->isWalkable() && !$grid->getNode($x + 1, $y - $dy)->isWalkable())
if (($grid->isWalkable($x - 1, $y) && !$grid->isWalkable($x - 1, $y - $dy))
|| ($grid->isWalkable($x + 1, $y) && !$grid->isWalkable($x + 1, $y - $dy))
) {
return $node;
}
}
if ($grid->getNode($x + $dx, $y)->isWalkable() || $grid->getNode($x, $y + $dy)->isWalkable()) {
if ($grid->isWalkable($x + $dx, $y) || $grid->isWalkable($x, $y + $dy)) {
return self::jumpDiagonalNoObstacle($grid->getNode($x + $dx, $y + $dy), $node, $grid);
}

View File

@ -98,6 +98,10 @@ class Path
}
$node = $this->grid->getNode($x0, $y0);
if ($node === null) {
break;
}
}
return $line;

View File

@ -23,6 +23,21 @@ namespace phpOMS\Algorithm\PathFinding;
* @since 1.0.0
*/
interface PathFinderInterface {
/**
* Find path from one point to another
*
* @param int $startX Start point X-Coordinate
* @param int $startY Start point Y-Coordinate
* @param int $endX End point X-Coordinate
* @param int $endY End point Y-Coordinate
* @param Grid $grid Grid with the walkable points
* @param int $heuristic Heuristic algorithm to use in order to calculate the distance for a good path
* @param int $movement Allowed movement (e.g. straight, diagonal, ...)
*
* @return Path
*
* @since 1.0.0
*/
public static function findPath(
int $startX, int $startY,
int $endX, int $endY,

View File

@ -24,6 +24,16 @@ namespace phpOMS\Business\Finance;
*/
final class Depreciation
{
/**
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Calculate linear depretiation rate
*

View File

@ -29,6 +29,15 @@ use phpOMS\Math\Statistic\Average;
*/
final class FinanceFormulas
{
/**
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Annual Percentage Yield

View File

@ -27,6 +27,16 @@ namespace phpOMS\Business\Finance;
*/
final class Loan
{
/**
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Balloon Loan - Payments
*

View File

@ -24,6 +24,16 @@ namespace phpOMS\Business\Finance;
*/
final class Lorenzkurve
{
/**
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Calculate Gini coefficient
*

View File

@ -27,6 +27,16 @@ namespace phpOMS\Business\Finance;
*/
final class StockBonds
{
/**
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Bond Equivalent Yield
*

View File

@ -30,6 +30,7 @@ final class Metrics
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{

View File

@ -30,6 +30,7 @@ final class Metrics
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{

View File

@ -31,6 +31,7 @@ final class MarketShareEstimation
* Constructure
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{

View File

@ -44,10 +44,10 @@ abstract class SettingsAbstract implements OptionsInterface
/**
* Database connection instance.
*
* @var null|ConnectionAbstract
* @var ConnectionAbstract
* @since 1.0.0
*/
protected ?ConnectionAbstract $connection = null;
protected ConnectionAbstract $connection;
/**
* Settings table.

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database;
use phpOMS\DataStorage\Database\Query\QueryType;
use phpOMS\DataStorage\DataStorageConnectionInterface;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
/**
* Database query builder.
@ -30,18 +30,18 @@ abstract class BuilderAbstract
/**
* Grammar.
*
* @var null|GrammarAbstract
* @var GrammarAbstract
* @since 1.0.0
*/
protected ?GrammarAbstract $grammar = null;
protected GrammarAbstract $grammar;
/**
* Database connection.
*
* @var null|DataStorageConnectionInterface
* @var ConnectionAbstract
* @since 1.0.0
*/
protected ?DataStorageConnectionInterface $connection = null;
protected ConnectionAbstract $connection;
/**
* Query type.
@ -70,11 +70,11 @@ abstract class BuilderAbstract
/**
* Get connection
*
* @return DataStorageConnectionInterface
* @return ConnectionAbstract
*
* @since 1.0.0
*/
public function getConnection() : DataStorageConnectionInterface
public function getConnection() : ConnectionAbstract
{
return $this->connection;
}

View File

@ -38,10 +38,10 @@ class DataMapperAbstract implements DataMapperInterface
/**
* Database connection.
*
* @var null|ConnectionAbstract
* @var ConnectionAbstract
* @since 1.0.0
*/
protected static ?ConnectionAbstract $db = null;
protected static ConnectionAbstract $db;
/**
* Overwriting extended values.
@ -2003,9 +2003,6 @@ class DataMapperAbstract implements DataMapperInterface
}
/**
* Populate data.
*
* @param mixed $obj Object to add the relations to
*
* @return void
*

View File

@ -18,6 +18,7 @@ use phpOMS\DataStorage\Database\Connection\ConnectionFactory;
use phpOMS\DataStorage\Database\Connection\NullConnection;
use phpOMS\DataStorage\DataStorageConnectionInterface;
use phpOMS\DataStorage\DataStoragePoolInterface;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
/**
* Database pool handler.
@ -33,7 +34,7 @@ final class DatabasePool implements DataStoragePoolInterface
/**
* Databases.
*
* @var DataStorageConnectionInterface[]
* @var ConnectionAbstract[]
* @since 1.0.0
*/
private array $pool = [];
@ -73,11 +74,11 @@ final class DatabasePool implements DataStoragePoolInterface
*
* @param string $key Database key
*
* @return DataStorageConnectionInterface
* @return ConnectionAbstract
*
* @since 1.0.0
*/
public function get(string $key = '') : DataStorageConnectionInterface
public function get(string $key = '') : ConnectionAbstract
{
if ((!empty($key) && !isset($this->pool[$key])) || empty($this->pool)) {
return new NullConnection();

View File

@ -194,7 +194,7 @@ abstract class GrammarAbstract
*
* @param array $elements Elements
* @param string $prefix Prefix for table
* @param string $column Is always column?
* @param bool $column Is column?
*
* @return string
*

View File

@ -316,12 +316,12 @@ class Grammar extends GrammarAbstract
return $this->compileSystem($value->getColumn(), $prefix);
} elseif ($value instanceof Builder) {
return '(' . \rtrim($value->toSql(), ';') . ')';
} elseif ($value instanceof \JsonSerializable) {
} elseif ($value instanceof \JsonSerializable) {
$encoded = \json_encode($value);
return $encoded ? $encoded : null;
} elseif ($value instanceof \Serializable) {
return $element->serialize();
return $encoded ? $encoded : 'NULL';
} elseif ($value instanceof \Serializable) {
return $value->serialize();
} else {
throw new \InvalidArgumentException(\gettype($value));
}

View File

@ -46,6 +46,6 @@ class MicrosoftGrammar extends Grammar
$query->limit = $query->limit ?? 1;
return 'SELECT TOP ' . $query->limit . ' ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST ' . $query->limit . ' ROWS ONLY';
return 'SELECT TOP ' . $query->limit . ' ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST ' . ($query->limit ?? 1) . ' ROWS ONLY';
}
}

View File

@ -53,6 +53,6 @@ class MysqlGrammar extends Grammar
$expression = '*';
}
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY \rand() ' . $this->compileLimit($query, $query->limit);
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY \rand() ' . $this->compileLimit($query, $query->limit ?? 1);
}
}

View File

@ -46,6 +46,6 @@ class OracleGrammar extends Grammar
$query->limit = $query->limit ?? 1;
return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum >= 1 AND rownum <= ' . $query->limit;
return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum >= 1 AND rownum <= ' . ($query->limit ?? 1);
}
}

View File

@ -44,6 +44,6 @@ class PostgresGrammar extends Grammar
$expression = '*';
}
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit);
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit ?? 1);
}
}

View File

@ -53,6 +53,6 @@ class SQLiteGrammar extends Grammar
$expression = '*';
}
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit);
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit ?? 1);
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\DataStorage\Database\Schema
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema;
/**
* Database table
*
* @package phpOMS\DataStorage\Database\Schema
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Field
{
}

View File

@ -59,7 +59,7 @@ class MysqlGrammar extends Grammar
* Compile from.
*
* @param Builder $query Builder
* @param array $table Tables
* @param string $table Tables
*
* @return string
*

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\DataStorage\Database\Schema
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Schema;
/**
* Database table
*
* @package phpOMS\DataStorage\Database\Schema
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Table
{
}

View File

@ -16,6 +16,8 @@ namespace phpOMS\DataStorage\Database;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Schema\Builder;
use phpOMS\DataStorage\Database\Schema\Field;
use phpOMS\DataStorage\Database\Schema\Table;
/**
* Database schema mapper.
@ -30,10 +32,10 @@ class SchemaMapper
/**
* Database connection.
*
* @var null|ConnectionAbstract
* @var ConnectionAbstract
* @since 1.0.0
*/
protected ?ConnectionAbstract $db = null;
protected ConnectionAbstract $db;
/**
* Constructor.
@ -95,11 +97,11 @@ class SchemaMapper
public function getFields(string $table) : array
{
$builder = new Builder($this->db);
$tNames = $builder->selectFields()->execute();
$tNames = $builder->selectFields($table)->execute();
$fields = [];
foreach ($tNames as $name) {
$fields[] = $this->getField($name);
$fields[] = $this->getField($table, $name);
}
return $fields;

View File

@ -56,10 +56,10 @@ final class ModuleManager
/**
* Application instance.
*
* @var null|ApplicationAbstract
* @var ApplicationAbstract
* @since 1.0.0
*/
private ?ApplicationAbstract $app = null;
private ApplicationAbstract $app;
/**
* Installed modules.
@ -249,7 +249,7 @@ final class ModuleManager
if (empty($this->all)) {
\chdir($this->modulePath);
$files = \glob('*', \GLOB_ONLYDIR);
$c = \count($files);
$c = $files === false ? 0 : \count($files);
for ($i = 0; $i < $c; ++$i) {
$path = $this->modulePath . '/' . $files[$i] . '/info.json';

View File

@ -26,6 +26,12 @@ class Heap
{
private \Closure $compare;
/**
* Heap elements
*
* @var array<int, mixed>
* @since 1.0.0
*/
private array $nodes = [];
public function __construct(\Closure $compare = null)
@ -39,7 +45,7 @@ class Heap
while ($lo < $hi) {
$mid = (int) \floor(($lo + $hi) / 2);
if (($this->compare)($x, $this->node[$mid]) < 0) {
if (($this->compare)($x, $this->nodes[$mid]) < 0) {
$hi = $mid;
} else {
$lo = $mid + 1;
@ -210,7 +216,7 @@ class Heap
$this->nodes = [];
}
public function empty() : bool
public function isEmpty() : bool
{
return empty($this->nodes);
}