Skip to content

Commit 225c4be

Browse files
committed
Refactor LengthConversions and WeightConversions classes for improved code style
- Updated class definitions to follow PSR standards by adding opening braces on the same line. - Adjusted method definitions to align with coding conventions. - Ensured consistent formatting and spacing throughout the conversion methods.
1 parent b04c1df commit 225c4be

3 files changed

Lines changed: 50 additions & 32 deletions

File tree

Conversions/Lengthconversions.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
2+
23
/**
34
* Class for converting length between different units.
45
*/
5-
class LengthConversions {
6+
class LengthConversions
7+
{
68
/**
79
* Validates input for conversion methods
8-
*
10+
*
911
* @param mixed $value The value to check
1012
* @param string $method The method name for error message
1113
* @throws InvalidArgumentException
1214
*/
13-
private static function validateInput($value, $method) {
15+
private static function validateInput($value, $method)
16+
{
1417
// Make sure we have a numeric value
1518
if (!is_numeric($value)) {
1619
throw new InvalidArgumentException("Invalid input for $method: expected numeric, got " . gettype($value) . " (" . var_export($value, true) . ")");
@@ -23,85 +26,93 @@ private static function validateInput($value, $method) {
2326
* @param float $meters The length in meters.
2427
* @return float The equivalent length in kilometers.
2528
*/
26-
public static function mToKm($meters) {
29+
public static function mToKm($meters)
30+
{
2731
self::validateInput($meters, 'mToKm');
2832
return round($meters / 1000, 4);
2933
}
30-
34+
3135
/**
3236
* Converts kilometers to meters.
3337
*
3438
* @param float $kilometers The length in kilometers.
3539
* @return float The equivalent length in meters.
3640
*/
37-
public static function kmToM($kilometers) {
41+
public static function kmToM($kilometers)
42+
{
3843
self::validateInput($kilometers, 'kmToM');
3944
return round($kilometers * 1000, 4);
4045
}
41-
46+
4247
/**
4348
* Converts meters to miles.
4449
*
4550
* @param float $meters The length in meters.
4651
* @return float The equivalent length in miles.
4752
*/
48-
public static function mToMiles($meters) {
53+
public static function mToMiles($meters)
54+
{
4955
self::validateInput($meters, 'mToMiles');
5056
return round($meters / 1609.34, 6);
5157
}
52-
58+
5359
/**
5460
* Converts miles to meters.
5561
*
5662
* @param float $miles The length in miles.
5763
* @return float The equivalent length in meters.
5864
*/
59-
public static function milesToM($miles) {
65+
public static function milesToM($miles)
66+
{
6067
self::validateInput($miles, 'milesToM');
6168
return round($miles * 1609.34, 4);
6269
}
63-
70+
6471
/**
6572
* Converts inches to centimeters.
6673
*
6774
* @param float $inches The length in inches.
6875
* @return float The equivalent length in centimeters.
6976
*/
70-
public static function inToCm($inches) {
77+
public static function inToCm($inches)
78+
{
7179
self::validateInput($inches, 'inToCm');
7280
return round($inches * 2.54, 4);
7381
}
74-
82+
7583
/**
7684
* Converts centimeters to inches.
7785
*
7886
* @param float $centimeters The length in centimeters.
7987
* @return float The equivalent length in inches.
8088
*/
81-
public static function cmToIn($centimeters) {
89+
public static function cmToIn($centimeters)
90+
{
8291
self::validateInput($centimeters, 'cmToIn');
8392
return round($centimeters / 2.54, 2);
8493
}
85-
94+
8695
/**
8796
* Converts kilometers to miles.
8897
*
8998
* @param float $km The length in kilometers.
9099
* @return float The equivalent length in miles.
91100
*/
92-
public static function kmToMiles($km) {
101+
public static function kmToMiles($km)
102+
{
93103
self::validateInput($km, 'kmToMiles');
94104
return round($km / 1.609, 5);
95105
}
96-
106+
97107
/**
98108
* Converts miles to kilometers.
99109
*
100110
* @param float $miles The length in miles.
101111
* @return float The equivalent length in kilometers.
102112
*/
103-
public static function milesToKm($miles) {
113+
public static function milesToKm($miles)
114+
{
104115
self::validateInput($miles, 'milesToKm');
105116
return round($miles * 1.609, 4);
106117
}
107-
}
118+
}

Conversions/Weightconversions.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
2+
23
/**
34
* Class for converting weight between different units.
45
*/
5-
class WeightConversions {
6+
class WeightConversions
7+
{
68
/**
79
* Validates input for conversion methods
8-
*
10+
*
911
* @param mixed $value The value to check
1012
* @param string $method The method name for error message
1113
* @throws InvalidArgumentException
1214
*/
13-
private static function validateInput($value, $method) {
15+
private static function validateInput($value, $method)
16+
{
1417
// Make sure we have a numeric value
1518
if (!is_numeric($value)) {
1619
throw new InvalidArgumentException("Invalid input for $method");
@@ -24,7 +27,8 @@ private static function validateInput($value, $method) {
2427
* @return float The equivalent weight in pounds.
2528
* @see https://en.wikipedia.org/wiki/Kilogram
2629
*/
27-
public static function kgToLbs($kg) {
30+
public static function kgToLbs($kg)
31+
{
2832
self::validateInput($kg, 'kgToLbs');
2933
return round($kg * 2.20462, 4);
3034
}
@@ -36,7 +40,8 @@ public static function kgToLbs($kg) {
3640
* @return float The equivalent weight in kilograms.
3741
* @see https://en.wikipedia.org/wiki/Pound_(mass)
3842
*/
39-
public static function lbsToKg($lbs) {
43+
public static function lbsToKg($lbs)
44+
{
4045
self::validateInput($lbs, 'lbsToKg');
4146
return round($lbs / 2.20462, 4);
4247
}
@@ -47,7 +52,8 @@ public static function lbsToKg($lbs) {
4752
* @param float $grams The weight in grams.
4853
* @return float The equivalent weight in kilograms.
4954
*/
50-
public static function gToKg($grams) {
55+
public static function gToKg($grams)
56+
{
5157
self::validateInput($grams, 'gToKg');
5258
return round($grams / 1000, 4);
5359
}
@@ -58,7 +64,8 @@ public static function gToKg($grams) {
5864
* @param float $kg The weight in kilograms.
5965
* @return float The equivalent weight in grams.
6066
*/
61-
public static function kgToG($kg) {
67+
public static function kgToG($kg)
68+
{
6269
self::validateInput($kg, 'kgToG');
6370
return round($kg * 1000, 4);
6471
}
@@ -69,7 +76,8 @@ public static function kgToG($kg) {
6976
* @param float $oz The weight in ounces.
7077
* @return float The equivalent weight in pounds.
7178
*/
72-
public static function ozToLbs($oz) {
79+
public static function ozToLbs($oz)
80+
{
7381
self::validateInput($oz, 'ozToLbs');
7482
return round($oz / 16, 4);
7583
}
@@ -80,8 +88,9 @@ public static function ozToLbs($oz) {
8088
* @param float $lbs The weight in pounds.
8189
* @return float The equivalent weight in ounces.
8290
*/
83-
public static function lbsToOz($lbs) {
91+
public static function lbsToOz($lbs)
92+
{
8493
self::validateInput($lbs, 'lbsToOz');
8594
return round($lbs * 16, 4);
8695
}
87-
}
96+
}

tests/Conversions/ConversionsTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function assertInvalidInputConversion($method, $value, $expectedMessag
159159
$className = $parts[0];
160160
$methodName = $parts[1];
161161
$className::$methodName($value);
162-
162+
163163
// If we get here, no exception was thrown
164164
$this->fail("Expected exception was not thrown for $method with invalid input");
165165
} catch (InvalidArgumentException $e) {
@@ -249,6 +249,4 @@ public function testMilesToKm()
249249
$this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001);
250250
$this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", 'Invalid input for milesToKm');
251251
}
252-
253-
254252
}

0 commit comments

Comments
 (0)