Skip to content

Commit

Permalink
Replace pow function with the ** operator
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Sep 27, 2021
1 parent e9ec9e8 commit 7955f7d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 43 deletions.
3 changes: 1 addition & 2 deletions libraries/classes/Advisor.php
Expand Up @@ -12,7 +12,6 @@
use function array_merge;
use function htmlspecialchars;
use function implode;
use function pow;
use function preg_match;
use function preg_replace_callback;
use function round;
Expand Down Expand Up @@ -426,7 +425,7 @@ public static function byTime(float $num, int $precision): string
$num = round($num, $precision);

if ($num == 0) {
$num = '<' . pow(10, -$precision);
$num = '<' . 10 ** (-$precision);
}

return $num . ' ' . $per;
Expand Down
Expand Up @@ -17,7 +17,6 @@
use function implode;
use function is_numeric;
use function mb_strtolower;
use function pow;
use function preg_match;
use function trim;

Expand Down Expand Up @@ -63,10 +62,7 @@ public function __invoke(ServerRequest $request, array $vars): void
'gb' => 3,
'gib' => 3,
];
$value = (float) $matches[1] * pow(
1024,
$exp[mb_strtolower($matches[3])]
);
$value = (float) $matches[1] * 1024 ** $exp[mb_strtolower($matches[3])];
} else {
$value = $this->dbi->escapeString($value);
}
Expand Down
5 changes: 2 additions & 3 deletions libraries/classes/Gis/GisPolygon.php
Expand Up @@ -20,7 +20,6 @@
use function max;
use function mb_substr;
use function min;
use function pow;
use function round;
use function sqrt;
use function str_contains;
Expand Down Expand Up @@ -521,7 +520,7 @@ public static function getPointOnSurface(array $ring)

// Always keep $epsilon < 1 to go with the reduction logic down here
$epsilon = 0.1;
$denominator = sqrt(pow($y1 - $y0, 2) + pow($x0 - $x1, 2));
$denominator = sqrt(($y1 - $y0) ** 2 + ($x0 - $x1) ** 2);
$pointA = [];
$pointB = [];

Expand All @@ -546,7 +545,7 @@ public static function getPointOnSurface(array $ring)

//If both are outside the polygon reduce the epsilon and
//recalculate the points(reduce exponentially for faster convergence)
$epsilon = pow($epsilon, 2);
$epsilon **= 2;
if ($epsilon == 0) {
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions libraries/classes/Import.php
Expand Up @@ -29,7 +29,6 @@
use function mb_strtoupper;
use function mb_substr;
use function mb_substr_count;
use function pow;
use function preg_match;
use function preg_replace;
use function sprintf;
Expand Down Expand Up @@ -554,10 +553,10 @@ public function getColumnNumberFromName(string $name): int
// base26 to base10 conversion : multiply each number
// with corresponding value of the position, in this case
// $i=0 : 1; $i=1 : 26; $i=2 : 676; ...
$columnNumber += $number * pow(26, $i);
$columnNumber += $number * 26 ** $i;
}

return $columnNumber;
return (int) $columnNumber;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions libraries/classes/IpAllowDeny.php
Expand Up @@ -20,7 +20,6 @@
use function mb_strtolower;
use function mb_substr;
use function min;
use function pow;
use function preg_match;
use function str_replace;
use function substr_replace;
Expand Down Expand Up @@ -85,7 +84,7 @@ public function ipv4MaskTest($testRange, $ipToTest): bool
continue;
}

$maskl += pow(2, 30 - $i);
$maskl += 2 ** (30 - $i);
}

return ($maskl & $rangel) == ($maskl & $ipl);
Expand Down Expand Up @@ -196,7 +195,7 @@ public function ipv6MaskTest($test_range, $ip_to_test): bool
$origval = hexdec($orig);

// OR it with (2^flexbits)-1, with flexbits limited to 4 at a time
$newval = $origval | pow(2, min(4, $flexbits)) - 1;
$newval = $origval | 2 ** min(4, $flexbits) - 1;

// Convert it back to a hexadecimal character
$new = dechex($newval);
Expand Down
31 changes: 15 additions & 16 deletions libraries/classes/Util.php
Expand Up @@ -59,7 +59,6 @@
use function number_format;
use function ord;
use function parse_url;
use function pow;
use function preg_match;
use function preg_quote;
use function preg_replace;
Expand Down Expand Up @@ -501,15 +500,15 @@ public static function formatByteDown($value, $limes = 6, $comma = 0): ?array
__('EiB'),
];

$dh = pow(10, $comma);
$li = pow(10, $limes);
$dh = 10 ** $comma;
$li = 10 ** $limes;
$unit = $byteUnits[0];

for ($d = 6, $ex = 15; $d >= 1; $d--, $ex -= 3) {
$unitSize = $li * pow(10, $ex);
$unitSize = $li * 10 ** $ex;
if (isset($byteUnits[$d]) && $value >= $unitSize) {
// use 1024.0 to avoid integer overflow on 64-bit machines
$value = round($value / (pow(1024, $d) / $dh)) / $dh;
$value = round($value / (1024 ** $d / $dh)) / $dh;
$unit = $byteUnits[$d];
break 1;
}
Expand Down Expand Up @@ -583,7 +582,7 @@ public static function formatNumber(
__(',')
);
if (($originalValue != 0) && (floatval($value) == 0)) {
$value = ' <' . (1 / pow(10, $digitsRight));
$value = ' <' . (1 / 10 ** $digitsRight);
}

return $value;
Expand Down Expand Up @@ -622,7 +621,7 @@ public static function formatNumber(
$sign = '';
}

$dh = pow(10, $digitsRight);
$dh = 10 ** $digitsRight;

/*
* This gives us the right SI prefix already,
Expand All @@ -634,7 +633,7 @@ public static function formatNumber(
* So if we have 3,6,9,12.. free digits ($digits_left - $cur_digits)
* to use, then lower the SI prefix
*/
$curDigits = floor(log10($value / pow(1000, $d)) + 1);
$curDigits = floor(log10($value / 1000 ** $d) + 1);
if ($digitsLeft > $curDigits) {
$d -= floor(($digitsLeft - $curDigits) / 3);
}
Expand All @@ -643,7 +642,7 @@ public static function formatNumber(
$d = 0;
}

$value = round($value / (pow(1000, $d) / $dh)) / $dh;
$value = round($value / (1000 ** $d / $dh)) / $dh;
$unit = $units[$d];

// number_format is not multibyte safe, str_replace is safe
Expand All @@ -655,7 +654,7 @@ public static function formatNumber(

if ($originalValue != 0 && floatval($value) == 0) {
return ' <' . number_format(
1 / pow(10, $digitsRight),
1 / 10 ** $digitsRight,
$digitsRight,
$decimalSep,
$thousandsSep
Expand All @@ -680,11 +679,11 @@ public static function extractValueFromFormattedSize($formattedSize)
$formattedSize = (string) $formattedSize;

if (preg_match('/^[0-9]+GB$/', $formattedSize)) {
$returnValue = (int) mb_substr($formattedSize, 0, -2) * pow(1024, 3);
$returnValue = (int) mb_substr($formattedSize, 0, -2) * 1024 ** 3;
} elseif (preg_match('/^[0-9]+MB$/', $formattedSize)) {
$returnValue = (int) mb_substr($formattedSize, 0, -2) * pow(1024, 2);
$returnValue = (int) mb_substr($formattedSize, 0, -2) * 1024 ** 2;
} elseif (preg_match('/^[0-9]+K$/', $formattedSize)) {
$returnValue = (int) mb_substr($formattedSize, 0, -1) * pow(1024, 1);
$returnValue = (int) mb_substr($formattedSize, 0, -1) * 1024 ** 1;
}

return $returnValue;
Expand Down Expand Up @@ -1338,7 +1337,7 @@ public static function printableBitValue(int $value, int $length): string
// FIXME: does not work for the leftmost bit of a 64-bit value
$i = 0;
$printable = '';
while ($value >= pow(2, $i)) {
while ($value >= 2 ** $i) {
++$i;
}

Expand All @@ -1347,11 +1346,11 @@ public static function printableBitValue(int $value, int $length): string
}

while ($i >= 0) {
if ($value - pow(2, $i) < 0) {
if ($value - 2 ** $i < 0) {
$printable = '0' . $printable;
} else {
$printable = '1' . $printable;
$value -= pow(2, $i);
$value -= 2 ** $i;
}

--$i;
Expand Down
17 changes: 6 additions & 11 deletions psalm-baseline.xml
Expand Up @@ -8015,14 +8015,13 @@
</RedundantPropertyInitializationCheck>
</file>
<file src="libraries/classes/Gis/GisPolygon.php">
<MixedArgument occurrences="7">
<MixedArgument occurrences="6">
<code>$points_arr[2]</code>
<code>$points_arr[2]</code>
<code>$points_arr[3]</code>
<code>$points_arr[3]</code>
<code>$wkt</code>
<code>$x0 - $x1</code>
<code>$y1 - $y0</code>
<code>($y1 - $y0) ** 2 + ($x0 - $x1) ** 2</code>
</MixedArgument>
<MixedArrayAccess occurrences="42">
<code>$gis_data[$index]['POLYGON']</code>
Expand Down Expand Up @@ -8106,7 +8105,7 @@
<MixedInferredReturnType occurrences="1">
<code>float</code>
</MixedInferredReturnType>
<MixedOperand occurrences="34">
<MixedOperand occurrences="35">
<code>$area</code>
<code>$area</code>
<code>$epsilon * ($y1 - $y0)</code>
Expand Down Expand Up @@ -8138,6 +8137,7 @@
<code>$y2</code>
<code>($pointA['x'] - $x2) * ($x0 - $x1)</code>
<code>($pointB['x'] - $x2) * ($x0 - $x1)</code>
<code>($y1 - $y0) ** 2</code>
</MixedOperand>
<MixedReturnStatement occurrences="1">
<code>$area</code>
Expand Down Expand Up @@ -8499,12 +8499,10 @@
</TooFewArguments>
</file>
<file src="libraries/classes/Import.php">
<InvalidReturnStatement occurrences="2">
<code>$columnNumber</code>
<InvalidReturnStatement occurrences="1">
<code>$result</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="2">
<code>int</code>
<InvalidReturnType occurrences="1">
<code>int</code>
</InvalidReturnType>
<MixedArgument occurrences="67">
Expand Down Expand Up @@ -9199,9 +9197,6 @@
<InvalidScalarArgument occurrences="1">
<code>$remote_ip</code>
</InvalidScalarArgument>
<PossiblyInvalidArgument occurrences="1">
<code>$newval</code>
</PossiblyInvalidArgument>
<TypeDoesNotContainType occurrences="1">
<code>is_array($rules)</code>
</TypeDoesNotContainType>
Expand Down

0 comments on commit 7955f7d

Please sign in to comment.