Test Failed
Push — develop ( 203fe3...42b9cf )
by Reüel
03:24
created

src/Money.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Money
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;
12
13
/**
14
 * Money
15
 *
16
 * @author Remco Tolsma
17
 * @version 1.2.1
18
 * @since   1.0.0
19
 */
20
class Money {
21
	/**
22
	 * Amount value.
23
	 *
24
	 * @var float
25
	 */
26
	private $value;
27
28
	/**
29
	 * Currency.
30
	 *
31
	 * @var Currency
32
	 */
33
	private $currency;
34
35
	/**
36
	 * Construct and initialize money object.
37
	 *
38
	 * @param string|int|float     $value    Amount value.
39
	 * @param Currency|string|null $currency Currency.
40
	 */
41
	public function __construct( $value = 0, $currency = null ) {
42
		$this->set_value( $value );
43
		$this->set_currency( $currency );
0 ignored issues
show
It seems like $currency defined by parameter $currency on line 41 can also be of type null or object<Pronamic\WordPress\Money\Currency>; however, Pronamic\WordPress\Money\Money::set_currency() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
	}
45
46
	/**
47
	 * Get default format.
48
	 *
49
	 * @return string
50
	 */
51
	public static function get_default_format() {
52
		/* translators: 1: currency symbol, 2: amount value, 3: currency code, note: use non-breaking space! */
53
		$format = _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' );
54
		// Note:               ↳ Non-breaking space.
55
		$format = apply_filters( 'pronamic_money_default_format', $format );
56
57
		return $format;
58
	}
59
60
	/**
61
	 * Format i18n.
62
	 *
63
	 * @param string|null $format Format.
64
	 *
65
	 * @return string
66
	 */
67
	public function format_i18n( $format = null ) {
68
		if ( is_null( $format ) ) {
69
			$format = self::get_default_format();
70
		}
71
72
		if ( isset( $this->currency ) ) {
73
			return sprintf(
74
				$format,
75
				$this->currency->get_symbol(),
76
				number_format_i18n( $this->get_value(), $this->currency->get_number_decimals() ),
77
				$this->currency->get_alphabetic_code()
78
			);
79
		}
80
81
		return number_format_i18n( $this->get_value(), 2 );
82
	}
83
84
	/**
85
	 * Get value.
86
	 *
87
	 * @return float Amount value.
88
	 */
89
	public function get_value() {
90
		return $this->value;
91
	}
92
93
	/**
94
	 * Get amount.
95
	 *
96
	 * @deprecated 1.2.0
97
	 * @return float Amount value.
98
	 */
99
	public function get_amount() {
100
		_deprecated_function( __METHOD__, '1.2.0', 'Money::get_value()' );
101
102
		return $this->get_value();
103
	}
104
105
	/**
106
	 * Get cents.
107
	 *
108
	 * @return float
109
	 */
110
	public function get_cents() {
111
		if ( function_exists( 'bcmul' ) ) {
112
			return (float) bcmul( $this->value, 100 );
113
		}
114
115
		return $this->value * 100;
116
	}
117
118
	/**
119
	 * Get amount in minor units.
120
	 *
121
	 * Examples for value 10:
122
	 *   JPY 0 decimals: 10
123
	 *   EUR 2 decimals: 1000
124
	 *   BHD 3 decimals: 10000
125
	 *   NLG 4 decimals: 100000
126
	 *
127
	 * @since 1.2.1
128
	 *
129
	 * @return int
130
	 */
131
	public function get_minor_units() {
132
		// Use 2 decimals by default (most common).
133
		$decimals = 2;
134
135
		// Get number of decimals from currency if available.
136
		if ( $this->get_currency() ) {
137
			$decimals = $this->currency->get_number_decimals();
138
		}
139
140
		// Return amount in minor units.
141
		if ( function_exists( 'bcmul' ) ) {
142
			$minor_units = bcmul( $this->value, pow( 10, $decimals ), 0 );
143
		} else {
144
			$minor_units = $this->value * pow( 10, $decimals );
145
		}
146
147
		return (int) (string) $minor_units;
148
	}
149
150
	/**
151
	 * Set value.
152
	 *
153
	 * @param mixed $value Amount value.
154
	 */
155
	public function set_value( $value ) {
156
		$this->value = floatval( $value );
157
	}
158
159
	/**
160
	 * Set amount.
161
	 *
162
	 * @deprecated 1.2.0
163
	 * @param mixed $value Amount value.
164
	 */
165
	public function set_amount( $value ) {
166
		_deprecated_function( __METHOD__, '1.2.0', 'Money::set_value()' );
167
168
		$this->set_value( $value );
169
	}
170
171
	/**
172
	 * Get currency.
173
	 *
174
	 * @return Currency
175
	 */
176
	public function get_currency() {
177
		return $this->currency;
178
	}
179
180
	/**
181
	 * Set currency.
182
	 *
183
	 * @param string $currency Currency.
184
	 */
185
	public function set_currency( $currency ) {
186
		if ( is_object( $currency ) && is_a( $currency, __NAMESPACE__ . '\Currency' ) ) {
187
			$this->currency = $currency;
188
189
			return;
190
		}
191
192
		$this->currency = Currency::get_instance( $currency );
193
	}
194
195
	/**
196
	 * Create a string representation of this money object.
197
	 *
198
	 * @return string
199
	 */
200
	public function __toString() {
201
		return $this->format_i18n();
202
	}
203
}
204