Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Conversions/Lengthconversions.php
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);
}

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);
}
}
52 changes: 52 additions & 0 deletions Conversions/Weightconversions.php
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);
}
}
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
* [Weightconversions](./Conversions/Weightconversions.php)
* [Lengthconversions](./Conversions/Lengthconversions.php)

## Datastructures
* Avltree
Expand Down
4 changes: 2 additions & 2 deletions Searches/TwoPointers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function twoPointers($list, $target)
// it means the sum is less than $target and we need to increase our sum
// to increase the sum we will move $left pointer to the right
$left++;
} else if ($list[$left] + $list[$right] > $target) {
} elseif ($list[$left] + $list[$right] > $target) {
// the sum is greater than the target, so we need to decrease the sum
// to decrease the sum we will move our $right pointer to the left
$right--;
} else if ($list[$left] + $list[$right] == $target) {
} elseif ($list[$left] + $list[$right] == $target) {
// if it's true, we have found a pair
$ans++;
// now we will move one of our pointers, otherwise it'll run forever
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.13"
},
"scripts": {
"test": "vendor/bin/phpunit tests"
}
}

107 changes: 107 additions & 0 deletions tests/Conversions/ConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
require_once __DIR__ . '/../../Conversions/TemperatureConversions.php';
require_once __DIR__ . '/../../Conversions/Weightconversions.php';
require_once __DIR__ . '/../../Conversions/Lengthconversions.php';

class ConversionsTest extends TestCase
{
Expand Down Expand Up @@ -142,4 +144,109 @@ public function testFahrenheitToKelvin()
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
FahrenheitToKelvin("non-numeric");
}
/**
* Helper method to test invalid input handling in conversion methods
*
* @param string $method The conversion method to test (e.g. 'WeightConversions::kgToLbs')
* @param mixed $value The invalid input value
* @param string $expectedMessage The expected exception message
*/
protected function assertInvalidInputConversion($method, $value, $expectedMessage)
{
try {
// Call the method with the invalid input
$parts = explode('::', $method);
$className = $parts[0];
$methodName = $parts[1];
$className::$methodName($value);

// If we get here, no exception was thrown
$this->fail("Expected exception was not thrown for $method with invalid input");
} catch (InvalidArgumentException $e) {
// Check if the exception message matches the expected message
$this->assertEquals($expectedMessage, $e->getMessage());
}
}
public function testKgToLbs()
{
$this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001);
$this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", "Invalid input for kgToLbs: expected numeric, got string ('invalid string')");
}

public function testLbsToKg()
{
$this->assertEquals(45.3593, WeightConversions::lbsToKg(100), 0.001);
$this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", "Invalid input for lbsToKg: expected numeric, got string ('invalid string')");
}

public function testGToKg()
{
$this->assertEquals(0.5, WeightConversions::gToKg(500), 0.001);
$this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", "Invalid input for gToKg: expected numeric, got string ('invalid string')");
}

public function testKgToG()
{
$this->assertEquals(1000, WeightConversions::kgToG(1), 0.001);
$this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", "Invalid input for kgToG: expected numeric, got string ('invalid string')");
}

public function testOzToLbs()
{
$this->assertEquals(3.5, WeightConversions::ozToLbs(56), 0.001);
$this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", "Invalid input for ozToLbs: expected numeric, got string ('invalid string')");
}

public function testLbsToOz()
{
$this->assertEquals(64, WeightConversions::lbsToOz(4), 0.001);
$this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", "Invalid input for lbsToOz: expected numeric, got string ('invalid string')");
}
public function testMToKm()
{
$this->assertEquals(1, LengthConversions::mToKm(1000), 0.001);
$this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", "Invalid input for mToKm: expected numeric, got string ('invalid string')");
}

public function testKmToM()
{
$this->assertEquals(5000, LengthConversions::kmToM(5), 0.001);
$this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", "Invalid input for kmToM: expected numeric, got string ('invalid string')");
}

public function testMToMiles()
{
$this->assertEquals(0.621373, LengthConversions::mToMiles(1000), 0.001);
$this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", "Invalid input for mToMiles: expected numeric, got string ('invalid string')");
}

public function testMilesToM()
{
$this->assertEquals(1609.34, LengthConversions::milesToM(1), 0.001);
$this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", "Invalid input for milesToM: expected numeric, got string ('invalid string')");
}

public function testInToCm()
{
$this->assertEquals(25.4, LengthConversions::inToCm(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", "Invalid input for inToCm: expected numeric, got string ('invalid string')");
}

public function testCmToIn()
{
$this->assertEquals(39.37, LengthConversions::cmToIn(100), 0.001);
$this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", "Invalid input for cmToIn: expected numeric, got string ('invalid string')");
}

public function testKmToMiles()
{
$this->assertEquals(6.21504, LengthConversions::kmToMiles(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", "Invalid input for kmToMiles: expected numeric, got string ('invalid string')");
}

public function testMilesToKm()
{
$this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001);
$this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", "Invalid input for milesToKm: expected numeric, got string ('invalid string')");
}
}