PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 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 All 36 releases
← All changes | includes/sdk/google/brick/math/src/BigRational.php +54 -151 1.1.1 → 1.4.2 View file →
@@ -1,13 +1,14 @@
1 1 <?php
2 2
3 3 declare (strict_types=1);
4 -namespace Dudlewebs\WPMCS\Brick\Math;
4 +namespace Dudlewebs\WPMCS\GCP\Brick\Math;
5 5
6 -use Dudlewebs\WPMCS\Brick\Math\Exception\DivisionByZeroException;
7 -use Dudlewebs\WPMCS\Brick\Math\Exception\MathException;
8 -use Dudlewebs\WPMCS\Brick\Math\Exception\NumberFormatException;
9 -use Dudlewebs\WPMCS\Brick\Math\Exception\RoundingNecessaryException;
6 +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\DivisionByZeroException;
7 +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\MathException;
8 +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\NumberFormatException;
9 +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\RoundingNecessaryException;
10 +use Dudlewebs\WPMCS\GCP\Override;
10 11 /**
11 12 * An arbitrarily large rational number.
12 13 *
13 14 * This class is immutable.
@@ -17,18 +18,14 @@
17 18 final class BigRational extends BigNumber
18 19 {
19 20 /**
20 21 * The numerator.
21 - *
22 - * @var BigInteger
23 22 */
24 - private $numerator;
23 + private readonly BigInteger $numerator;
25 24 /**
26 25 * The denominator. Always strictly positive.
27 - *
28 - * @var BigInteger
29 26 */
30 - private $denominator;
27 + private readonly BigInteger $denominator;
31 28 /**
32 29 * Protected constructor. Use a factory method to obtain an instance.
33 30 *
34 31 * @param BigInteger $numerator The numerator.
@@ -51,21 +48,14 @@
51 48 $this->numerator = $numerator;
52 49 $this->denominator = $denominator;
53 50 }
54 51 /**
55 - * Creates a BigRational of the given value.
56 - *
57 - * @param BigNumber|int|float|string $value
58 - *
59 - * @return BigRational
60 - *
61 - * @throws MathException If the value cannot be converted to a BigRational.
62 - *
63 52 * @psalm-pure
64 53 */
65 - public static function of($value): BigNumber
54 + #[\Override]
55 + protected static function from(BigNumber $number) : static
66 56 {
67 - return parent::of($value)->toBigRational();
57 + return $number->toBigRational();
68 58 }
69 59 /**
70 60 * Creates a BigRational out of a numerator and a denominator.
71 61 *
@@ -74,10 +64,8 @@
74 64 *
75 65 * @param BigNumber|int|float|string $numerator The numerator. Must be convertible to a BigInteger.
76 66 * @param BigNumber|int|float|string $denominator The denominator. Must be convertible to a BigInteger.
77 67 *
78 - * @return BigRational
79 - *
80 68 * @throws NumberFormatException If an argument does not represent a valid number.
81 69 * @throws RoundingNecessaryException If an argument represents a non-integer number.
82 70 * @throws DivisionByZeroException If the denominator is zero.
83 71 *
@@ -82,9 +70,9 @@
82 70 * @throws DivisionByZeroException If the denominator is zero.
83 71 *
84 72 * @psalm-pure
85 73 */
86 - public static function nd($numerator, $denominator): BigRational
74 + public static function nd(BigNumber|int|float|string $numerator, BigNumber|int|float|string $denominator) : BigRational
87 75 {
88 76 $numerator = BigInteger::of($numerator);
89 77 $denominator = BigInteger::of($denominator);
90 78 return new BigRational($numerator, $denominator, \true);
@@ -91,13 +79,11 @@
91 79 }
92 80 /**
93 81 * Returns a BigRational representing zero.
94 82 *
95 - * @return BigRational
96 - *
97 83 * @psalm-pure
98 84 */
99 - public static function zero(): BigRational
85 + public static function zero() : BigRational
100 86 {
101 87 /**
102 88 * @psalm-suppress ImpureStaticVariable
103 89 * @var BigRational|null $zero
@@ -110,13 +96,11 @@
110 96 }
111 97 /**
112 98 * Returns a BigRational representing one.
113 99 *
114 - * @return BigRational
115 - *
116 100 * @psalm-pure
117 101 */
118 - public static function one(): BigRational
102 + public static function one() : BigRational
119 103 {
120 104 /**
121 105 * @psalm-suppress ImpureStaticVariable
122 106 * @var BigRational|null $one
@@ -129,13 +113,11 @@
129 113 }
130 114 /**
131 115 * Returns a BigRational representing ten.
132 116 *
133 - * @return BigRational
134 - *
135 117 * @psalm-pure
136 118 */
137 - public static function ten(): BigRational
119 + public static function ten() : BigRational
138 120 {
139 121 /**
140 122 * @psalm-suppress ImpureStaticVariable
141 123 * @var BigRational|null $ten
@@ -145,37 +127,27 @@
145 127 $ten = new BigRational(BigInteger::ten(), BigInteger::one(), \false);
146 128 }
147 129 return $ten;
148 130 }
149 - /**
150 - * @return BigInteger
151 - */
152 - public function getNumerator(): BigInteger
131 + public function getNumerator() : BigInteger
153 132 {
154 133 return $this->numerator;
155 134 }
156 - /**
157 - * @return BigInteger
158 - */
159 - public function getDenominator(): BigInteger
135 + public function getDenominator() : BigInteger
160 136 {
161 137 return $this->denominator;
162 138 }
163 139 /**
164 140 * Returns the quotient of the division of the numerator by the denominator.
165 - *
166 - * @return BigInteger
167 141 */
168 - public function quotient(): BigInteger
142 + public function quotient() : BigInteger
169 143 {
170 144 return $this->numerator->quotient($this->denominator);
171 145 }
172 146 /**
173 147 * Returns the remainder of the division of the numerator by the denominator.
174 - *
175 - * @return BigInteger
176 148 */
177 - public function remainder(): BigInteger
149 + public function remainder() : BigInteger
178 150 {
179 151 return $this->numerator->remainder($this->denominator);
180 152 }
181 153 /**
@@ -181,10 +153,12 @@
181 153 /**
182 154 * Returns the quotient and remainder of the division of the numerator by the denominator.
183 155 *
184 156 * @return BigInteger[]
157 + *
158 + * @psalm-return array{BigInteger, BigInteger}
185 159 */
186 - public function quotientAndRemainder(): array
160 + public function quotientAndRemainder() : array
187 161 {
188 162 return $this->numerator->quotientAndRemainder($this->denominator);
189 163 }
190 164 /**
@@ -191,13 +165,11 @@
191 165 * Returns the sum of this number and the given one.
192 166 *
193 167 * @param BigNumber|int|float|string $that The number to add.
194 168 *
195 - * @return BigRational The result.
196 - *
197 169 * @throws MathException If the number is not valid.
198 170 */
199 - public function plus($that): BigRational
171 + public function plus(BigNumber|int|float|string $that) : BigRational
200 172 {
201 173 $that = BigRational::of($that);
202 174 $numerator = $this->numerator->multipliedBy($that->denominator);
203 175 $numerator = $numerator->plus($that->numerator->multipliedBy($this->denominator));
@@ -208,13 +180,11 @@
208 180 * Returns the difference of this number and the given one.
209 181 *
210 182 * @param BigNumber|int|float|string $that The number to subtract.
211 183 *
212 - * @return BigRational The result.
213 - *
214 184 * @throws MathException If the number is not valid.
215 185 */
216 - public function minus($that): BigRational
186 + public function minus(BigNumber|int|float|string $that) : BigRational
217 187 {
218 188 $that = BigRational::of($that);
219 189 $numerator = $this->numerator->multipliedBy($that->denominator);
220 190 $numerator = $numerator->minus($that->numerator->multipliedBy($this->denominator));
@@ -225,13 +195,11 @@
225 195 * Returns the product of this number and the given one.
226 196 *
227 197 * @param BigNumber|int|float|string $that The multiplier.
228 198 *
229 - * @return BigRational The result.
230 - *
231 199 * @throws MathException If the multiplier is not a valid number.
232 200 */
233 - public function multipliedBy($that): BigRational
201 + public function multipliedBy(BigNumber|int|float|string $that) : BigRational
234 202 {
235 203 $that = BigRational::of($that);
236 204 $numerator = $this->numerator->multipliedBy($that->numerator);
237 205 $denominator = $this->denominator->multipliedBy($that->denominator);
@@ -241,13 +209,11 @@
241 209 * Returns the result of the division of this number by the given one.
242 210 *
243 211 * @param BigNumber|int|float|string $that The divisor.
244 212 *
245 - * @return BigRational The result.
246 - *
247 213 * @throws MathException If the divisor is not a valid number, or is zero.
248 214 */
249 - public function dividedBy($that): BigRational
215 + public function dividedBy(BigNumber|int|float|string $that) : BigRational
250 216 {
251 217 $that = BigRational::of($that);
252 218 $numerator = $this->numerator->multipliedBy($that->denominator);
253 219 $denominator = $this->denominator->multipliedBy($that->numerator);
@@ -255,15 +221,11 @@
255 221 }
256 222 /**
257 223 * Returns this number exponentiated to the given value.
258 224 *
259 - * @param int $exponent The exponent.
260 - *
261 - * @return BigRational The result.
262 - *
263 225 * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
264 226 */
265 - public function power(int $exponent): BigRational
227 + public function power(int $exponent) : BigRational
266 228 {
267 229 if ($exponent === 0) {
268 230 $one = BigInteger::one();
269 231 return new BigRational($one, $one, \false);
@@ -277,40 +239,32 @@
277 239 * Returns the reciprocal of this BigRational.
278 240 *
279 241 * The reciprocal has the numerator and denominator swapped.
280 242 *
281 - * @return BigRational
282 - *
283 243 * @throws DivisionByZeroException If the numerator is zero.
284 244 */
285 - public function reciprocal(): BigRational
245 + public function reciprocal() : BigRational
286 246 {
287 247 return new BigRational($this->denominator, $this->numerator, \true);
288 248 }
289 249 /**
290 250 * Returns the absolute value of this BigRational.
291 - *
292 - * @return BigRational
293 251 */
294 - public function abs(): BigRational
252 + public function abs() : BigRational
295 253 {
296 254 return new BigRational($this->numerator->abs(), $this->denominator, \false);
297 255 }
298 256 /**
299 257 * Returns the negated value of this BigRational.
300 - *
301 - * @return BigRational
302 258 */
303 - public function negated(): BigRational
259 + public function negated() : BigRational
304 260 {
305 261 return new BigRational($this->numerator->negated(), $this->denominator, \false);
306 262 }
307 263 /**
308 264 * Returns the simplified value of this BigRational.
309 - *
310 - * @return BigRational
311 265 */
312 - public function simplified(): BigRational
266 + public function simplified() : BigRational
313 267 {
314 268 $gcd = $this->numerator->gcd($this->denominator);
315 269 $numerator = $this->numerator->quotient($gcd);
316 270 $denominator = $this->denominator->quotient($gcd);
@@ -315,26 +269,20 @@
315 269 $numerator = $this->numerator->quotient($gcd);
316 270 $denominator = $this->denominator->quotient($gcd);
317 271 return new BigRational($numerator, $denominator, \false);
318 272 }
319 - /**
320 - * {@inheritdoc}
321 - */
322 - public function compareTo($that): int
273 + #[\Override]
274 + public function compareTo(BigNumber|int|float|string $that) : int
323 275 {
324 276 return $this->minus($that)->getSign();
325 277 }
326 - /**
327 - * {@inheritdoc}
328 - */
329 - public function getSign(): int
278 + #[\Override]
279 + public function getSign() : int
330 280 {
331 281 return $this->numerator->getSign();
332 282 }
333 - /**
334 - * {@inheritdoc}
335 - */
336 - public function toBigInteger(): BigInteger
283 + #[\Override]
284 + public function toBigInteger() : BigInteger
337 285 {
338 286 $simplified = $this->simplified();
339 287 if (!$simplified->denominator->isEqualTo(1)) {
340 288 throw new RoundingNecessaryException('This rational number cannot be represented as an integer value without rounding.');
@@ -340,47 +288,36 @@
340 288 throw new RoundingNecessaryException('This rational number cannot be represented as an integer value without rounding.');
341 289 }
342 290 return $simplified->numerator;
343 291 }
344 - /**
345 - * {@inheritdoc}
346 - */
347 - public function toBigDecimal(): BigDecimal
292 + #[\Override]
293 + public function toBigDecimal() : BigDecimal
348 294 {
349 295 return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
350 296 }
351 - /**
352 - * {@inheritdoc}
353 - */
354 - public function toBigRational(): BigRational
297 + #[\Override]
298 + public function toBigRational() : BigRational
355 299 {
356 300 return $this;
357 301 }
358 - /**
359 - * {@inheritdoc}
360 - */
361 - public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY): BigDecimal
302 + #[\Override]
303 + public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
362 304 {
363 305 return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
364 306 }
365 - /**
366 - * {@inheritdoc}
367 - */
368 - public function toInt(): int
307 + #[\Override]
308 + public function toInt() : int
369 309 {
370 310 return $this->toBigInteger()->toInt();
371 311 }
372 - /**
373 - * {@inheritdoc}
374 - */
375 - public function toFloat(): float
312 + #[\Override]
313 + public function toFloat() : float
376 314 {
377 - return $this->numerator->toFloat() / $this->denominator->toFloat();
315 + $simplified = $this->simplified();
316 + return $simplified->numerator->toFloat() / $simplified->denominator->toFloat();
378 317 }
379 - /**
380 - * {@inheritdoc}
381 - */
382 - public function __toString(): string
318 + #[\Override]
319 + public function __toString() : string
383 320 {
384 321 $numerator = (string) $this->numerator;
385 322 $denominator = (string) $this->denominator;
386 323 if ($denominator === '1') {
@@ -385,9 +322,9 @@
385 322 $denominator = (string) $this->denominator;
386 323 if ($denominator === '1') {
387 324 return $numerator;
388 325 }
389 - return $this->numerator . '/' . $this->denominator;
326 + return $numerator . '/' . $denominator;
390 327 }
391 328 /**
392 329 * This method is required for serializing the object and SHOULD NOT be accessed directly.
393 330 *
@@ -394,9 +331,9 @@
394 331 * @internal
395 332 *
396 333 * @return array{numerator: BigInteger, denominator: BigInteger}
397 334 */
398 - public function __serialize(): array
335 + public function __serialize() : array
399 336 {
400 337 return ['numerator' => $this->numerator, 'denominator' => $this->denominator];
401 338 }
402 339 /**
@@ -406,13 +343,11 @@
406 343 * @psalm-suppress RedundantPropertyInitializationCheck
407 344 *
408 345 * @param array{numerator: BigInteger, denominator: BigInteger} $data
409 346 *
410 - * @return void
411 - *
412 347 * @throws \LogicException
413 348 */
414 - public function __unserialize(array $data): void
349 + public function __unserialize(array $data) : void
415 350 {
416 351 if (isset($this->numerator)) {
417 352 throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
418 353 }
@@ -417,38 +352,6 @@
417 352 throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
418 353 }
419 354 $this->numerator = $data['numerator'];
420 355 $this->denominator = $data['denominator'];
421 - }
422 - /**
423 - * This method is required by interface Serializable and SHOULD NOT be accessed directly.
424 - *
425 - * @internal
426 - *
427 - * @return string
428 - */
429 - public function serialize(): string
430 - {
431 - return $this->numerator . '/' . $this->denominator;
432 - }
433 - /**
434 - * This method is only here to implement interface Serializable and cannot be accessed directly.
435 - *
436 - * @internal
437 - * @psalm-suppress RedundantPropertyInitializationCheck
438 - *
439 - * @param string $value
440 - *
441 - * @return void
442 - *
443 - * @throws \LogicException
444 - */
445 - public function unserialize($value): void
446 - {
447 - if (isset($this->numerator)) {
448 - throw new \LogicException('unserialize() is an internal function, it must not be called directly.');
449 - }
450 - [$numerator, $denominator] = \explode('/', $value);
451 - $this->numerator = BigInteger::of($numerator);
452 - $this->denominator = BigInteger::of($denominator);
453 356 }
454 357 }