This commit is contained in:
Dennis Eichhorn 2023-09-21 11:35:17 +00:00
parent e579f1ab30
commit 0d200da299
10 changed files with 43 additions and 22 deletions

View File

@ -68,7 +68,7 @@ final class DBSCAN
* *
* Array of points assigned to a cluster * Array of points assigned to a cluster
* *
* @var array<int, array{x:float, y:float}> * @var array<int, array>
* @since 1.0.0 * @since 1.0.0
*/ */
private array $clusters = []; private array $clusters = [];
@ -232,6 +232,7 @@ final class DBSCAN
$points[] = $p->coordinates; $points[] = $p->coordinates;
} }
// @todo: this is only good for 2D. Fix this for ND.
$this->convexHulls[$c] = MonotoneChain::createConvexHull($points); $this->convexHulls[$c] = MonotoneChain::createConvexHull($points);
} }
} }

View File

@ -106,7 +106,9 @@ final class JWT
return []; return [];
} }
return \json_decode(Base64Url::decode($explode[0]), true); $json = \json_decode(Base64Url::decode($explode[0]), true);
return \is_array($json) ? $json : [];
} }
/** /**
@ -126,7 +128,9 @@ final class JWT
return []; return [];
} }
return \json_decode(Base64Url::decode($explode[1]), true); $json = \json_decode(Base64Url::decode($explode[1]), true);
return \is_array($json) ? $json : [];
} }
/** /**
@ -150,6 +154,11 @@ final class JWT
try { try {
$header = \json_decode(Base64Url::decode($explode[0]), true); $header = \json_decode(Base64Url::decode($explode[0]), true);
$payload = \json_decode(Base64Url::decode($explode[1]), true); $payload = \json_decode(Base64Url::decode($explode[1]), true);
if (!\is_array($header) || !\is_array($payload)) {
return false;
}
$signature = self::createSignature($secret, $header, $payload); $signature = self::createSignature($secret, $header, $payload);
return \hash_equals($signature, $explode[2]); return \hash_equals($signature, $explode[2]);

View File

@ -73,6 +73,7 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa
*/ */
public function offsetSet($offset, $value) : void public function offsetSet($offset, $value) : void
{ {
/** @var float|int $value */
if ($offset === null) { if ($offset === null) {
$this->result[] = $value; $this->result[] = $value;
} else { } else {

View File

@ -101,6 +101,7 @@ abstract class NgramParser
} }
} }
/** @var array $tokens */
foreach ($tokens as $i => $token) { foreach ($tokens as $i => $token) {
$sum = \array_sum($token); $sum = \array_sum($token);

View File

@ -697,11 +697,11 @@ class Matrix implements \ArrayAccess, \Iterator
* *
* @param self $B Matrix * @param self $B Matrix
* *
* @return self * @return int|float|self
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function dot(self $B) : self public function dot(self $B) : int|float|self
{ {
$value1 = $this->matrix; $value1 = $this->matrix;
$value2 = $B->getMatrix(); $value2 = $B->getMatrix();
@ -731,6 +731,8 @@ class Matrix implements \ArrayAccess, \Iterator
$result[$i][$c] = $temp; $result[$i][$c] = $temp;
} }
} }
return self::fromArray($result);
} elseif (!$isMatrix1 && !$isMatrix2) { } elseif (!$isMatrix1 && !$isMatrix2) {
if ($m1 !== $m2) { if ($m1 !== $m2) {
throw new InvalidDimensionException($m1 . 'x' . $m2); throw new InvalidDimensionException($m1 . 'x' . $m2);
@ -742,6 +744,8 @@ class Matrix implements \ArrayAccess, \Iterator
/** @var array $value2 */ /** @var array $value2 */
$result += $value1[$i] * $value2[$i]; $result += $value1[$i] * $value2[$i];
} }
return $result;
} elseif ($isMatrix1 && !$isMatrix2) { } elseif ($isMatrix1 && !$isMatrix2) {
$result = []; $result = [];
for ($i = 0; $i < $m1; ++$i) { // Row of 1 for ($i = 0; $i < $m1; ++$i) { // Row of 1
@ -754,11 +758,11 @@ class Matrix implements \ArrayAccess, \Iterator
$result[$i] = $temp; $result[$i] = $temp;
} }
} else {
throw new \InvalidArgumentException(); return self::fromArray($result);
} }
return self::fromArray($result); throw new \InvalidArgumentException();
} }
/** /**

View File

@ -101,7 +101,7 @@ final class Vector extends Matrix
{ {
$dotProduct = 0.0; $dotProduct = 0.0;
for ($i = 0; $i < $this->m; ++$i) { for ($i = 0; $i < $this->m; ++$i) {
$dotProduct += $this->matrix[$i][0] * $v[$i]; $dotProduct += $this->matrix[$i][0] * $v[$i][0];
} }
$sumOfSquares = 0; $sumOfSquares = 0;
@ -116,7 +116,7 @@ final class Vector extends Matrix
} }
$magnitude2 = \sqrt($sumOfSquares); $magnitude2 = \sqrt($sumOfSquares);
if ($magnitude1 === 0 || $magnitude2 === 0) { if ($magnitude1 === 0.0 || $magnitude2 === 0.0) {
return \PHP_FLOAT_MAX; return \PHP_FLOAT_MAX;
} }

View File

@ -583,7 +583,7 @@ class Graph
while (!empty($stack)) { while (!empty($stack)) {
$cNode = \array_pop($stack); $cNode = \array_pop($stack);
if (isset($visited[$cNode->getId()]) && $visited[$cNode->getId()]) { if (isset($visited[$cNode->getId()])) {
continue; continue;
} }
@ -592,7 +592,7 @@ class Graph
$neighbors = $cNode->getNeighbors(); $neighbors = $cNode->getNeighbors();
foreach ($neighbors as $neighbor) { foreach ($neighbors as $neighbor) {
if (!isset($visited[$neighbor->getId()]) || !$visited[$neighbor->getId()]) { if (!isset($visited[$neighbor->getId()])) {
$stack[] = $neighbor; $stack[] = $neighbor;
} }
} }

View File

@ -311,17 +311,21 @@ final class ArrayUtils
*/ */
public static function getArg(string $id, array $args) : mixed public static function getArg(string $id, array $args) : mixed
{ {
$key = 0;
if (\is_numeric($id)) { if (\is_numeric($id)) {
return $args[(int) $id] ?? null; $key = ((int) $id) - 1;
} else {
if (($key = \array_search($id, $args)) === false || $key === \count($args) - 1) {
return null;
}
$key = (int) $key;
$args = \array_values($args);
} }
if (($key = \array_search($id, $args)) === false || $key === \count($args) - 1) { $value = $args[$key + 1] ?? null;
return null;
}
$value = $args[(int) $key + 1]; return \is_string($value) ? \trim($value, '\'" ') : $value;
return \is_string($value) ? \trim($value, '" ') : $value;
} }
/** /**
@ -336,7 +340,6 @@ final class ArrayUtils
*/ */
public static function hasArg(string $id, array $args) : int public static function hasArg(string $id, array $args) : int
{ {
$t = \array_search($id, $args);
return ($key = \array_search($id, $args)) === false return ($key = \array_search($id, $args)) === false
? -1 ? -1
: (int) $key; : (int) $key;

View File

@ -613,7 +613,8 @@ class Datamatrix extends TwoDAbstract
if ($this->isCharMode($tmpchr, self::ENC_X12)) { if ($this->isCharMode($tmpchr, self::ENC_X12)) {
return self::ENC_X12; return self::ENC_X12;
} elseif (!$this->isCharMode($tmpchr, self::ENC_X12) && !$this->isCharMode($tmpchr, self::ENC_C40) } elseif (!$this->isCharMode($tmpchr, self::ENC_X12)
&& !$this->isCharMode($tmpchr, self::ENC_C40)
) { ) {
break; break;
} }

View File

@ -48,7 +48,8 @@ final class KmeansTest extends \PHPUnit\Framework\TestCase
new Point([3.5, 4.5], '7'), new Point([3.5, 4.5], '7'),
]; ];
$kmeans = new Kmeans($points, 2); $kmeans = new Kmeans();
$kmeans->generateClusters($points, 2);
if ($kmeans->cluster($points[0])->group === 0 if ($kmeans->cluster($points[0])->group === 0
&& $kmeans->cluster($points[1])->group === 0 && $kmeans->cluster($points[1])->group === 0