Skip to content

Commit

Permalink
refactoring with Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Oct 28, 2023
1 parent fecf340 commit 6e615f6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
SetList::CODE_QUALITY,
SetList::EARLY_RETURN,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION
// SetList::PRIVATIZATION
]);
};
2 changes: 1 addition & 1 deletion src/ArrUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 12 additions & 18 deletions src/Freq.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -48,13 +48,7 @@ public static function frequencies(array $data, bool $transformToInteger = false
*/
public static function cumulativeFrequencies(array $data): array
{
/**
* @var array<mixed, int> array of cumulative frequencies
*/
$freqCumul = [];
/**
* @var int cumulative frequency
*/
$cumul = 0;
$freqs = self::frequencies($data);
foreach ($freqs as $key => $value) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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)
&&
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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)
&&
Expand All @@ -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]);
}
Expand Down
38 changes: 18 additions & 20 deletions src/Stat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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];
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.'
);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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.'
);
Expand All @@ -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;
Expand Down Expand Up @@ -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.'
);
Expand Down
10 changes: 2 additions & 8 deletions src/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class Statistics

/**
* Whether array contains not a numbers
*
* @var bool
*/
private ?bool $containsNan = null;

Expand All @@ -43,9 +41,7 @@ public function __construct(
*/
public static function make(array $values): self
{
$freqTable = new self($values);

return $freqTable;
return new self($values);
}

/**
Expand Down Expand Up @@ -309,9 +305,7 @@ public function numericalArray(): array
if ($this->containsNan) {
throw new InvalidDataInputException('The data must not contain non-number elements.');
}
/** @var array<int|float> */
$numericalArray = $this->values;

return $numericalArray;
return $this->values;
}
}

0 comments on commit 6e615f6

Please sign in to comment.