From 496509419ab7b579ff5748cd7abbe8375fc67971 Mon Sep 17 00:00:00 2001 From: Robert Snedeker Date: Thu, 10 Aug 2023 16:55:56 -0400 Subject: [PATCH 1/9] Add return type --- src/Num.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Num.php b/src/Num.php index 9868956..ef005b1 100644 --- a/src/Num.php +++ b/src/Num.php @@ -89,7 +89,8 @@ public static function guessDecimalSeparator(string $value): string return self::POINT; } - public static function canBeInteger($input) { + public static function canBeInteger($input): bool + { $cleanedInput = preg_replace("/[^0-9,.]/", "", $input); $dotCount = substr_count($cleanedInput, "."); From 2822903557804725161ef79f5d4bb774f9ba4b0b Mon Sep 17 00:00:00 2001 From: Robert Snedeker Date: Thu, 10 Aug 2023 16:58:48 -0400 Subject: [PATCH 2/9] Redundant else statement --- src/Num.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Num.php b/src/Num.php index ef005b1..84f92a6 100644 --- a/src/Num.php +++ b/src/Num.php @@ -19,12 +19,10 @@ public static function float(string $value, ?string $decimalSeparator = null): f $cleanedValue = preg_replace('/[^0-9' . preg_quote($decimalSeparator) . ']/', '', $value); if ($decimalSeparator === self::COMMA) { - $floatValue = (float) str_replace($decimalSeparator, self::POINT, $cleanedValue); - } else { - $floatValue = (float) $cleanedValue; + return (float) str_replace($decimalSeparator, self::POINT, $cleanedValue); } - - return $floatValue; + + return (float) $cleanedValue; } /** From 05c1c3d6bef5933886cb795d163249b91e589164 Mon Sep 17 00:00:00 2001 From: Robert Snedeker Date: Thu, 10 Aug 2023 17:00:02 -0400 Subject: [PATCH 3/9] Strict comparisons --- src/Num.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Num.php b/src/Num.php index 84f92a6..5ce7af6 100644 --- a/src/Num.php +++ b/src/Num.php @@ -41,13 +41,13 @@ public static function guessDecimalSeparator(string $value): string $pointCount = substr_count($value, self::POINT); $commaCount = substr_count($value, self::COMMA); - if ($pointCount == 0 && $commaCount == 0) { + if ($pointCount === 0 && $commaCount === 0) { return self::POINT; } $canBeInteger = self::canBeInteger($value); - if ($pointCount > 0 && $commaCount == 0) { + if ($pointCount > 0 && $commaCount === 0) { if(!$canBeInteger && $pointCount === 1) { return self::POINT; } @@ -57,7 +57,7 @@ public static function guessDecimalSeparator(string $value): string return ($pointCount > 1) ? self::COMMA : self::POINT; } - if ($commaCount > 0 && $pointCount == 0) { + if ($commaCount > 0 && $pointCount === 0) { if(!$canBeInteger && $commaCount === 1) { return self::COMMA; } From 0feaee252198476153eabc9a4dbc918656dc5027 Mon Sep 17 00:00:00 2001 From: Robert Snedeker Date: Thu, 10 Aug 2023 17:01:16 -0400 Subject: [PATCH 4/9] Consistent code styling --- src/Num.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Num.php b/src/Num.php index 5ce7af6..c851832 100644 --- a/src/Num.php +++ b/src/Num.php @@ -48,22 +48,26 @@ public static function guessDecimalSeparator(string $value): string $canBeInteger = self::canBeInteger($value); if ($pointCount > 0 && $commaCount === 0) { - if(!$canBeInteger && $pointCount === 1) { + if (!$canBeInteger && $pointCount === 1) { return self::POINT; } - if($canBeInteger && $pointCount === 1) { + + if ($canBeInteger && $pointCount === 1) { return self::COMMA; } + return ($pointCount > 1) ? self::COMMA : self::POINT; } if ($commaCount > 0 && $pointCount === 0) { - if(!$canBeInteger && $commaCount === 1) { + if (!$canBeInteger && $commaCount === 1) { return self::COMMA; } - if($canBeInteger && $commaCount === 1) { + + if ($canBeInteger && $commaCount === 1) { return self::POINT; } + return ($commaCount > 1) ? self::POINT : self::COMMA; } From c017ff89fc5360465755af767c4d1c1706ab9d67 Mon Sep 17 00:00:00 2001 From: Robert Snedeker Date: Thu, 10 Aug 2023 17:09:20 -0400 Subject: [PATCH 5/9] Simplified if statement --- src/Num.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Num.php b/src/Num.php index c851832..c726440 100644 --- a/src/Num.php +++ b/src/Num.php @@ -101,12 +101,9 @@ public static function canBeInteger($input): bool if ($dotCount + $commaCount === 1) { $dotPosition = strpos($cleanedInput, "."); $commaPosition = strpos($cleanedInput, ","); - - if ($dotCount === 1) { - $digitsAfterSeparator = substr($cleanedInput, $dotPosition + 1); - } else { - $digitsAfterSeparator = substr($cleanedInput, $commaPosition + 1); - } + + $pos = (($dotCount === 1) ? $dotPosition : $commaPosition) + 1; + $digitsAfterSeparator = substr($cleanedInput, $pos); $digitCount = strlen($digitsAfterSeparator); From bb4a8b3ca9b697ee9b8beadd26894c7fcca302a6 Mon Sep 17 00:00:00 2001 From: Command_String Date: Thu, 10 Aug 2023 17:39:33 -0400 Subject: [PATCH 6/9] prevent nesting --- src/Num.php | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/Num.php b/src/Num.php index 9868956..6e755ce 100644 --- a/src/Num.php +++ b/src/Num.php @@ -43,47 +43,34 @@ public static function guessDecimalSeparator(string $value): string $pointCount = substr_count($value, self::POINT); $commaCount = substr_count($value, self::COMMA); - if ($pointCount == 0 && $commaCount == 0) { + $hasComma = $commaCount > 0; + $hasPoint = $pointCount > 0; + + if (!$hasComma && !$hasPoint) { return self::POINT; } $canBeInteger = self::canBeInteger($value); if ($pointCount > 0 && $commaCount == 0) { - if(!$canBeInteger && $pointCount === 1) { - return self::POINT; - } - if($canBeInteger && $pointCount === 1) { - return self::COMMA; - } - return ($pointCount > 1) ? self::COMMA : self::POINT; + return (!$canBeInteger) ? self::POINT : self::COMMA; } if ($commaCount > 0 && $pointCount == 0) { - if(!$canBeInteger && $commaCount === 1) { - return self::COMMA; - } - if($canBeInteger && $commaCount === 1) { - return self::POINT; - } - return ($commaCount > 1) ? self::POINT : self::COMMA; + return (!$canBeInteger) ? self::COMMA : self::POINT; } - if ($pointCount < $commaCount) { - return self::POINT; - } elseif ($commaCount < $pointCount) { + if ($commaCount < $pointCount) { + return self::COMMA; + } + + $lastPointPosition = strrpos($value, self::POINT); + $lastCommaPosition = strrpos($value, self::COMMA); + + if ($lastPointPosition !== false && $lastCommaPosition !== false) { + return ($lastPointPosition > $lastCommaPosition) ? self::POINT : self::COMMA; + } elseif ($lastCommaPosition !== false) { return self::COMMA; - } else { - $lastPointPosition = strrpos($value, self::POINT); - $lastCommaPosition = strrpos($value, self::COMMA); - - if ($lastPointPosition !== false && $lastCommaPosition !== false) { - return ($lastPointPosition > $lastCommaPosition) ? self::POINT : self::COMMA; - } elseif ($lastPointPosition !== false) { - return self::POINT; - } elseif ($lastCommaPosition !== false) { - return self::COMMA; - } } return self::POINT; From 8b4898d8b357f3b22db6e4db89e98930b3bfbcd3 Mon Sep 17 00:00:00 2001 From: Command_String Date: Thu, 10 Aug 2023 17:48:26 -0400 Subject: [PATCH 7/9] add my changes back --- src/Num.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Num.php b/src/Num.php index c3bb709..764274c 100644 --- a/src/Num.php +++ b/src/Num.php @@ -19,12 +19,10 @@ public static function float(string $value, ?string $decimalSeparator = null): f $cleanedValue = preg_replace('/[^0-9' . preg_quote($decimalSeparator) . ']/', '', $value); if ($decimalSeparator === self::COMMA) { - $floatValue = (float) str_replace($decimalSeparator, self::POINT, $cleanedValue); - } else { - $floatValue = (float) $cleanedValue; + return (float)str_replace($decimalSeparator, self::POINT, $cleanedValue); } - return $floatValue; + return (float)$cleanedValue; } /** @@ -32,7 +30,7 @@ public static function float(string $value, ?string $decimalSeparator = null): f */ public static function int(string $value, ?string $decimalSeparator = null): int { - return (int) self::float($value, $decimalSeparator); + return (int)self::float($value, $decimalSeparator); } /** @@ -87,11 +85,8 @@ public static function canBeInteger($input): bool $dotPosition = strpos($cleanedInput, "."); $commaPosition = strpos($cleanedInput, ","); - if ($dotCount === 1) { - $digitsAfterSeparator = substr($cleanedInput, $dotPosition + 1); - } else { - $digitsAfterSeparator = substr($cleanedInput, $commaPosition + 1); - } + $pos = (($dotCount === 1) ? $dotPosition : $commaPosition) + 1; + $digitsAfterSeparator = substr($cleanedInput, $pos); $digitCount = strlen($digitsAfterSeparator); @@ -102,4 +97,4 @@ public static function canBeInteger($input): bool return true; } -} +} \ No newline at end of file From 91e6c0526bee5be0701169fa2903f3ce879e24ad Mon Sep 17 00:00:00 2001 From: Command_String Date: Thu, 10 Aug 2023 17:54:28 -0400 Subject: [PATCH 8/9] added script for running test --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 7d57882..9c2b275 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ }, "require-dev": { "phpunit/phpunit": "^10.1" + }, + "scripts": { + "test": "phpunit" } } \ No newline at end of file From b3bea1351177d98f5300596ca6745a2b397a7a49 Mon Sep 17 00:00:00 2001 From: Command_String Date: Thu, 10 Aug 2023 17:55:16 -0400 Subject: [PATCH 9/9] added workflow --- .github/workflows/php.yml | 41 +++++++++++++++++++++++++++++++++++++++ composer.json | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/php.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..0957990 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,41 @@ +name: PHP Composer + +on: + push: + branches: + - '**' + pull_request: + branches: + - '**' + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php: [ '8.1', '8.2' ] + + steps: + - uses: actions/checkout@v3 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + ini-values: xdebug.mode=develop,debug,coverage, + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: PHPUnit + run: composer tests \ No newline at end of file diff --git a/composer.json b/composer.json index 9c2b275..4dbcc2a 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ "phpunit/phpunit": "^10.1" }, "scripts": { - "test": "phpunit" + "tests": "phpunit" } } \ No newline at end of file