Page MenuHomeIn-Portal Phabricator

D509.id1392.diff
No OneTemporary

File Metadata

Created
Fri, Sep 26, 11:26 PM

D509.id1392.diff

Index: core/kernel/globals.php
===================================================================
--- core/kernel/globals.php
+++ core/kernel/globals.php
@@ -368,9 +368,15 @@
* @param bool $pounds_only
* @return float
* @access public
+ *
+ * @throws InvalidArgumentException When given kg argument, that should be numeric isn't numeric.
*/
public static function Kg2Pounds($kg, $pounds_only = false)
{
+ if ( !is_numeric($kg) ) {
+ throw new InvalidArgumentException('Given kg argument, that should be numeric isn\'t numeric.');
+ }
+
$major = floor( round($kg / self::POUND_TO_KG, 3) );
$minor = abs(round(($kg - $major * self::POUND_TO_KG) / self::POUND_TO_KG * 16, 2));
@@ -388,9 +394,15 @@
* @param float $ounces
* @return float
* @access public
+ *
+ * @throws InvalidArgumentException When either of a given arguments, that should be numeric aren't numeric.
*/
public static function Pounds2Kg($pounds, $ounces = 0.00)
{
+ if ( !is_numeric($pounds) || !is_numeric($ounces) ) {
+ throw new InvalidArgumentException('Either of a given arguments, that should be numeric aren\'t numeric.');
+ }
+
return round(($pounds + ($ounces / 16)) * self::POUND_TO_KG, 5);
}
Index: core/kernel/utility/formatters/unit_formatter.php
===================================================================
--- core/kernel/utility/formatters/unit_formatter.php
+++ core/kernel/utility/formatters/unit_formatter.php
@@ -87,7 +87,7 @@
return;
}
else {
- $value = kUtil::Pounds2Kg($major, $minor);
+ $value = kUtil::Pounds2Kg((float)$major, (float)$minor);
}
}
@@ -118,7 +118,7 @@
$minor = null;
}
else {
- list($major, $minor) = kUtil::Kg2Pounds($value);
+ list($major, $minor) = kUtil::Kg2Pounds((float)$value);
// $major = floor( $value / 0.5 );
// $minor = ($value - $major * 0.5) * 32;
}
@@ -128,4 +128,4 @@
}
}
}
-}
\ No newline at end of file
+}
Index: core/tests/Unit/kernel/kUtilTest.php
===================================================================
--- core/tests/Unit/kernel/kUtilTest.php
+++ core/tests/Unit/kernel/kUtilTest.php
@@ -56,4 +56,56 @@
);
}
+ /**
+ * @dataProvider kg2PoundsDataProvider
+ */
+ public function testKg2Pounds($kg, $pounds_only, $expected_pounds, $expected_ounces)
+ {
+ if ( is_numeric($expected_pounds) ) {
+ list($pounds, $ounces) = kUtil::Kg2Pounds($kg, $pounds_only);
+ $this->assertEquals($expected_pounds, $pounds);
+ $this->assertEquals($expected_ounces, $ounces);
+
+ return;
+ }
+
+ $this->expectException('InvalidArgumentException');
+ kUtil::Kg2Pounds($kg, $pounds_only);
+ }
+
+ public static function kg2PoundsDataProvider()
+ {
+ return array(
+ array(kUtil::POUND_TO_KG * 1.5, false, 1, 8),
+ array(kUtil::POUND_TO_KG * 1.5, true, 1.5, 0),
+ array(null, true, null, null),
+ );
+ }
+
+ /**
+ * @dataProvider pounds2KgDataProvider
+ */
+ public function testPounds2Kg($pounds, $ounces, $expected)
+ {
+ if ( is_numeric($expected) ) {
+ $this->assertEquals($expected, kUtil::Pounds2Kg($pounds, $ounces));
+
+ return;
+ }
+
+ $this->expectException('InvalidArgumentException');
+ kUtil::Pounds2Kg($pounds, $ounces);
+ }
+
+ public static function pounds2KgDataProvider()
+ {
+ return array(
+ array(1, 8, 0.68039),
+ array(1.5, 0, 0.68039),
+ array(null, 8, null),
+ array(1, null, null),
+ array(null, null, null),
+ );
+ }
+
}

Event Timeline