Test Failed
Push — develop ( 88d89c...00d2c4 )
by Remco
12:20
created

BcMathCalculator::subtract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
cc 1
nc 1
nop 2
rs 10
1
<?php
2
/**
3
 * BC Math Calculator
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money\Calculator;
12
13
use Pronamic\WordPress\Money\Calculator;
14
15
/**
16
 * BC Math Calculator
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.2.1
20
 * @since   1.0.0
21
 */
22
class BcMathCalculator implements Calculator {
23
	/**
24
	 * Scale.
25
	 *
26
	 * @var int
27
	 */
28
	private $scale;
29
30
	/**
31
	 * Construct BC Math Calculator.
32
	 *
33
	 * @param int $scale Scale.
34
	 */
35
	public function __construct( $scale = 14 ) {
36
		$this->scale = $scale;
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	public static function supported() {
43
		return extension_loaded( 'bcmath' );
44
	}
45
46
	/**
47
	 * {@inheritdoc}
48
	 *
49
	 * @link https://github.com/moneyphp/money/blob/v3.2.1/src/Calculator/BcMathCalculator.php
50
	 *
51
	 * @param string $value  Value.
52
	 * @param string $addend Addend.
53
	 *
54
	 * @return string
55
	 */
56
	public function add( $value, $addend ) {
57
		return bcadd( $value, $addend, $this->scale );
58
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 *
63
	 * @link https://github.com/moneyphp/money/blob/v3.2.1/src/Calculator/BcMathCalculator.php#L51-L62
64
	 *
65
	 * @param string $value      Value.
66
	 * @param string $subtrahend Subtrahend.
67
	 *
68
	 * @return string
69
	 */
70
	public function subtract( $value, $subtrahend ) {
71
		return bcsub( $value, $subtrahend, $this->scale );
72
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 *
77
	 * @link https://github.com/moneyphp/money/blob/v3.2.1/src/Calculator/BcMathCalculator.php#L64-L72
78
	 *
79
	 * @param string           $value      Value.
80
	 * @param int|float|string $multiplier Multiplier.
81
	 *
82
	 * @return string
83
	 */
84
	public function multiply( $value, $multiplier ) {
85
		return bcmul( $value, strval( $multiplier ), $this->scale );
86
	}
87
88
	/**
89
	 * {@inheritdoc}
90
	 *
91
	 * @link https://github.com/moneyphp/money/blob/v3.2.1/src/Calculator/BcMathCalculator.php#L74-L82
92
	 *
93
	 * @param string           $value   Value.
94
	 * @param int|float|string $divisor Divisor.
95
	 *
96
	 * @return string
97
	 */
98
	public function divide( $value, $divisor ) {
99
		return bcdiv( $value, strval( $divisor ), $this->scale );
100
	}
101
}
102