Test Failed
Push — develop ( 42b9cf...b55195 )
by Reüel
03:54
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
		$alphabetic_code = $this->currency->get_alphabetic_code();
73
74
		if ( ! empty( $alphabetic_code ) ) {
75
			return sprintf(
76
				$format,
77
				$this->currency->get_symbol(),
78
				number_format_i18n( $this->get_value(), $this->currency->get_number_decimals() ),
79
				$this->currency->get_alphabetic_code()
80
			);
81
		}
82
83
		return number_format_i18n( $this->get_value(), 2 );
84
	}
85
86
	/**
87
	 * Get value.
88
	 *
89
	 * @return float Amount value.
90
	 */
91
	public function get_value() {
92
		return $this->value;
93
	}
94
95
	/**
96
	 * Get amount.
97
	 *
98
	 * @deprecated 1.2.0
99
	 * @return float Amount value.
100
	 */
101
	public function get_amount() {
102
		_deprecated_function( __METHOD__, '1.2.0', 'Money::get_value()' );
103
104
		return $this->get_value();
105
	}
106
107
	/**
108
	 * Get cents.
109
	 *
110
	 * @return float
111
	 */
112
	public function get_cents() {
113
		if ( function_exists( 'bcmul' ) ) {
114
			return (float) bcmul( $this->value, 100 );
115
		}
116
117
		return $this->value * 100;
118
	}
119
120
	/**
121
	 * Get amount in minor units.
122
	 *
123
	 * Examples for value 10:
124
	 *   JPY 0 decimals: 10
125
	 *   EUR 2 decimals: 1000
126
	 *   BHD 3 decimals: 10000
127
	 *   NLG 4 decimals: 100000
128
	 *
129
	 * @since 1.2.1
130
	 *
131
	 * @return int
132
	 */
133
	public function get_minor_units() {
134
		// Use 2 decimals by default (most common).
135
		$decimals = 2;
136
137
		// Get number of decimals from currency if available.
138
		if ( $this->get_currency() ) {
139
			$decimals = $this->currency->get_number_decimals();
140
		}
141
142
		// Return amount in minor units.
143
		if ( function_exists( 'bcmul' ) ) {
144
			$minor_units = bcmul( $this->value, pow( 10, $decimals ), 0 );
145
		} else {
146
			$minor_units = $this->value * pow( 10, $decimals );
147
		}
148
149
		return (int) (string) $minor_units;
150
	}
151
152
	/**
153
	 * Set value.
154
	 *
155
	 * @param mixed $value Amount value.
156
	 */
157
	public function set_value( $value ) {
158
		$this->value = floatval( $value );
159
	}
160
161
	/**
162
	 * Set amount.
163
	 *
164
	 * @deprecated 1.2.0
165
	 * @param mixed $value Amount value.
166
	 */
167
	public function set_amount( $value ) {
168
		_deprecated_function( __METHOD__, '1.2.0', 'Money::set_value()' );
169
170
		$this->set_value( $value );
171
	}
172
173
	/**
174
	 * Get currency.
175
	 *
176
	 * @return Currency
177
	 */
178
	public function get_currency() {
179
		return $this->currency;
180
	}
181
182
	/**
183
	 * Set currency.
184
	 *
185
	 * @param string $currency Currency.
186
	 */
187
	public function set_currency( $currency ) {
188
		if ( is_object( $currency ) && is_a( $currency, __NAMESPACE__ . '\Currency' ) ) {
189
			$this->currency = $currency;
190
191
			return;
192
		}
193
194
		$this->currency = Currency::get_instance( $currency );
195
	}
196
197
	/**
198
	 * Create a string representation of this money object.
199
	 *
200
	 * @return string
201
	 */
202
	public function __toString() {
203
		return $this->format_i18n();
204
	}
205
}
206