Completed
Push — develop ( 3d25fb...24403a )
by Remco
07:26
created

Parser::parse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
1
<?php
2
/**
3
 * Parser
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money;
12
13
/**
14
 * Parser
15
 *
16
 * @author Remco Tolsma
17
 * @version 1.0
18
 */
19
class Parser {
20
	/**
21
	 * Parse.
22
	 *
23
	 * @link https://github.com/wp-pay/core/blob/2.0.2/src/Core/Util.php#L128-L176
24
	 *
25
	 * @param string $money
0 ignored issues
show
Bug introduced by
There is no parameter named $money. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
	 * @return Money
27
	 */
28
	public function parse( $string ) {
29
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
30
31
		$decimal_sep = $wp_locale->number_format['decimal_point'];
32
33
		// Seperators.
34
		$seperators = array( $decimal_sep, '.', ',' );
35
		$seperators = array_unique( array_filter( $seperators ) );
36
37
		// Check.
38
		foreach ( array( - 3, - 2 ) as $i ) {
39
			$test = substr( $string, $i, 1 );
40
41
			if ( in_array( $test, $seperators, true ) ) {
42
				$decimal_sep = $test;
43
44
				break;
45
			}
46
		}
47
48
		// Split.
49
		$position = strrpos( $string, $decimal_sep );
50
51
		if ( false !== $position ) {
52
			$full = substr( $string, 0, $position );
53
			$half = substr( $string, $position + 1 );
54
55
			$full = filter_var( $full, FILTER_SANITIZE_NUMBER_INT );
56
			$half = filter_var( $half, FILTER_SANITIZE_NUMBER_INT );
57
58
			$string = $full . '.' . $half;
59
		} else {
60
			$string = filter_var( $string, FILTER_SANITIZE_NUMBER_INT );
61
		}
62
63
		// Filter.
64
		$value = filter_var( $string, FILTER_VALIDATE_FLOAT );
65
66
		return new Money( $value );
67
	}
68
}
69