Passed
Push — FixRounding ( 5ebe98 )
by Daniel
02:51
created

QuantityFormatterTest::validProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 91
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 3 Features 1
Metric Value
c 9
b 3
f 1
dl 0
loc 91
rs 8.518
cc 1
eloc 61
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\QuantityValue;
6
use ValueFormatters\DecimalFormatter;
7
use ValueFormatters\FormatterOptions;
8
use ValueFormatters\QuantityFormatter;
9
10
/**
11
 * @covers ValueFormatters\QuantityFormatter
12
 *
13
 * @group ValueFormatters
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0+
17
 * @author Daniel Kinzler
18
 */
19
class QuantityFormatterTest extends ValueFormatterTestBase {
20
21
	/**
22
	 * @deprecated since DataValues Interfaces 0.2, just use getInstance.
23
	 */
24
	protected function getFormatterClass() {
25
		throw new \LogicException( 'Should not be called, use getInstance' );
26
	}
27
28
	/**
29
	 * @see ValueFormatterTestBase::getInstance
30
	 *
31
	 * @param FormatterOptions|null $options
32
	 *
33
	 * @return QuantityFormatter
34
	 */
35
	protected function getInstance( FormatterOptions $options = null ) {
36
		return $this->getQuantityFormatter( $options );
37
	}
38
39
	/**
40
	 * @param FormatterOptions|null $options
41
	 * @param string|null $quantityWithUnitFormat
42
	 *
43
	 * @return QuantityFormatter
44
	 */
45
	private function getQuantityFormatter(
46
		FormatterOptions $options = null,
47
		$quantityWithUnitFormat = null
48
	) {
49
		$vocabularyUriFormatter = $this->getMock( 'ValueFormatters\ValueFormatter' );
50
		$vocabularyUriFormatter->expects( $this->any() )
51
			->method( 'format' )
52
			->will( $this->returnCallback( function( $unit ) {
53
				return $unit === '1' ? null : $unit;
54
			} ) );
55
56
		return new QuantityFormatter(
57
			$options,
58
			new DecimalFormatter( $options ),
59
			$vocabularyUriFormatter,
60
			$quantityWithUnitFormat
61
		);
62
	}
63
64
	/**
65
	 * @see ValueFormatterTestBase::validProvider
66
	 */
67
	public function validProvider() {
68
		$noMargin = new FormatterOptions( array(
69
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false
70
		) );
71
72
		$withMargin = new FormatterOptions( array(
73
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true
74
		) );
75
76
		$noRounding = new FormatterOptions( array(
77
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => true,
78
			QuantityFormatter::OPT_APPLY_ROUNDING => false
79
		) );
80
81
		$exactRounding = new FormatterOptions( array(
82
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
83
			QuantityFormatter::OPT_APPLY_ROUNDING => -2
84
		) );
85
86
		$forceSign = new FormatterOptions( array(
87
			QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
88
			DecimalFormatter::OPT_FORCE_SIGN => true,
89
		) );
90
91
		$noUnit = new FormatterOptions( array(
92
			QuantityFormatter::OPT_APPLY_UNIT => false,
93
		) );
94
95
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('+0/nm' => ... (array<*,array>) is incompatible with the return type declared by the abstract method ValueFormatters\Test\Val...TestBase::validProvider of type array[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
			'+0/nm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $noMargin ),
97
			'+0/wm' => array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), '0', $withMargin ),
98
99
			'+0.0/nm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $noMargin ),
100
			'+0.0/wm' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0±0.1 °', $withMargin ),
101
			'+0.0/xr' => array( QuantityValue::newFromNumber( '+0.0', '°', '+0.1', '-0.1' ), '0.0 °', $exactRounding ),
102
103
			'-1205/nm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1200 m', $noMargin ),
104
			'-1205/wm' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $withMargin ),
105
			'-1205/nr' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100 m', $noRounding ),
106
			'-1205/nu' => array( QuantityValue::newFromNumber( '-1205', 'm', '-1105', '-1305' ), '-1205±100', $noUnit ),
107
108
			'+3.025/nm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025', $noMargin ),
109
			'+3.025/wm' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.025±0.0039', $withMargin ),
110
			'+3.025/xr' => array( QuantityValue::newFromNumber( '+3.025', '1', '+3.02744', '+3.0211' ), '3.03', $exactRounding ),
111
112
			'+3.125/nr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
113
			'+3.125/xr' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '3.13', $exactRounding ),
114
115
			'+3.125/fs' => array( QuantityValue::newFromNumber( '+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
116
117
			// Rounding with a fixed +/-1 margin
118
			array( QuantityValue::newFromNumber( '+1.44', '1', '+2.44', '+0.44' ), '1', $noMargin ),
119
			array( QuantityValue::newFromNumber( '+1.45', '1', '+2.45', '+0.45' ), '1', $noMargin ),
120
			array( QuantityValue::newFromNumber( '+1.49', '1', '+2.49', '+0.49' ), '1', $noMargin ),
121
			array( QuantityValue::newFromNumber( '+1.50', '1', '+2.50', '+0.50' ), '2', $noMargin ),
122
			array( QuantityValue::newFromNumber( '+2.50', '1', '+3.50', '+1.50' ), '3', $noMargin ),
123
124
			// Rounding with different margins
125
			'1.55+/-0.09' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.64', '+1.46' ), '1.55', $noMargin ),
126
			'1.55+/-0.1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+1.65', '+1.45' ), '1.6', $noMargin ),
127
			'1.55+/-0.49' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.04', '+1.06' ), '1.6', $noMargin ),
128
			'1.55+/-0.5' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.05', '+1.05' ), '1.6', $noMargin ),
129
			'1.55+/-0.99' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.54', '+0.56' ), '1.6', $noMargin ),
130
			'1.55+/-1' => array( QuantityValue::newFromNumber( '+1.55', '1', '+2.55', '+0.55' ), '2', $noMargin ),
131
			// FIXME: We should probably never round to zero as it is confusing.
132
			'1.55+/-10' => array( QuantityValue::newFromNumber( '+1.55', '1', '+11.55', '-8.45' ), '0', $noMargin ),
133
134
			// Do not mess with the value when the margin is rendered
135
			array( QuantityValue::newFromNumber( '+1500', '1', '+2500', '+500' ), '1500±1000' ),
136
			array( QuantityValue::newFromNumber( '+2', '1', '+2.005', '+1.995' ), '2±0.005' ),
137
			array( QuantityValue::newFromNumber( '+1.5', '1', '+2.5', '+0.5' ), '1.5±1' ),
138
			array( QuantityValue::newFromNumber( '+1.0005', '1', '+1.0015', '+0.9995' ), '1.0005±0.001' ),
139
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.0025', '+0.0005' ), '0.0015±0.001' ),
140
141
			// Never mess with the margin
142
			array( QuantityValue::newFromNumber( '+2', '1', '+3.5', '+0.5' ), '2±1.5' ),
143
			array( QuantityValue::newFromNumber( '+2', '1', '+2.0015', '+1.9985' ), '2±0.0015' ),
144
			array( QuantityValue::newFromNumber( '+0.0015', '1', '+0.003', '+0' ), '0.0015±0.0015' ),
145
			array( QuantityValue::newFromNumber( '+2.0011', '1', '+2.0022', '+2' ), '2.0011±0.0011' ),
146
			array( QuantityValue::newFromNumber( '+2.0099', '1', '+2.0198', '+2' ), '2.0099±0.0099' ),
147
			array(
148
				QuantityValue::newFromNumber(
149
					'+1.00000000000000015',
150
					'1',
151
					'+1.00000000000000025',
152
					'+1.00000000000000005'
153
				),
154
				'1.00000000000000015±0.0000000000000001'
155
			),
156
		);
157
	}
158
159
	public function testFormatWithFormatString() {
160
		$formatter = $this->getQuantityFormatter( null, '<$2>$1' );
161
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
162
		$formatted = $formatter->format( $value );
163
		$this->assertSame( '<USD>5', $formatted );
164
	}
165
166
}
167