Skip to content

Commit

Permalink
bug: Fix CI alerts from Psalm and PHPCS. Use the float casting and th…
Browse files Browse the repository at this point in the history
…e is_numeric function to separate cases.

Signed-off-by: codisart <louis.celier@gmail.com>
  • Loading branch information
codisart committed Feb 21, 2024
1 parent 7aed602 commit 4923b71
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 0 additions & 4 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1513,10 +1513,6 @@
</RedundantConditionGivenDocblockType>
</file>
<file src="src/GpsPoint.php">
<DocblockTypeContradiction>
<code>$value === false || $value === null</code>
<code>$value === null</code>
</DocblockTypeContradiction>
<InvalidOperand>
<code>$matches[1][0]</code>
<code>$matches[2][0]</code>
Expand Down
13 changes: 10 additions & 3 deletions src/GpsPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laminas\Validator;

use function explode;
use function is_numeric;
use function preg_match;
use function preg_match_all;
use function preg_replace;
Expand Down Expand Up @@ -53,12 +54,18 @@ private function isValidCoordinate(string $value, float $maxBoundary): bool
$value = $this->removeDegreeSign($value);
}

if ($value === false || $value === null || !is_numeric($value)) {
if ($value === false) {
$this->error(self::CONVERT_ERROR);
return false;
}

if (!$this->isValueInbound((float) $value, $maxBoundary)) {
$castedValue = (float) $value;
if (! is_numeric($value) && $castedValue === 0.0) {
$this->error(self::CONVERT_ERROR);
return false;
}

if (! $this->isValueInbound($castedValue, $maxBoundary)) {
$this->error(self::OUT_OF_BOUNDS);
return false;
}
Expand Down Expand Up @@ -100,6 +107,6 @@ private function isValueInbound(float $value, float $boundary): bool
{
$max = $boundary;
$min = -1 * $boundary;
return ($min <= $value && $value <= $max);
return $min <= $value && $value <= $max;
}
}
3 changes: 2 additions & 1 deletion test/GPSPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ public static function basicDataProvider(): array
public static function errorMessageTestValues(): array
{
return [
['63 47 24.691 N, 18 2 54.363 W', GpsPoint::CONVERT_ERROR, '63 47 24.691 N'],
['63 47 24.691 N, 18 2 54.363 W', GpsPoint::OUT_OF_BOUNDS, '63 47 24.691 N'],
['65° 4\' N,-22.728867530822754', GpsPoint::CONVERT_ERROR, '65° 4\' N'],
['° \' " N,° \' " E', GpsPoint::CONVERT_ERROR, '° \' " N'],
['° \' " N', GpsPoint::INCOMPLETE_COORDINATE, '° \' " N'],
['foo,bar', GpsPoint::CONVERT_ERROR, 'foo'],
[' ,-22.4', GpsPoint::CONVERT_ERROR, ' '],
];
}
}

0 comments on commit 4923b71

Please sign in to comment.