PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
← All changes | includes/sdk/google/brick/math/src/Internal/Calculator.php +65 -148 1.2.131.4.1 View file →
@@ -1,11 +1,11 @@
1 1 <?php
2 2
3 3 declare (strict_types=1);
4 -namespace Dudlewebs\WPMCS\Brick\Math\Internal;
4 +namespace Dudlewebs\WPMCS\GCP\Brick\Math\Internal;
5 5
6 -use Dudlewebs\WPMCS\Brick\Math\Exception\RoundingNecessaryException;
7 -use Dudlewebs\WPMCS\Brick\Math\RoundingMode;
6 +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\RoundingNecessaryException;
7 +use Dudlewebs\WPMCS\GCP\Brick\Math\RoundingMode;
8 8 /**
9 9 * Performs basic operations on arbitrary size integers.
10 10 *
11 11 * Unless otherwise specified, all parameters must be validated as non-empty strings of digits,
@@ -29,12 +29,10 @@
29 29 */
30 30 public const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
31 31 /**
32 32 * The Calculator instance in use.
33 - *
34 - * @var Calculator|null
35 33 */
36 - private static $instance;
34 + private static ?Calculator $instance = null;
37 35 /**
38 36 * Sets the Calculator instance to use.
39 37 *
40 38 * An instance is typically set only in unit tests: the autodetect is usually the best option.
@@ -39,12 +37,10 @@
39 37 *
40 38 * An instance is typically set only in unit tests: the autodetect is usually the best option.
41 39 *
42 40 * @param Calculator|null $calculator The calculator instance, or NULL to revert to autodetect.
43 - *
44 - * @return void
45 41 */
46 - final public static function set(?Calculator $calculator): void
42 + public static final function set(?Calculator $calculator) : void
47 43 {
48 44 self::$instance = $calculator;
49 45 }
50 46 /**
@@ -51,14 +47,12 @@
51 47 * Returns the Calculator instance to use.
52 48 *
53 49 * If none has been explicitly set, the fastest available implementation will be returned.
54 50 *
55 - * @return Calculator
56 - *
57 51 * @psalm-pure
58 52 * @psalm-suppress ImpureStaticProperty
59 53 */
60 - final public static function get(): Calculator
54 + public static final function get() : Calculator
61 55 {
62 56 if (self::$instance === null) {
63 57 /** @psalm-suppress ImpureMethodCall */
64 58 self::$instance = self::detect();
@@ -68,12 +62,10 @@
68 62 /**
69 63 * Returns the fastest available Calculator implementation.
70 64 *
71 65 * @codeCoverageIgnore
72 - *
73 - * @return Calculator
74 66 */
75 - private static function detect(): Calculator
67 + private static function detect() : Calculator
76 68 {
77 69 if (\extension_loaded('gmp')) {
78 70 return new Calculator\GmpCalculator();
79 71 }
@@ -84,36 +76,25 @@
84 76 }
85 77 /**
86 78 * Extracts the sign & digits of the operands.
87 79 *
88 - * @param string $a The first operand.
89 - * @param string $b The second operand.
90 - *
91 80 * @return array{bool, bool, string, string} Whether $a and $b are negative, followed by their digits.
92 81 */
93 - final protected function init(string $a, string $b): array
82 + protected final function init(string $a, string $b) : array
94 83 {
95 84 return [$aNeg = $a[0] === '-', $bNeg = $b[0] === '-', $aNeg ? \substr($a, 1) : $a, $bNeg ? \substr($b, 1) : $b];
96 85 }
97 86 /**
98 87 * Returns the absolute value of a number.
99 - *
100 - * @param string $n The number.
101 - *
102 - * @return string The absolute value.
103 88 */
104 - final public function abs(string $n): string
89 + public final function abs(string $n) : string
105 90 {
106 91 return $n[0] === '-' ? \substr($n, 1) : $n;
107 92 }
108 93 /**
109 94 * Negates a number.
110 - *
111 - * @param string $n The number.
112 - *
113 - * @return string The negated value.
114 95 */
115 - final public function neg(string $n): string
96 + public final function neg(string $n) : string
116 97 {
117 98 if ($n === '0') {
118 99 return '0';
119 100 }
@@ -124,14 +105,13 @@
124 105 }
125 106 /**
126 107 * Compares two numbers.
127 108 *
128 - * @param string $a The first number.
129 - * @param string $b The second number.
109 + * @psalm-return -1|0|1
130 110 *
131 - * @return int [-1, 0, 1] If the first number is less than, equal to, or greater than the second number.
111 + * @return int -1 if the first number is less than, 0 if equal to, 1 if greater than the second number.
132 112 */
133 - final public function cmp(string $a, string $b): int
113 + public final function cmp(string $a, string $b) : int
134 114 {
135 115 [$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
136 116 if ($aNeg && !$bNeg) {
137 117 return -1;
@@ -151,33 +131,18 @@
151 131 return $aNeg ? -$result : $result;
152 132 }
153 133 /**
154 134 * Adds two numbers.
155 - *
156 - * @param string $a The augend.
157 - * @param string $b The addend.
158 - *
159 - * @return string The sum.
160 135 */
161 - abstract public function add(string $a, string $b): string;
136 + public abstract function add(string $a, string $b) : string;
162 137 /**
163 138 * Subtracts two numbers.
164 - *
165 - * @param string $a The minuend.
166 - * @param string $b The subtrahend.
167 - *
168 - * @return string The difference.
169 139 */
170 - abstract public function sub(string $a, string $b): string;
140 + public abstract function sub(string $a, string $b) : string;
171 141 /**
172 142 * Multiplies two numbers.
173 - *
174 - * @param string $a The multiplicand.
175 - * @param string $b The multiplier.
176 - *
177 - * @return string The product.
178 143 */
179 - abstract public function mul(string $a, string $b): string;
144 + public abstract function mul(string $a, string $b) : string;
180 145 /**
181 146 * Returns the quotient of the division of two numbers.
182 147 *
183 148 * @param string $a The dividend.
@@ -184,9 +149,9 @@
184 149 * @param string $b The divisor, must not be zero.
185 150 *
186 151 * @return string The quotient.
187 152 */
188 - abstract public function divQ(string $a, string $b): string;
153 + public abstract function divQ(string $a, string $b) : string;
189 154 /**
190 155 * Returns the remainder of the division of two numbers.
191 156 *
192 157 * @param string $a The dividend.
@@ -193,9 +158,9 @@
193 158 * @param string $b The divisor, must not be zero.
194 159 *
195 160 * @return string The remainder.
196 161 */
197 - abstract public function divR(string $a, string $b): string;
162 + public abstract function divR(string $a, string $b) : string;
198 163 /**
199 164 * Returns the quotient and remainder of the division of two numbers.
200 165 *
201 166 * @param string $a The dividend.
@@ -200,11 +165,11 @@
200 165 *
201 166 * @param string $a The dividend.
202 167 * @param string $b The divisor, must not be zero.
203 168 *
204 - * @return string[] An array containing the quotient and remainder.
169 + * @return array{string, string} An array containing the quotient and remainder.
205 170 */
206 - abstract public function divQR(string $a, string $b): array;
171 + public abstract function divQR(string $a, string $b) : array;
207 172 /**
208 173 * Exponentiates a number.
209 174 *
210 175 * @param string $a The base number.
@@ -211,16 +176,13 @@
211 176 * @param int $e The exponent, validated as an integer between 0 and MAX_POWER.
212 177 *
213 178 * @return string The power.
214 179 */
215 - abstract public function pow(string $a, int $e): string;
180 + public abstract function pow(string $a, int $e) : string;
216 181 /**
217 - * @param string $a
218 182 * @param string $b The modulus; must not be zero.
219 - *
220 - * @return string
221 183 */
222 - public function mod(string $a, string $b): string
184 + public function mod(string $a, string $b) : string
223 185 {
224 186 return $this->divR($this->add($this->divR($a, $b), $b), $b);
225 187 }
226 188 /**
@@ -229,14 +191,11 @@
229 191 * If $x has no multiplicative inverse mod m, this method must return null.
230 192 *
231 193 * This method can be overridden by the concrete implementation if the underlying library has built-in support.
232 194 *
233 - * @param string $x
234 195 * @param string $m The modulus; must not be negative or zero.
235 - *
236 - * @return string|null
237 196 */
238 - public function modInverse(string $x, string $m): ?string
197 + public function modInverse(string $x, string $m) : ?string
239 198 {
240 199 if ($m === '1') {
241 200 return '0';
242 201 }
@@ -243,11 +202,9 @@
243 202 $modVal = $x;
244 203 if ($x[0] === '-' || $this->cmp($this->abs($x), $m) >= 0) {
245 204 $modVal = $this->mod($x, $m);
246 205 }
247 - $x = '0';
248 - $y = '0';
249 - $g = $this->gcdExtended($modVal, $m, $x, $y);
206 + [$g, $x] = $this->gcdExtended($modVal, $m);
250 207 if ($g !== '1') {
251 208 return null;
252 209 }
253 210 return $this->mod($this->add($this->mod($x, $m), $m), $m);
@@ -257,12 +214,10 @@
257 214 *
258 215 * @param string $base The base number; must be positive or zero.
259 216 * @param string $exp The exponent; must be positive or zero.
260 217 * @param string $mod The modulus; must be strictly positive.
261 - *
262 - * @return string The power.
263 218 */
264 - abstract public function modPow(string $base, string $exp, string $mod): string;
219 + public abstract function modPow(string $base, string $exp, string $mod) : string;
265 220 /**
266 221 * Returns the greatest common divisor of the two numbers.
267 222 *
268 223 * This method can be overridden by the concrete implementation if the underlying library
@@ -267,14 +222,11 @@
267 222 *
268 223 * This method can be overridden by the concrete implementation if the underlying library
269 224 * has built-in support for GCD calculations.
270 225 *
271 - * @param string $a The first number.
272 - * @param string $b The second number.
273 - *
274 226 * @return string The GCD, always positive, or zero if both arguments are zero.
275 227 */
276 - public function gcd(string $a, string $b): string
228 + public function gcd(string $a, string $b) : string
277 229 {
278 230 if ($a === '0') {
279 231 return $this->abs($b);
280 232 }
@@ -282,21 +234,20 @@
282 234 return $this->abs($a);
283 235 }
284 236 return $this->gcd($b, $this->divR($a, $b));
285 237 }
286 - private function gcdExtended(string $a, string $b, string &$x, string &$y): string
238 + /**
239 + * @return array{string, string, string} GCD, X, Y
240 + */
241 + private function gcdExtended(string $a, string $b) : array
287 242 {
288 243 if ($a === '0') {
289 - $x = '0';
290 - $y = '1';
291 - return $b;
244 + return [$b, '0', '1'];
292 245 }
293 - $x1 = '0';
294 - $y1 = '0';
295 - $gcd = $this->gcdExtended($this->mod($b, $a), $a, $x1, $y1);
246 + [$gcd, $x1, $y1] = $this->gcdExtended($this->mod($b, $a), $a);
296 247 $x = $this->sub($y1, $this->mul($this->divQ($b, $a), $x1));
297 248 $y = $x1;
298 - return $gcd;
249 + return [$gcd, $x, $y];
299 250 }
300 251 /**
301 252 * Returns the square root of the given number, rounded down.
302 253 *
@@ -301,14 +252,10 @@
301 252 * Returns the square root of the given number, rounded down.
302 253 *
303 254 * The result is the largest x such that x² ≤ n.
304 255 * The input MUST NOT be negative.
305 - *
306 - * @param string $n The number.
307 - *
308 - * @return string The square root.
309 256 */
310 - abstract public function sqrt(string $n): string;
257 + public abstract function sqrt(string $n) : string;
311 258 /**
312 259 * Converts a number from an arbitrary base.
313 260 *
314 261 * This method can be overridden by the concrete implementation if the underlying library
@@ -318,9 +265,9 @@
318 265 * @param int $base The base of the number, validated from 2 to 36.
319 266 *
320 267 * @return string The converted number, following the Calculator conventions.
321 268 */
322 - public function fromBase(string $number, int $base): string
269 + public function fromBase(string $number, int $base) : string
323 270 {
324 271 return $this->fromArbitraryBase(\strtolower($number), self::ALPHABET, $base);
325 272 }
326 273 /**
@@ -333,9 +280,9 @@
333 280 * @param int $base The base to convert to, validated from 2 to 36.
334 281 *
335 282 * @return string The converted number, lowercase.
336 283 */
337 - public function toBase(string $number, int $base): string
284 + public function toBase(string $number, int $base) : string
338 285 {
339 286 $negative = $number[0] === '-';
340 287 if ($negative) {
341 288 $number = \substr($number, 1);
@@ -355,9 +302,9 @@
355 302 * @param int $base The base of the number, validated from 2 to alphabet length.
356 303 *
357 304 * @return string The number in base 10, following the Calculator conventions.
358 305 */
359 - final public function fromArbitraryBase(string $number, string $alphabet, int $base): string
306 + public final function fromArbitraryBase(string $number, string $alphabet, int $base) : string
360 307 {
361 308 // remove leading "zeros"
362 309 $number = \ltrim($number, $alphabet[0]);
363 310 if ($number === '') {
@@ -389,9 +336,9 @@
389 336 * @param int $base The base to convert to, validated from 2 to alphabet length.
390 337 *
391 338 * @return string The converted number in the given alphabet.
392 339 */
393 - final public function toArbitraryBase(string $number, string $alphabet, int $base): string
340 + public final function toArbitraryBase(string $number, string $alphabet, int $base) : string
394 341 {
395 342 if ($number === '0') {
396 343 return $alphabet[0];
397 344 }
@@ -408,23 +355,23 @@
408 355 * Performs a rounded division.
409 356 *
410 357 * Rounding is performed when the remainder of the division is not zero.
411 358 *
412 - * @param string $a The dividend.
413 - * @param string $b The divisor, must not be zero.
414 - * @param int $roundingMode The rounding mode.
359 + * @param string $a The dividend.
360 + * @param string $b The divisor, must not be zero.
361 + * @param RoundingMode $roundingMode The rounding mode.
415 362 *
416 - * @return string
417 - *
418 363 * @throws \InvalidArgumentException If the rounding mode is invalid.
419 364 * @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided but rounding is necessary.
365 + *
366 + * @psalm-suppress ImpureFunctionCall
420 367 */
421 - final public function divRound(string $a, string $b, int $roundingMode): string
368 + public final function divRound(string $a, string $b, RoundingMode $roundingMode) : string
422 369 {
423 370 [$quotient, $remainder] = $this->divQR($a, $b);
424 371 $hasDiscardedFraction = $remainder !== '0';
425 372 $isPositiveOrZero = ($a[0] === '-') === ($b[0] === '-');
426 - $discardedFractionSign = function () use ($remainder, $b): int {
373 + $discardedFractionSign = function () use($remainder, $b) : int {
427 374 $r = $this->abs($this->mul($remainder, '2'));
428 375 $b = $this->abs($b);
429 376 return $this->cmp($r, $b);
430 377 };
@@ -475,15 +422,10 @@
475 422 * Calculates bitwise AND of two numbers.
476 423 *
477 424 * This method can be overridden by the concrete implementation if the underlying library
478 425 * has built-in support for bitwise operations.
479 - *
480 - * @param string $a
481 - * @param string $b
482 - *
483 - * @return string
484 426 */
485 - public function and(string $a, string $b): string
427 + public function and(string $a, string $b) : string
486 428 {
487 429 return $this->bitwise('and', $a, $b);
488 430 }
489 431 /**
@@ -490,15 +432,10 @@
490 432 * Calculates bitwise OR of two numbers.
491 433 *
492 434 * This method can be overridden by the concrete implementation if the underlying library
493 435 * has built-in support for bitwise operations.
494 - *
495 - * @param string $a
496 - * @param string $b
497 - *
498 - * @return string
499 436 */
500 - public function or(string $a, string $b): string
437 + public function or(string $a, string $b) : string
501 438 {
502 439 return $this->bitwise('or', $a, $b);
503 440 }
504 441 /**
@@ -505,15 +442,10 @@
505 442 * Calculates bitwise XOR of two numbers.
506 443 *
507 444 * This method can be overridden by the concrete implementation if the underlying library
508 445 * has built-in support for bitwise operations.
509 - *
510 - * @param string $a
511 - * @param string $b
512 - *
513 - * @return string
514 446 */
515 - public function xor(string $a, string $b): string
447 + public function xor(string $a, string $b) : string
516 448 {
517 449 return $this->bitwise('xor', $a, $b);
518 450 }
519 451 /**
@@ -518,15 +450,13 @@
518 450 }
519 451 /**
520 452 * Performs a bitwise operation on a decimal number.
521 453 *
522 - * @param string $operator The operator to use, must be "and", "or" or "xor".
523 - * @param string $a The left operand.
524 - * @param string $b The right operand.
525 - *
526 - * @return string
454 + * @param 'and'|'or'|'xor' $operator The operator to use.
455 + * @param string $a The left operand.
456 + * @param string $b The right operand.
527 457 */
528 - private function bitwise(string $operator, string $a, string $b): string
458 + private function bitwise(string $operator, string $a, string $b) : string
529 459 {
530 460 [$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
531 461 $aBin = $this->toBinary($aDig);
532 462 $bBin = $this->toBinary($bDig);
@@ -542,25 +472,18 @@
542 472 }
543 473 if ($bNeg) {
544 474 $bBin = $this->twosComplement($bBin);
545 475 }
546 - switch ($operator) {
547 - case 'and':
548 - $value = $aBin & $bBin;
549 - $negative = ($aNeg and $bNeg);
550 - break;
551 - case 'or':
552 - $value = $aBin | $bBin;
553 - $negative = ($aNeg or $bNeg);
554 - break;
555 - case 'xor':
556 - $value = $aBin ^ $bBin;
557 - $negative = ($aNeg xor $bNeg);
558 - break;
559 - // @codeCoverageIgnoreStart
560 - default:
561 - throw new \InvalidArgumentException('Invalid bitwise operator.');
562 - }
476 + $value = match ($operator) {
477 + 'and' => $aBin & $bBin,
478 + 'or' => $aBin | $bBin,
479 + 'xor' => $aBin ^ $bBin,
480 + };
481 + $negative = match ($operator) {
482 + 'and' => $aNeg and $bNeg,
483 + 'or' => $aNeg or $bNeg,
484 + 'xor' => $aNeg xor $bNeg,
485 + };
563 486 if ($negative) {
564 487 $value = $this->twosComplement($value);
565 488 }
566 489 $result = $this->toDecimal($value);
@@ -567,12 +490,10 @@
567 490 return $negative ? $this->neg($result) : $result;
568 491 }
569 492 /**
570 493 * @param string $number A positive, binary number.
571 - *
572 - * @return string
573 494 */
574 - private function twosComplement(string $number): string
495 + private function twosComplement(string $number) : string
575 496 {
576 497 $xor = \str_repeat("\xff", \strlen($number));
577 498 $number ^= $xor;
578 499 for ($i = \strlen($number) - 1; $i >= 0; $i--) {
@@ -591,12 +512,10 @@
591 512 /**
592 513 * Converts a decimal number to a binary string.
593 514 *
594 515 * @param string $number The number to convert, positive or zero, only digits.
595 - *
596 - * @return string
597 516 */
598 - private function toBinary(string $number): string
517 + private function toBinary(string $number) : string
599 518 {
600 519 $result = '';
601 520 while ($number !== '0') {
602 521 [$number, $remainder] = $this->divQR($number, '256');
@@ -607,12 +526,10 @@
607 526 /**
608 527 * Returns the positive decimal representation of a binary number.
609 528 *
610 529 * @param string $bytes The bytes representing the number.
611 - *
612 - * @return string
613 530 */
614 - private function toDecimal(string $bytes): string
531 + private function toDecimal(string $bytes) : string
615 532 {
616 533 $result = '0';
617 534 $power = '1';
618 535 for ($i = \strlen($bytes) - 1; $i >= 0; $i--) {