Skip to content

Commit c179bec

Browse files
committed
Apply coding standard
1 parent 9dca599 commit c179bec

21 files changed

+1405
-1256
lines changed

src/BigDecimal.php

Lines changed: 90 additions & 78 deletions
Large diffs are not rendered by default.

src/BigInteger.php

Lines changed: 124 additions & 105 deletions
Large diffs are not rendered by default.

src/BigNumber.php

Lines changed: 244 additions & 224 deletions
Large diffs are not rendered by default.

src/BigRational.php

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Brick\Math\Exception\MathException;
99
use Brick\Math\Exception\NumberFormatException;
1010
use Brick\Math\Exception\RoundingNecessaryException;
11+
use InvalidArgumentException;
12+
use LogicException;
1113
use Override;
1214

1315
/**
@@ -46,21 +48,15 @@ protected function __construct(BigInteger $numerator, BigInteger $denominator, b
4648
}
4749

4850
if ($denominator->isNegative()) {
49-
$numerator = $numerator->negated();
51+
$numerator = $numerator->negated();
5052
$denominator = $denominator->negated();
5153
}
5254
}
5355

54-
$this->numerator = $numerator;
56+
$this->numerator = $numerator;
5557
$this->denominator = $denominator;
5658
}
5759

58-
#[Override]
59-
protected static function from(BigNumber $number): static
60-
{
61-
return $number->toBigRational();
62-
}
63-
6460
/**
6561
* Creates a BigRational out of a numerator and a denominator.
6662
*
@@ -79,8 +75,8 @@ protected static function from(BigNumber $number): static
7975
public static function nd(
8076
BigNumber|int|float|string $numerator,
8177
BigNumber|int|float|string $denominator,
82-
) : BigRational {
83-
$numerator = BigInteger::of($numerator);
78+
): BigRational {
79+
$numerator = BigInteger::of($numerator);
8480
$denominator = BigInteger::of($denominator);
8581

8682
return new BigRational($numerator, $denominator, true);
@@ -91,7 +87,7 @@ public static function nd(
9187
*
9288
* @pure
9389
*/
94-
public static function zero() : BigRational
90+
public static function zero(): BigRational
9591
{
9692
/** @var BigRational|null $zero */
9793
static $zero;
@@ -108,7 +104,7 @@ public static function zero() : BigRational
108104
*
109105
* @pure
110106
*/
111-
public static function one() : BigRational
107+
public static function one(): BigRational
112108
{
113109
/** @var BigRational|null $one */
114110
static $one;
@@ -125,7 +121,7 @@ public static function one() : BigRational
125121
*
126122
* @pure
127123
*/
128-
public static function ten() : BigRational
124+
public static function ten(): BigRational
129125
{
130126
/** @var BigRational|null $ten */
131127
static $ten;
@@ -140,15 +136,15 @@ public static function ten() : BigRational
140136
/**
141137
* @pure
142138
*/
143-
public function getNumerator() : BigInteger
139+
public function getNumerator(): BigInteger
144140
{
145141
return $this->numerator;
146142
}
147143

148144
/**
149145
* @pure
150146
*/
151-
public function getDenominator() : BigInteger
147+
public function getDenominator(): BigInteger
152148
{
153149
return $this->denominator;
154150
}
@@ -158,7 +154,7 @@ public function getDenominator() : BigInteger
158154
*
159155
* @pure
160156
*/
161-
public function quotient() : BigInteger
157+
public function quotient(): BigInteger
162158
{
163159
return $this->numerator->quotient($this->denominator);
164160
}
@@ -168,7 +164,7 @@ public function quotient() : BigInteger
168164
*
169165
* @pure
170166
*/
171-
public function remainder() : BigInteger
167+
public function remainder(): BigInteger
172168
{
173169
return $this->numerator->remainder($this->denominator);
174170
}
@@ -180,7 +176,7 @@ public function remainder() : BigInteger
180176
*
181177
* @pure
182178
*/
183-
public function quotientAndRemainder() : array
179+
public function quotientAndRemainder(): array
184180
{
185181
return $this->numerator->quotientAndRemainder($this->denominator);
186182
}
@@ -194,12 +190,12 @@ public function quotientAndRemainder() : array
194190
*
195191
* @pure
196192
*/
197-
public function plus(BigNumber|int|float|string $that) : BigRational
193+
public function plus(BigNumber|int|float|string $that): BigRational
198194
{
199195
$that = BigRational::of($that);
200196

201-
$numerator = $this->numerator->multipliedBy($that->denominator);
202-
$numerator = $numerator->plus($that->numerator->multipliedBy($this->denominator));
197+
$numerator = $this->numerator->multipliedBy($that->denominator);
198+
$numerator = $numerator->plus($that->numerator->multipliedBy($this->denominator));
203199
$denominator = $this->denominator->multipliedBy($that->denominator);
204200

205201
return new BigRational($numerator, $denominator, false);
@@ -214,12 +210,12 @@ public function plus(BigNumber|int|float|string $that) : BigRational
214210
*
215211
* @pure
216212
*/
217-
public function minus(BigNumber|int|float|string $that) : BigRational
213+
public function minus(BigNumber|int|float|string $that): BigRational
218214
{
219215
$that = BigRational::of($that);
220216

221-
$numerator = $this->numerator->multipliedBy($that->denominator);
222-
$numerator = $numerator->minus($that->numerator->multipliedBy($this->denominator));
217+
$numerator = $this->numerator->multipliedBy($that->denominator);
218+
$numerator = $numerator->minus($that->numerator->multipliedBy($this->denominator));
223219
$denominator = $this->denominator->multipliedBy($that->denominator);
224220

225221
return new BigRational($numerator, $denominator, false);
@@ -234,11 +230,11 @@ public function minus(BigNumber|int|float|string $that) : BigRational
234230
*
235231
* @pure
236232
*/
237-
public function multipliedBy(BigNumber|int|float|string $that) : BigRational
233+
public function multipliedBy(BigNumber|int|float|string $that): BigRational
238234
{
239235
$that = BigRational::of($that);
240236

241-
$numerator = $this->numerator->multipliedBy($that->numerator);
237+
$numerator = $this->numerator->multipliedBy($that->numerator);
242238
$denominator = $this->denominator->multipliedBy($that->denominator);
243239

244240
return new BigRational($numerator, $denominator, false);
@@ -253,11 +249,11 @@ public function multipliedBy(BigNumber|int|float|string $that) : BigRational
253249
*
254250
* @pure
255251
*/
256-
public function dividedBy(BigNumber|int|float|string $that) : BigRational
252+
public function dividedBy(BigNumber|int|float|string $that): BigRational
257253
{
258254
$that = BigRational::of($that);
259255

260-
$numerator = $this->numerator->multipliedBy($that->denominator);
256+
$numerator = $this->numerator->multipliedBy($that->denominator);
261257
$denominator = $this->denominator->multipliedBy($that->numerator);
262258

263259
return new BigRational($numerator, $denominator, true);
@@ -266,11 +262,11 @@ public function dividedBy(BigNumber|int|float|string $that) : BigRational
266262
/**
267263
* Returns this number exponentiated to the given value.
268264
*
269-
* @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
265+
* @throws InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
270266
*
271267
* @pure
272268
*/
273-
public function power(int $exponent) : BigRational
269+
public function power(int $exponent): BigRational
274270
{
275271
if ($exponent === 0) {
276272
$one = BigInteger::one();
@@ -285,7 +281,7 @@ public function power(int $exponent) : BigRational
285281
return new BigRational(
286282
$this->numerator->power($exponent),
287283
$this->denominator->power($exponent),
288-
false
284+
false,
289285
);
290286
}
291287

@@ -298,7 +294,7 @@ public function power(int $exponent) : BigRational
298294
*
299295
* @pure
300296
*/
301-
public function reciprocal() : BigRational
297+
public function reciprocal(): BigRational
302298
{
303299
return new BigRational($this->denominator, $this->numerator, true);
304300
}
@@ -308,7 +304,7 @@ public function reciprocal() : BigRational
308304
*
309305
* @pure
310306
*/
311-
public function abs() : BigRational
307+
public function abs(): BigRational
312308
{
313309
return new BigRational($this->numerator->abs(), $this->denominator, false);
314310
}
@@ -318,7 +314,7 @@ public function abs() : BigRational
318314
*
319315
* @pure
320316
*/
321-
public function negated() : BigRational
317+
public function negated(): BigRational
322318
{
323319
return new BigRational($this->numerator->negated(), $this->denominator, false);
324320
}
@@ -328,7 +324,7 @@ public function negated() : BigRational
328324
*
329325
* @pure
330326
*/
331-
public function simplified() : BigRational
327+
public function simplified(): BigRational
332328
{
333329
$gcd = $this->numerator->gcd($this->denominator);
334330

@@ -339,19 +335,19 @@ public function simplified() : BigRational
339335
}
340336

341337
#[Override]
342-
public function compareTo(BigNumber|int|float|string $that) : int
338+
public function compareTo(BigNumber|int|float|string $that): int
343339
{
344340
return $this->minus($that)->getSign();
345341
}
346342

347343
#[Override]
348-
public function getSign() : int
344+
public function getSign(): int
349345
{
350346
return $this->numerator->getSign();
351347
}
352348

353349
#[Override]
354-
public function toBigInteger() : BigInteger
350+
public function toBigInteger(): BigInteger
355351
{
356352
$simplified = $this->simplified();
357353

@@ -363,40 +359,41 @@ public function toBigInteger() : BigInteger
363359
}
364360

365361
#[Override]
366-
public function toBigDecimal() : BigDecimal
362+
public function toBigDecimal(): BigDecimal
367363
{
368364
return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
369365
}
370366

371367
#[Override]
372-
public function toBigRational() : BigRational
368+
public function toBigRational(): BigRational
373369
{
374370
return $this;
375371
}
376372

377373
#[Override]
378-
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
374+
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY): BigDecimal
379375
{
380376
return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
381377
}
382378

383379
#[Override]
384-
public function toInt() : int
380+
public function toInt(): int
385381
{
386382
return $this->toBigInteger()->toInt();
387383
}
388384

389385
#[Override]
390-
public function toFloat() : float
386+
public function toFloat(): float
391387
{
392388
$simplified = $this->simplified();
389+
393390
return $simplified->numerator->toFloat() / $simplified->denominator->toFloat();
394391
}
395392

396393
#[Override]
397-
public function __toString() : string
394+
public function __toString(): string
398395
{
399-
$numerator = (string) $this->numerator;
396+
$numerator = (string) $this->numerator;
400397
$denominator = (string) $this->denominator;
401398

402399
if ($denominator === '1') {
@@ -425,17 +422,23 @@ public function __serialize(): array
425422
*
426423
* @param array{numerator: BigInteger, denominator: BigInteger} $data
427424
*
428-
* @throws \LogicException
425+
* @throws LogicException
429426
*/
430427
public function __unserialize(array $data): void
431428
{
432429
/** @phpstan-ignore isset.initializedProperty */
433430
if (isset($this->numerator)) {
434-
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
431+
throw new LogicException('__unserialize() is an internal function, it must not be called directly.');
435432
}
436433

437434
/** @phpstan-ignore deadCode.unreachable */
438435
$this->numerator = $data['numerator'];
439436
$this->denominator = $data['denominator'];
440437
}
438+
439+
#[Override]
440+
protected static function from(BigNumber $number): static
441+
{
442+
return $number->toBigRational();
443+
}
441444
}

src/Exception/DivisionByZeroException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ final class DivisionByZeroException extends MathException
1212
/**
1313
* @pure
1414
*/
15-
public static function divisionByZero() : DivisionByZeroException
15+
public static function divisionByZero(): DivisionByZeroException
1616
{
1717
return new self('Division by zero.');
1818
}
1919

2020
/**
2121
* @pure
2222
*/
23-
public static function modulusMustNotBeZero() : DivisionByZeroException
23+
public static function modulusMustNotBeZero(): DivisionByZeroException
2424
{
2525
return new self('The modulus must not be zero.');
2626
}
2727

2828
/**
2929
* @pure
3030
*/
31-
public static function denominatorMustNotBeZero() : DivisionByZeroException
31+
public static function denominatorMustNotBeZero(): DivisionByZeroException
3232
{
3333
return new self('The denominator of a rational number cannot be zero.');
3434
}

src/Exception/IntegerOverflowException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Brick\Math\BigInteger;
88

9+
use function sprintf;
10+
11+
use const PHP_INT_MAX;
12+
use const PHP_INT_MIN;
13+
914
/**
1015
* Exception thrown when an integer overflow occurs.
1116
*/
@@ -14,10 +19,10 @@ final class IntegerOverflowException extends MathException
1419
/**
1520
* @pure
1621
*/
17-
public static function toIntOverflow(BigInteger $value) : IntegerOverflowException
22+
public static function toIntOverflow(BigInteger $value): IntegerOverflowException
1823
{
1924
$message = '%s is out of range %d to %d and cannot be represented as an integer.';
2025

21-
return new self(\sprintf($message, (string) $value, PHP_INT_MIN, PHP_INT_MAX));
26+
return new self(sprintf($message, (string) $value, PHP_INT_MIN, PHP_INT_MAX));
2227
}
2328
}

src/Exception/MathException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Brick\Math\Exception;
66

7+
use RuntimeException;
8+
79
/**
810
* Base class for all math exceptions.
911
*/
10-
class MathException extends \RuntimeException
12+
class MathException extends RuntimeException
1113
{
1214
}

0 commit comments

Comments
 (0)