GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (10bb61)
by Rudie
02:14
created

Quantity::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 6
eloc 10
nc 5
nop 3
1
<?php
2
3
namespace rdx\units;
4
5
use rdx\units\exceptions\ConversionException;
6
7
abstract class Quantity {
8
9
	// The sub class must set these, since every Quantity has different units
10
	// const BASE_UNIT = 'l';
11
12
	// static public $default_unit = self::BASE_UNIT;
1 ignored issue
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
13
	// static public $units = array();
1 ignored issue
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
15
	/**
16
	 * Convert without the bother of an object
17
	 */
18
	static public function convert( $amount, $fromUnit, $toUnit ) {
19
		$quantity = new static($amount, $fromUnit);
20
		return $quantity->to($toUnit);
21
	}
22
23
	/**
24
	 * Check if the given unit is valid for this quantity
25
	 */
26
	static public function validUnit( $unit ) {
27
		$units = static::$units;
28
		if ( isset($units[0]) ) {
29
			return in_array($unit, $units);
30
		}
31
32
		return isset($units[$unit]);
33
	}
34
35
36
37
	public $original_unit = '';
38
	public $unit = '';
39
	public $amount = 0;
40
41
	/**
42
	 *
43
	 */
44
	public function __construct( $amount, $unit = null, $convertToDefault = true ) {
45
		// Invalid unit!
46
		if ( $unit && !static::validUnit($unit) ) {
47
			$quantity = (new \ReflectionClass(get_called_class()))->getShortName();
48
			throw new ConversionException("Can't import '$quantity' as '$unit'.");
49
		}
50
51
		$this->amount = $amount;
52
		$this->unit = $unit ?: $this::$default_unit;
53
54
		$this->original_unit = $this->unit;
55
56
		// Convert incoming unit into storage unit
57
		if ( $convertToDefault && $this->unit != $this::$default_unit ) {
58
			$this->amount = $this->to($this::$default_unit);
59
			$this->unit = $this::$default_unit;
60
		}
61
	}
62
63
	/**
64
	 * Convert object amount to another unit, and return, don't save
65
	 */
66
	public function to( $toUnit ) {
67
		if ( $toUnit === 'base' ) {
68
			$toUnit = static::BASE_UNIT;
69
		}
70
71
		return $this->convertor($toUnit);
72
	}
73
74
	/**
75
	 * The convertor heart, that every sub class needs
76
	 */
77
	abstract protected function convertor( $toUnit );
78
79
	/**
80
	 * The default convertor, using the Quantity's conversion table
81
	 */
82
	protected function convertUsingTable( $toUnit ) {
83
		// Invalid unit!
84
		if ( !$this::validUnit($toUnit) ) {
85
			$quantity = (new \ReflectionClass($this))->getShortName();
86
			throw new ConversionException("Can't convert '$quantity' to '$toUnit'.");
87
		}
88
89
		$amount = $this->amount;
90
91
		// First convert from `$this->unit` to `BASE_UNIT`
92
		$factor = $this::$units[$this->unit];
93
		$amount = $factor < 0 ? -$factor / $amount : $amount * $factor;
94
95
		// Then convert from `BASE_UNIT` to `$toUnit`
96
		$factor = $this::$units[$toUnit];
97
		$amount = $factor < 0 ? -$factor / $amount : $amount / $factor;
98
99
		return $amount;
100
	}
101
102
	/**
103
	 * Advanded convertor, using unit-base-unit methods
104
	 */
105
	protected function convertUsingMethods( $toUnit ) {
106
		$amount = $this->amount;
107
108
		if ( $this->unit != $this::BASE_UNIT ) {
109
			$amount = call_user_func(array($this, $this->unit . 'to' . $this::BASE_UNIT), $amount);
110
		}
111
112
		if ( $this::BASE_UNIT != $toUnit ) {
113
			$amount = call_user_func(array($this, $this::BASE_UNIT . 'to' . $toUnit), $amount);
114
		}
115
116
		return $amount;
117
	}
118
119
	/**
120
	 * Convert the object to another standard unit
121
	 */
122
	public function convertTo( $unit ) {
123
		$this->amount = $this->to($unit);
124
		$this->unit = $unit;
125
	}
126
127
	/**
128
	 * Return this quantity in all known units
129
	 */
130
	public function all() {
131
		$all = array();
132
		foreach ( $this::$units as $unit => $conversion ) {
133
			if ( is_int($unit) ) {
134
				$unit = $conversion;
135
			}
136
137
			$all[$unit] = $this->to($unit);
138
		}
139
140
		return $all;
141
	}
142
143
}
144