diff --git a/Algorithm/Clustering/DBSCAN.php b/Algorithm/Clustering/DBSCAN.php index 09f011ef9..5596e2e7c 100644 --- a/Algorithm/Clustering/DBSCAN.php +++ b/Algorithm/Clustering/DBSCAN.php @@ -68,7 +68,7 @@ final class DBSCAN * * Array of points assigned to a cluster * - * @var array + * @var array * @since 1.0.0 */ private array $clusters = []; @@ -232,6 +232,7 @@ final class DBSCAN $points[] = $p->coordinates; } + // @todo: this is only good for 2D. Fix this for ND. $this->convexHulls[$c] = MonotoneChain::createConvexHull($points); } } diff --git a/DataStorage/Session/JWT.php b/DataStorage/Session/JWT.php index 283651e03..9a6eb1694 100644 --- a/DataStorage/Session/JWT.php +++ b/DataStorage/Session/JWT.php @@ -106,7 +106,9 @@ final class JWT 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 \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 { $header = \json_decode(Base64Url::decode($explode[0]), true); $payload = \json_decode(Base64Url::decode($explode[1]), true); + + if (!\is_array($header) || !\is_array($payload)) { + return false; + } + $signature = self::createSignature($secret, $header, $payload); return \hash_equals($signature, $explode[2]); diff --git a/Localization/LanguageDetection/LanguageResult.php b/Localization/LanguageDetection/LanguageResult.php index 6b76f4067..c1bbce596 100755 --- a/Localization/LanguageDetection/LanguageResult.php +++ b/Localization/LanguageDetection/LanguageResult.php @@ -73,6 +73,7 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa */ public function offsetSet($offset, $value) : void { + /** @var float|int $value */ if ($offset === null) { $this->result[] = $value; } else { diff --git a/Localization/LanguageDetection/NgramParser.php b/Localization/LanguageDetection/NgramParser.php index d6a9f1eca..f8002150d 100755 --- a/Localization/LanguageDetection/NgramParser.php +++ b/Localization/LanguageDetection/NgramParser.php @@ -101,6 +101,7 @@ abstract class NgramParser } } + /** @var array $tokens */ foreach ($tokens as $i => $token) { $sum = \array_sum($token); diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 2a7cca7b4..e678a0cf8 100755 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -697,11 +697,11 @@ class Matrix implements \ArrayAccess, \Iterator * * @param self $B Matrix * - * @return self + * @return int|float|self * * @since 1.0.0 */ - public function dot(self $B) : self + public function dot(self $B) : int|float|self { $value1 = $this->matrix; $value2 = $B->getMatrix(); @@ -731,6 +731,8 @@ class Matrix implements \ArrayAccess, \Iterator $result[$i][$c] = $temp; } } + + return self::fromArray($result); } elseif (!$isMatrix1 && !$isMatrix2) { if ($m1 !== $m2) { throw new InvalidDimensionException($m1 . 'x' . $m2); @@ -742,6 +744,8 @@ class Matrix implements \ArrayAccess, \Iterator /** @var array $value2 */ $result += $value1[$i] * $value2[$i]; } + + return $result; } elseif ($isMatrix1 && !$isMatrix2) { $result = []; for ($i = 0; $i < $m1; ++$i) { // Row of 1 @@ -754,11 +758,11 @@ class Matrix implements \ArrayAccess, \Iterator $result[$i] = $temp; } - } else { - throw new \InvalidArgumentException(); + + return self::fromArray($result); } - return self::fromArray($result); + throw new \InvalidArgumentException(); } /** diff --git a/Math/Matrix/Vector.php b/Math/Matrix/Vector.php index 662b1f614..46fb214f5 100755 --- a/Math/Matrix/Vector.php +++ b/Math/Matrix/Vector.php @@ -101,7 +101,7 @@ final class Vector extends Matrix { $dotProduct = 0.0; for ($i = 0; $i < $this->m; ++$i) { - $dotProduct += $this->matrix[$i][0] * $v[$i]; + $dotProduct += $this->matrix[$i][0] * $v[$i][0]; } $sumOfSquares = 0; @@ -116,7 +116,7 @@ final class Vector extends Matrix } $magnitude2 = \sqrt($sumOfSquares); - if ($magnitude1 === 0 || $magnitude2 === 0) { + if ($magnitude1 === 0.0 || $magnitude2 === 0.0) { return \PHP_FLOAT_MAX; } diff --git a/Stdlib/Graph/Graph.php b/Stdlib/Graph/Graph.php index 137192abc..2d6f0fffd 100755 --- a/Stdlib/Graph/Graph.php +++ b/Stdlib/Graph/Graph.php @@ -583,7 +583,7 @@ class Graph while (!empty($stack)) { $cNode = \array_pop($stack); - if (isset($visited[$cNode->getId()]) && $visited[$cNode->getId()]) { + if (isset($visited[$cNode->getId()])) { continue; } @@ -592,7 +592,7 @@ class Graph $neighbors = $cNode->getNeighbors(); foreach ($neighbors as $neighbor) { - if (!isset($visited[$neighbor->getId()]) || !$visited[$neighbor->getId()]) { + if (!isset($visited[$neighbor->getId()])) { $stack[] = $neighbor; } } diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index e704f4b2a..6ed578f26 100755 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -311,17 +311,21 @@ final class ArrayUtils */ public static function getArg(string $id, array $args) : mixed { + $key = 0; 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) { - return null; - } + $value = $args[$key + 1] ?? 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 { - $t = \array_search($id, $args); return ($key = \array_search($id, $args)) === false ? -1 : (int) $key; diff --git a/Utils/Barcode/Datamatrix.php b/Utils/Barcode/Datamatrix.php index 101505d0b..c9c8c2cbd 100755 --- a/Utils/Barcode/Datamatrix.php +++ b/Utils/Barcode/Datamatrix.php @@ -613,7 +613,8 @@ class Datamatrix extends TwoDAbstract if ($this->isCharMode($tmpchr, 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; } diff --git a/tests/Algorithm/Clustering/KmeansTest.php b/tests/Algorithm/Clustering/KmeansTest.php index d2743815a..5a2a30a40 100755 --- a/tests/Algorithm/Clustering/KmeansTest.php +++ b/tests/Algorithm/Clustering/KmeansTest.php @@ -48,7 +48,8 @@ final class KmeansTest extends \PHPUnit\Framework\TestCase 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 && $kmeans->cluster($points[1])->group === 0