Test Failed
Push — develop ( 8240d1...c1373b )
by Reüel
02:13
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.0
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
		return $this->value * 100;
112
	}
113
114
	/**
115
	 * Get amount in minor units.
116
	 *
117
	 * Examples for value 10:
118
	 *   JPY 0 decimals: 10
119
	 *   EUR 2 decimals: 1000
120
	 *   BHD 3 decimals: 10000
121
	 *   NLG 4 decimals: 100000
122
	 *
123
	 * @return int
124
	 */
125
	public function get_minor_units() {
126
		// Use 2 decimals by default (most common).
127
		$decimals = 2;
128
129
		// Get number of decimals from currency if available.
130
		if ( $this->get_currency() ) {
131
			$decimals = $this->currency->get_number_decimals();
132
		}
133
134
		// Return amount in minor units.
135
		return (int) $this->value * pow( 10, $decimals );
136
	}
137
138
	/**
139
	 * Set value.
140
	 *
141
	 * @param mixed $value Amount value.
142
	 */
143
	public function set_value( $value ) {
144
		$this->value = floatval( $value );
145
	}
146
147
	/**
148
	 * Set amount.
149
	 *
150
	 * @deprecated 1.2.0
151
	 * @param mixed $value Amount value.
152
	 */
153
	public function set_amount( $value ) {
154
		_deprecated_function( __METHOD__, '1.2.0', 'Money::set_value()' );
155
156
		$this->set_value( $value );
157
	}
158
159
	/**
160
	 * Get currency.
161
	 *
162
	 * @return Currency
163
	 */
164
	public function get_currency() {
165
		return $this->currency;
166
	}
167
168
	/**
169
	 * Set currency.
170
	 *
171
	 * @param string $currency Currency.
172
	 */
173
	public function set_currency( $currency ) {
174
		if ( is_object( $currency ) && is_a( $currency, __NAMESPACE__ . '\Currency' ) ) {
175
			$this->currency = $currency;
176
177
			return;
178
		}
179
180
		$this->currency = Currency::get_instance( $currency );
181
	}
182
183
	/**
184
	 * Create a string representation of this money object.
185
	 *
186
	 * @return string
187
	 */
188
	public function __toString() {
189
		return $this->format_i18n();
190
	}
191
}
192