Skip to content

Commit

Permalink
Immutable classes and pure functions/methods annotated (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyshockov authored and BenMorel committed Jan 27, 2020
1 parent d03814e commit ea9a095
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/BigDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Immutable, arbitrary-precision signed decimal numbers.
*
* @psalm-immutable
*/
final class BigDecimal extends BigNumber
{
Expand Down Expand Up @@ -54,6 +56,8 @@ protected function __construct(string $value, int $scale = 0)
* @return BigDecimal
*
* @throws MathException If the value cannot be converted to a BigDecimal.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
Expand All @@ -71,6 +75,8 @@ public static function of($value) : BigNumber
* @return BigDecimal
*
* @throws \InvalidArgumentException If the scale is negative.
*
* @psalm-pure
*/
public static function ofUnscaledValue($value, int $scale = 0) : BigDecimal
{
Expand All @@ -85,10 +91,13 @@ public static function ofUnscaledValue($value, int $scale = 0) : BigDecimal
* Returns a BigDecimal representing zero, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function zero() : BigDecimal
{
static $zero = null;
/** @psalm-suppress ImpureStaticVariable */
static $zero;

if ($zero === null) {
$zero = new BigDecimal('0');
Expand All @@ -101,10 +110,13 @@ public static function zero() : BigDecimal
* Returns a BigDecimal representing one, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function one() : BigDecimal
{
static $one = null;
/** @psalm-suppress ImpureStaticVariable */
static $one;

if ($one === null) {
$one = new BigDecimal('1');
Expand All @@ -117,10 +129,13 @@ public static function one() : BigDecimal
* Returns a BigDecimal representing ten, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function ten() : BigDecimal
{
static $ten = null;
/** @psalm-suppress ImpureStaticVariable */
static $ten;

if ($ten === null) {
$ten = new BigDecimal('10');
Expand Down
17 changes: 17 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
* All methods accepting a number as a parameter accept either a BigInteger instance,
* an integer, or a string representing an arbitrary size integer.
*
* @psalm-immutable
*/
final class BigInteger extends BigNumber
{
Expand Down Expand Up @@ -47,6 +49,8 @@ protected function __construct(string $value)
* @return BigInteger
*
* @throws MathException If the value cannot be converted to a BigInteger.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
Expand All @@ -71,6 +75,8 @@ public static function of($value) : BigNumber
*
* @throws NumberFormatException If the number is empty, or contains invalid chars for the given base.
* @throws \InvalidArgumentException If the base is out of range.
*
* @psalm-pure
*/
public static function fromBase(string $number, int $base) : BigInteger
{
Expand Down Expand Up @@ -136,6 +142,8 @@ public static function fromBase(string $number, int $base) : BigInteger
*
* @throws NumberFormatException If the given number is empty or contains invalid chars for the given alphabet.
* @throws \InvalidArgumentException If the alphabet does not contain at least 2 chars.
*
* @psalm-pure
*/
public static function fromArbitraryBase(string $number, string $alphabet) : BigInteger
{
Expand Down Expand Up @@ -164,9 +172,12 @@ public static function fromArbitraryBase(string $number, string $alphabet) : Big
* Returns a BigInteger representing zero.
*
* @return BigInteger
*
* @psalm-pure
*/
public static function zero() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
static $zero;

if ($zero === null) {
Expand All @@ -180,9 +191,12 @@ public static function zero() : BigInteger
* Returns a BigInteger representing one.
*
* @return BigInteger
*
* @psalm-pure
*/
public static function one() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
static $one;

if ($one === null) {
Expand All @@ -196,9 +210,12 @@ public static function one() : BigInteger
* Returns a BigInteger representing ten.
*
* @return BigInteger
*
* @psalm-pure
*/
public static function ten() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
static $ten;

if ($ten === null) {
Expand Down
19 changes: 19 additions & 0 deletions src/BigNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Common interface for arbitrary-precision rational numbers.
*
* @psalm-immutable
*/
abstract class BigNumber implements \Serializable, \JsonSerializable
{
Expand Down Expand Up @@ -50,6 +52,8 @@ abstract class BigNumber implements \Serializable, \JsonSerializable
*
* @throws NumberFormatException If the format of the number is not valid.
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
Expand Down Expand Up @@ -113,6 +117,9 @@ public static function of($value) : BigNumber
* @param float $float
*
* @return string
*
* @psalm-pure
* @psalm-suppress ImpureFunctionCall
*/
private static function floatToString(float $float) : string
{
Expand All @@ -134,6 +141,8 @@ private static function floatToString(float $float) : string
* @param mixed ...$args The arguments to the constructor.
*
* @return static
*
* @psalm-pure
*/
protected static function create(... $args) : BigNumber
{
Expand All @@ -151,6 +160,8 @@ protected static function create(... $args) : BigNumber
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-pure
*/
public static function min(...$values) : BigNumber
{
Expand Down Expand Up @@ -181,6 +192,8 @@ public static function min(...$values) : BigNumber
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-pure
*/
public static function max(...$values) : BigNumber
{
Expand Down Expand Up @@ -211,6 +224,8 @@ public static function max(...$values) : BigNumber
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-pure
*/
public static function sum(...$values) : BigNumber
{
Expand Down Expand Up @@ -246,6 +261,8 @@ public static function sum(...$values) : BigNumber
* @param BigNumber $b
*
* @return BigNumber
*
* @psalm-pure
*/
private static function add(BigNumber $a, BigNumber $b) : BigNumber
{
Expand Down Expand Up @@ -276,6 +293,8 @@ private static function add(BigNumber $a, BigNumber $b) : BigNumber
* @param string $number The number, validated as a non-empty string of digits with optional sign.
*
* @return string
*
* @psalm-pure
*/
private static function cleanUp(string $number) : string
{
Expand Down
15 changes: 15 additions & 0 deletions src/BigRational.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* An arbitrarily large rational number.
*
* This class is immutable.
*
* @psalm-immutable
*/
final class BigRational extends BigNumber
{
Expand Down Expand Up @@ -64,6 +66,8 @@ protected function __construct(BigInteger $numerator, BigInteger $denominator, b
* @return BigRational
*
* @throws MathException If the value cannot be converted to a BigRational.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
Expand All @@ -84,6 +88,8 @@ public static function of($value) : BigNumber
* @throws NumberFormatException If an argument does not represent a valid number.
* @throws RoundingNecessaryException If an argument represents a non-integer number.
* @throws DivisionByZeroException If the denominator is zero.
*
* @psalm-pure
*/
public static function nd($numerator, $denominator) : BigRational
{
Expand All @@ -97,9 +103,12 @@ public static function nd($numerator, $denominator) : BigRational
* Returns a BigRational representing zero.
*
* @return BigRational
*
* @psalm-pure
*/
public static function zero() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
static $zero;

if ($zero === null) {
Expand All @@ -113,9 +122,12 @@ public static function zero() : BigRational
* Returns a BigRational representing one.
*
* @return BigRational
*
* @psalm-pure
*/
public static function one() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
static $one;

if ($one === null) {
Expand All @@ -129,9 +141,12 @@ public static function one() : BigRational
* Returns a BigRational representing ten.
*
* @return BigRational
*
* @psalm-pure
*/
public static function ten() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
static $ten;

if ($ten === null) {
Expand Down
4 changes: 4 additions & 0 deletions src/Exception/DivisionByZeroException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class DivisionByZeroException extends MathException
{
/**
* @return DivisionByZeroException
*
* @psalm-pure
*/
public static function divisionByZero() : DivisionByZeroException
{
Expand All @@ -19,6 +21,8 @@ public static function divisionByZero() : DivisionByZeroException

/**
* @return DivisionByZeroException
*
* @psalm-pure
*/
public static function denominatorMustNotBeZero() : DivisionByZeroException
{
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/IntegerOverflowException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class IntegerOverflowException extends MathException
* @param BigInteger $value
*
* @return IntegerOverflowException
*
* @psalm-pure
*/
public static function toIntOverflow(BigInteger $value) : IntegerOverflowException
{
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/NumberFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class NumberFormatException extends MathException
* @param string $char The failing character.
*
* @return NumberFormatException
*
* @psalm-pure
*/
public static function charNotInAlphabet(string $char) : self
{
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RoundingNecessaryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class RoundingNecessaryException extends MathException
{
/**
* @return RoundingNecessaryException
*
* @psalm-pure
*/
public static function roundingNecessary() : RoundingNecessaryException
{
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* All methods must return strings respecting this format, unless specified otherwise.
*
* @internal
*
* @psalm-immutable
*/
abstract class Calculator
{
Expand Down Expand Up @@ -57,10 +59,13 @@ final public static function set(?Calculator $calculator) : void
* If none has been explicitly set, the fastest available implementation will be returned.
*
* @return Calculator
*
* @psalm-pure
*/
final public static function get() : Calculator
{
if (self::$instance === null) {
/** @psalm-suppress ImpureMethodCall */
self::$instance = self::detect();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Calculator/BcMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Calculator implementation built around the bcmath library.
*
* @internal
*
* @psalm-immutable
*/
class BcMathCalculator extends Calculator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Calculator/GmpCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Calculator implementation built around the GMP library.
*
* @internal
*
* @psalm-immutable
*/
class GmpCalculator extends Calculator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Calculator/NativeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Calculator implementation using only native PHP code.
*
* @internal
*
* @psalm-immutable
*/
class NativeCalculator extends Calculator
{
Expand Down

0 comments on commit ea9a095

Please sign in to comment.