From 6e615f6d6aad9ab1cc923de281965b713c1b4747 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Sat, 28 Oct 2023 17:59:03 +0200 Subject: [PATCH] refactoring with Rector --- composer.json | 2 ++ rector.php | 2 +- src/ArrUtil.php | 2 +- src/Freq.php | 30 ++++++++++++------------------ src/Stat.php | 38 ++++++++++++++++++-------------------- src/Statistics.php | 10 ++-------- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/composer.json b/composer.json index 47d58fd..af07cf3 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,8 @@ "test": "vendor/bin/pest", "test-coverage": "vendor/bin/pest --coverage", "static-code": "vendor/bin/phpstan analyse -c phpstan.neon", + "rector-dry-run": "rector process --dry-run", + "rector": "rector process", "all-check": [ "@format", "@test", diff --git a/rector.php b/rector.php index 5b0f01b..dd04de2 100644 --- a/rector.php +++ b/rector.php @@ -24,6 +24,6 @@ SetList::CODE_QUALITY, SetList::EARLY_RETURN, SetList::TYPE_DECLARATION, - SetList::PRIVATIZATION + // SetList::PRIVATIZATION ]); }; diff --git a/src/ArrUtil.php b/src/ArrUtil.php index 549ad1f..0cae6f5 100644 --- a/src/ArrUtil.php +++ b/src/ArrUtil.php @@ -12,7 +12,7 @@ class ArrUtil public static function toString(array $data, bool|int $sample = false): string { if ($sample) { - return implode(',', array_slice($data, 0, intval($sample))); + return implode(',', array_slice($data, 0, (int) $sample)); } return implode(',', $data); diff --git a/src/Freq.php b/src/Freq.php index a273fe1..8c81945 100644 --- a/src/Freq.php +++ b/src/Freq.php @@ -24,12 +24,12 @@ private static function isDiscreteType(mixed $value): bool */ public static function frequencies(array $data, bool $transformToInteger = false): array { - if (! Stat::count($data)) { + if (Stat::count($data) === 0) { return []; } if ($transformToInteger || ! self::isDiscreteType($data[0])) { foreach ($data as $key => $value) { - $data[$key] = intval($value); + $data[$key] = (int) $value; } } $frequencies = array_count_values($data); @@ -48,13 +48,7 @@ public static function frequencies(array $data, bool $transformToInteger = false */ public static function cumulativeFrequencies(array $data): array { - /** - * @var array array of cumulative frequencies - */ $freqCumul = []; - /** - * @var int cumulative frequency - */ $cumul = 0; $freqs = self::frequencies($data); foreach ($freqs as $key => $value) { @@ -99,7 +93,7 @@ public static function cumulativeRelativeFrequencies(array $data): array $cumul = 0; $relFreqs = self::relativeFrequencies($data); foreach ($relFreqs as $key => $value) { - $cumul = $cumul + $value; + $cumul += $value; $freqCumul[$key] = $cumul; } @@ -113,8 +107,8 @@ public static function cumulativeRelativeFrequencies(array $data): array public static function frequencyTableBySize(array $data, int $chunkSize = 1): array { $result = []; - $min = floor(floatval(min($data))); - $max = ceil(floatval(max($data))); + $min = floor((float) min($data)); + $max = ceil((float) max($data)); //$limit = ceil(($max - $min) / $category); sort($data); @@ -123,7 +117,7 @@ public static function frequencyTableBySize(array $data, int $chunkSize = 1): ar while ($rangeHigh < $max) { $count = 0; $rangeHigh = ($rangeLow + $chunkSize); - foreach ($data as $key => $number) { + foreach ($data as $number) { if ( ($number >= $rangeLow) && @@ -133,7 +127,7 @@ public static function frequencyTableBySize(array $data, int $chunkSize = 1): ar //unset($data[$key]); } } - $result[strval($rangeLow)] = $count; + $result[(string) $rangeLow] = $count; $rangeLow = $rangeHigh; } @@ -153,8 +147,8 @@ public static function frequencyTableBySize(array $data, int $chunkSize = 1): ar public static function frequencyTable(array $data, int $category = null): array { $result = []; - $min = floor(floatval(min($data))); - $max = ceil(floatval(max($data))); + $min = floor((float) min($data)); + $max = ceil((float) max($data)); if (is_null($category)) { $category = ($max - $min) + 1; } @@ -165,7 +159,7 @@ public static function frequencyTable(array $data, int $category = null): array for ($i = 0; $i < $category; $i++) { $count = 0; $rangeHigh = $rangeLow + $limit; - foreach ($data as $key => $number) { + foreach ($data as $number) { if ( ($number >= $rangeLow) && @@ -175,12 +169,12 @@ public static function frequencyTable(array $data, int $category = null): array //unset($data[$key]); } } - $result[strval($rangeLow)] = $count; + $result[(string) $rangeLow] = $count; $rangeLow = $rangeHigh; } // eliminate - foreach ($result as $key => $item) { + foreach (array_keys($result) as $key) { if ($key > max($data)) { unset($result[$key]); } diff --git a/src/Stat.php b/src/Stat.php index 2c81cc8..214bec0 100644 --- a/src/Stat.php +++ b/src/Stat.php @@ -6,11 +6,11 @@ class Stat { - public const MEDIAN_TYPE_LOW = 'LOW'; + final public const MEDIAN_TYPE_LOW = 'LOW'; - public const MEDIAN_TYPE_HIGH = 'HIGH'; + final public const MEDIAN_TYPE_HIGH = 'HIGH'; - public const MEDIAN_TYPE_MIDDLE = 'MIDDLE'; + final public const MEDIAN_TYPE_MIDDLE = 'MIDDLE'; /** * Count the element in the array @@ -38,7 +38,7 @@ public static function count(array $data): int public static function mean(array $data): int|float|null { $sum = 0; - if (! self::count($data)) { + if (self::count($data) === 0) { throw new InvalidDataInputException('The data must not be empty.'); } $sum = array_sum($data); @@ -57,11 +57,11 @@ public static function mean(array $data): int|float|null public static function median(array $data, string $medianType = self::MEDIAN_TYPE_MIDDLE): mixed { $count = self::count($data); - if (! $count) { + if ($count === 0) { throw new InvalidDataInputException('The data must not be empty.'); } $index = floor($count / 2); // cache the index - if ($count & 1) { // count is odd + if (($count & 1) !== 0) { // count is odd return $data[$index]; } @@ -125,7 +125,7 @@ public static function mode(array $data, bool $multimode = false): mixed throw new InvalidDataInputException('The data must not be empty.'); } $sameMode = true; - foreach ($frequencies as $key => $value) { + foreach ($frequencies as $value) { if ($value > 1) { $sameMode = false; @@ -155,10 +155,7 @@ public static function mode(array $data, bool $multimode = false): mixed */ public static function multimode(array $data): array|null { - /** @var mixed[]|null */ - $mode = self::mode($data, true); - - return $mode; + return self::mode($data, true); } /** @@ -249,7 +246,7 @@ public static function pstdev(array $data, ?int $round = null): float { $variance = self::pvariance($data); - return (float) Math::round(sqrt($variance), $round); + return Math::round(sqrt($variance), $round); } /** @@ -293,7 +290,7 @@ public static function stdev(array $data, int $round = null): float { $variance = self::variance($data); - return (float) Math::round(sqrt($variance), $round); + return Math::round(sqrt($variance), $round); } /** @@ -337,7 +334,7 @@ public static function variance(array $data, ?int $round = null): float public static function geometricMean(array $data, ?int $round = null): float { $count = self::count($data); - if (! $count) { + if ($count === 0) { throw new InvalidDataInputException('The data must not be empty.'); } $product = 1; @@ -363,7 +360,7 @@ public static function harmonicMean(array $data, ?array $weights = null, ?int $r { $sum = 0; $count = self::count($data); - if (! $count) { + if ($count === 0) { throw new InvalidDataInputException('The data must not be empty.'); } $sumWeigth = 0; @@ -393,7 +390,7 @@ public static function covariance(array $x, array $y): false|float { $countX = count($x); $countY = count($y); - if ($countX != $countY) { + if ($countX !== $countY) { throw new InvalidDataInputException( 'Covariance requires that both inputs have same number of data points.' ); @@ -426,7 +423,7 @@ public static function covariance(array $x, array $y): false|float } // covariance for sample: N - 1 - return $add / floatval($countX - 1); + return $add / (float) ($countX - 1); } /** @@ -448,7 +445,7 @@ public static function correlation(array $x, array $y): false|float { $countX = count($x); $countY = count($y); - if ($countX != $countY) { + if ($countX !== $countY) { throw new InvalidDataInputException( 'Correlation requires that both inputs have same number of data points.' ); @@ -463,7 +460,8 @@ public static function correlation(array $x, array $y): false|float $a = 0; $bx = 0; $by = 0; - for ($i = 0; $i < count($x); $i++) { + $counter = count($x); + for ($i = 0; $i < $counter; $i++) { $xr = $x[$i] - $meanX; $yr = $y[$i] - $meanY; $a += $xr * $yr; @@ -493,7 +491,7 @@ public static function linearRegression(array $x, array $y): array { $countX = count($x); $countY = count($y); - if ($countX != $countY) { + if ($countX !== $countY) { throw new InvalidDataInputException( 'Linear regression requires that both inputs have same number of data points.' ); diff --git a/src/Statistics.php b/src/Statistics.php index 4af91c6..47d0c4a 100755 --- a/src/Statistics.php +++ b/src/Statistics.php @@ -22,8 +22,6 @@ class Statistics /** * Whether array contains not a numbers - * - * @var bool */ private ?bool $containsNan = null; @@ -43,9 +41,7 @@ public function __construct( */ public static function make(array $values): self { - $freqTable = new self($values); - - return $freqTable; + return new self($values); } /** @@ -309,9 +305,7 @@ public function numericalArray(): array if ($this->containsNan) { throw new InvalidDataInputException('The data must not contain non-number elements.'); } - /** @var array */ - $numericalArray = $this->values; - return $numericalArray; + return $this->values; } }