Skip to content

Add weight and length conversion functions - #183

Open
haj8110 wants to merge 4 commits into
TheAlgorithms:masterfrom
haj8110:new-feature-branch
Open

haj8110 wants to merge 4 commits into
TheAlgorithms:masterfrom
haj8110:new-feature-branch

Conversation

@haj8110

@haj8110 haj8110 commented Mar 18, 2025

Copy link
Copy Markdown

No description provided.

@darwinz darwinz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Please run the linter by running vendor/bin/phpcs -n. There are a few linter errors that need to be addressed. Thanks!

Comment thread Conversions/Lengthconversions.php
Comment thread Conversions/Lengthconversions.php Outdated
Co-authored-by: Brandon Johnson <bbj1979@gmail.com>
@darwinz

darwinz commented Apr 28, 2025

Copy link
Copy Markdown
Contributor

@haj8110, please run the linter on your branch and fix the errors.

vendor/bin/phpcs -n

@darwinz

darwinz commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

@haj8110 I can't merge until you get the linter to pass. Are you able to fix the errors?

… 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.

@haj8110 haj8110 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update and remove the error

@darwinz darwinz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the lint errors, phpcs -n passes now!

I merged this against current master locally and ran composer test: 27 tests, 8 failures. All of the LengthConversions tests fail on the exception message. I've left inline comments with the details; the main items are the failing tests, the assertion tolerance, the inconsistent mile constants, and matching the repo conventions from the PHP 8.2 modernization (strict types, typed signatures, file names).

You can check locally with composer test and vendor/bin/phpcs -n. Thanks!

@@ -0,0 +1,118 @@
<?php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PHP 8.2 modernization (#190), every file in Conversions/ starts with declare(strict_types=1);. Please add it here, and add parameter/return types to the methods (e.g. public static function mToKm(float|int $meters): float).

Also, please rename this file to LengthConversions.php so it matches the class name and the existing naming (TemperatureConversions.php, SpeedConversion.php).

{
// Make sure we have a numeric value
if (!is_numeric($value)) {
throw new InvalidArgumentException("Invalid input for $method: expected numeric, got " . gettype($value) . " (" . var_export($value, true) . ")");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message doesn't match what the tests expect ('Invalid input for mToKm'), so all 8 LengthConversions tests fail when I run composer test against master. Please use the same message as WeightConversions::validateInput.

public static function mToMiles($meters)
{
self::validateInput($meters, 'mToMiles');
return round($meters / 1609.34, 6);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mile constant here (1609.34 m) disagrees with the one in kmToMiles/milesToKm (1.609 km), so mToMiles(1000) returns 0.621373 while kmToMiles(1) returns 0.621504. The exact value is 1609.344 m. Could you define it once as a class constant (e.g. private const METERS_PER_MILE = 1609.344;) and use it in all four mile conversions?

public static function cmToIn($centimeters)
{
self::validateInput($centimeters, 'cmToIn');
return round($centimeters / 2.54, 2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rounding to 2 decimals loses a lot of precision (cmToIn(1) returns 0.39). The precision also varies across methods (2, 4, 5, 6). I'd suggest dropping the round() calls and letting the tests compare with a delta, or at least using one precision everywhere.

public static function kmToMiles($km)
{
self::validateInput($km, 'kmToMiles');
return round($km / 1.609, 5);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.609 should be 1.609344 (see the comment on mToMiles).

@@ -0,0 +1,96 @@
<?php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the length file: please add declare(strict_types=1); and parameter/return types, and rename the file to WeightConversions.php to match the class name.

* @param mixed $value The invalid input value
* @param string $expectedMessage The expected exception message
*/
protected function assertInvalidInputConversion($method, $value, $expectedMessage)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper is more machinery than we need. The rest of this file uses $this->expectException(...) / $this->expectExceptionMessage(...), so a separate small test per invalid-input case would be more consistent.

}
public function testKgToLbs()
{
$this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHPUnit 10/11 the third argument of assertEquals is the failure message, not a delta, so 0.001 isn't a tolerance here. These are exact float comparisons that only pass because the rounded outputs happen to match. Please switch these to assertEqualsWithDelta($expected, $actual, 0.001) (this applies to every conversion assertion below too).

Comment thread DIRECTORY.md
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
* [Weightconversions](./Conversions/Weightconversions.php)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the DIRECTORY.md changes. That file is regenerated automatically by the directory workflow after merge.

@darwinz darwinz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking this as changes requested so it isn't merged before the items in my review above are addressed. Please see the inline comments there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants