Kohana_Jam_Field_Price::get()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.3888
c 0
b 0
f 0
cc 5
nc 2
nop 3
crap 5
1
<?php
2
3
use OpenBuildings\Monetary\Monetary;
4
5
/**
6
 * @package    Openbuildings\Jam
7
 * @author     Ivan Kerin <[email protected]>
8
 * @copyright  (c) 2013 OpenBuildings Ltd.
9
 * @license    http://spdx.org/licenses/BSD-3-Clause
10
 */
11
class Kohana_Jam_Field_Price extends Jam_Field_String {
12
13
	public $default = 0;
14
15
	public $allow_null = TRUE;
16
17
	public $convert_empty = TRUE;
18
19
	public $default_currency = 'GBP';
20
21
	public $default_display_currency = 'GBP';
22
23
	public $default_ceil_on_convert = FALSE;
24
25
	public static $autoload_from_model = array('currency', 'display_currency', 'monetary', 'ceil_on_convert');
26
27
	/**
28
	 * Casts to a string, preserving NULLs along the way.
29
	 *
30
	 * @param   mixed   $value
31
	 * @return  string
32
	 */
33 19
	public function set(Jam_Validated $model, $value, $is_changed)
34
	{
35 19
		list($value, $return) = $this->_default($model, $value);
0 ignored issues
show
Unused Code introduced by
The assignment to $return is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
36
37 19
		return $value;
38
	}
39
40
	/**
41
	 * Preserve nulls and 0 / 0.0 / '0' / '0.0' values
42
	 * @param  Jam_Validated $model
43
	 * @param  mixed        $value
44
	 * @return array
45
	 */
46 19
	protected function _default(Jam_Validated $model, $value)
47
	{
48 19
		$return = FALSE;
49
50 19
		$value = $this->run_filters($model, $value);
51 19
		if ((is_string($value) AND is_numeric($value)) OR is_int($value) ) {
52 9
			$value = (float) $value;
53
		}
54
55
		// Convert empty values to NULL, if needed
56 19
		if ($this->convert_empty AND empty($value) AND $value !== 0.0)
57
		{
58 5
			$value  = $this->empty_value;
59 5
			$return = TRUE;
60
		}
61
62
		// Allow NULL values to pass through untouched by the field
63 19
		if ($this->allow_null AND $value === NULL)
64
		{
65 6
			$value  = NULL;
66 6
			$return = TRUE;
67
		}
68
69 19
		return array($value, $return);
70
	}
71
72 2
	public function monetary()
73
	{
74 2
		return Monetary::instance();
75
	}
76
77
	/**
78
	 * convert to Jam_Price if not NULL
79
	 * @param  Jam_Validated $model
80
	 * @param  mixed        $value
81
	 * @param  boolean        $is_loaded
82
	 * @return Jam_Price
83
	 */
84 2
	public function get(Jam_Validated $model, $value, $is_loaded)
85
	{
86 2
		if ( ! ($value instanceof Jam_Price) AND $value !== NULL)
87
		{
88 2
			$value = new Jam_Price($value, $this->default_currency, $this->monetary(), $this->default_display_currency, $this->default_ceil_on_convert);
89
90 2
			foreach (static::$autoload_from_model as $autoload_method)
91
			{
92 2
				if (method_exists($model, $autoload_method))
93
				{
94 1
					$value->$autoload_method($model->$autoload_method());
95
				}
96
			}
97
		}
98
99 2
		return $value;
100
	}
101
}
102