-
-
Notifications
You must be signed in to change notification settings - Fork 536
Add length and weight conversion classes with input validation issue fixed #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
class LengthConversions | ||
{ | ||
public static function mToKm($m) | ||
{ | ||
if (!is_numeric($m)) { | ||
throw new \InvalidArgumentException("Invalid input for mToKm: expected numeric, got string ('{$m}')"); | ||
} | ||
return round($m / 1000, 6); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using magic numbers like this (example, 1000) - I'd prefer to use named class constants, e.g.
Same for the other magic numbers used in this file... |
||
} | ||
|
||
public static function kmToM($km) | ||
{ | ||
if (!is_numeric($km)) { | ||
throw new \InvalidArgumentException("Invalid input for kmToM: expected numeric, got string ('{$km}')"); | ||
} | ||
return round($km * 1000, 6); | ||
} | ||
|
||
public static function mToMiles($m) | ||
{ | ||
if (!is_numeric($m)) { | ||
throw new \InvalidArgumentException("Invalid input for mToMiles: expected numeric, got string ('{$m}')"); | ||
} | ||
return round($m * 0.000621373, 6); | ||
} | ||
|
||
public static function milesToM($miles) | ||
{ | ||
if (!is_numeric($miles)) { | ||
throw new \InvalidArgumentException("Invalid input for milesToM: expected numeric, got string ('{$miles}')"); | ||
} | ||
return round($miles * 1609.34, 6); | ||
} | ||
|
||
public static function inToCm($in) | ||
{ | ||
if (!is_numeric($in)) { | ||
throw new \InvalidArgumentException("Invalid input for inToCm: expected numeric, got string ('{$in}')"); | ||
} | ||
return round($in * 2.54, 6); | ||
} | ||
|
||
public static function cmToIn($cm) | ||
{ | ||
if (!is_numeric($cm)) { | ||
throw new \InvalidArgumentException("Invalid input for cmToIn: expected numeric, got string ('{$cm}')"); | ||
} | ||
return round($cm * 0.3937, 6); | ||
} | ||
|
||
public static function kmToMiles($km) | ||
{ | ||
if (!is_numeric($km)) { | ||
throw new \InvalidArgumentException("Invalid input for kmToMiles: expected numeric, got string ('{$km}')"); | ||
} | ||
return round($km * 0.621504, 6); | ||
} | ||
|
||
public static function milesToKm($miles) | ||
{ | ||
if (!is_numeric($miles)) { | ||
throw new \InvalidArgumentException("Invalid input for milesToKm: expected numeric, got string ('{$miles}')"); | ||
} | ||
return round($miles * 1.609, 6); | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PSR‑12 expects StudlyCaps class names and generally one class per file with matching filename (StudlyCaps). Please rename files to:
(Also update DIRECTORY.md entries to the exact casing.) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
class WeightConversions | ||
{ | ||
public static function kgToLbs($kg) | ||
{ | ||
if (!is_numeric($kg)) { | ||
throw new \InvalidArgumentException("Invalid input for kgToLbs: expected numeric, got string ('{$kg}')"); | ||
} | ||
return round($kg * 2.20462, 5); | ||
} | ||
|
||
public static function lbsToKg($lbs) | ||
{ | ||
if (!is_numeric($lbs)) { | ||
throw new \InvalidArgumentException("Invalid input for lbsToKg: expected numeric, got string ('{$lbs}')"); | ||
} | ||
return round($lbs * 0.453593, 5); | ||
} | ||
|
||
public static function gToKg($g) | ||
{ | ||
if (!is_numeric($g)) { | ||
throw new \InvalidArgumentException("Invalid input for gToKg: expected numeric, got string ('{$g}')"); | ||
} | ||
return round($g / 1000, 5); | ||
} | ||
|
||
public static function kgToG($kg) | ||
{ | ||
if (!is_numeric($kg)) { | ||
throw new \InvalidArgumentException("Invalid input for kgToG: expected numeric, got string ('{$kg}')"); | ||
} | ||
return round($kg * 1000, 5); | ||
} | ||
|
||
public static function ozToLbs($oz) | ||
{ | ||
if (!is_numeric($oz)) { | ||
throw new \InvalidArgumentException("Invalid input for ozToLbs: expected numeric, got string ('{$oz}')"); | ||
} | ||
return round($oz * 0.0625, 5); | ||
} | ||
|
||
public static function lbsToOz($lbs) | ||
{ | ||
if (!is_numeric($lbs)) { | ||
throw new \InvalidArgumentException("Invalid input for lbsToOz: expected numeric, got string ('{$lbs}')"); | ||
} | ||
return round($lbs * 16, 5); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PSR‑12 expects StudlyCaps class names and generally one class per file with matching filename (StudlyCaps). Please rename file to:
(Also update DIRECTORY.md entries to the exact casing.)