Skip to content

Commit

Permalink
Add QA tools and update code appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed Jun 16, 2023
1 parent 825722e commit c8216c8
Show file tree
Hide file tree
Showing 10 changed files with 3,767 additions and 22 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "PHPUnit tests"

on:
pull_request:
push:

jobs:
ci:
name: "CI checks"
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.2"
coverage: none
tools: composer:v2

- name: "Cache dependencies"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
vendor
key: "php-8.2"
restore-keys: "php-8.2"

- name: "Install dependencies"
run: "composer update --no-interaction --no-progress --no-suggest"

- name: "Lint"
run: ./vendor/bin/phplint NumberToWords.php test

- name: "PHPCS"
run: ./vendor/bin/phpcs

- name: "PHPStan"
run: ./vendor/bin/phpstan --configuration=phpstan.neon

- name: "PHPUnit"
run: ./vendor/bin/phpunit
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
vendor/
/vendor/
/coverage/
/.phplint.cache
/.phpunit.cache
/.phpunit.result.cache
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: *

list:
@grep -E '^[a-zA-Z%_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-24s\033[0m %s\n", $$1, $$2}'

check: lint cs phpstan test ## check lint, code style and static analysis using the PHP within Docker

lint:
./vendor/bin/phplint NumberToWords.php test

cs:
./vendor/bin/phpcs

phpstan:
./vendor/bin/phpstan --configuration=phpstan.neon

test:
XDEBUG_MODE=coverage php ./vendor/bin/phpunit --coverage-html coverage

41 changes: 20 additions & 21 deletions NumberToWords.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace NFNumberToWord;

use RuntimeException;
namespace NFNumberToWord;

class NumberToWords
{
protected $dictionary = array(
/**
* @var array|string[]
*/
protected array $dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
Expand Down Expand Up @@ -43,20 +45,22 @@ class NumberToWords
1000000000000000000 => 'quintillion'
);


public function toWords($number, $titleCase = false)
public function toWords(mixed $number, bool $titleCase = false): string|false
{
$dictionary = $this->dictionary;
if ($titleCase) {
array_walk($dictionary, function (&$item, $key) {
array_walk($dictionary, static function (&$item) {
$item = ucfirst($item);
});
}

return $this->convertNumbersToWords($number, $dictionary);
}

private function convertNumbersToWords($number, $dictionary = null)
/**
* @param String[] $dictionary
*/
private function convertNumbersToWords(mixed $number, array $dictionary): string|false
{
// From: http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

Expand All @@ -70,22 +74,17 @@ private function convertNumbersToWords($number, $dictionary = null)
return false;
}

if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
throw new RuntimeException(
'convertNumbersToWords only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX
);
}

if ($number < 0) {
return $negative . $this->convertNumbersToWords(abs($number), $dictionary);
}

$string = $fraction = null;
$fraction = null;

if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
if (str_contains((string)$number, '.')) {
[$number, $fraction] = explode('.', (string)$number);
$fraction = (int)$fraction;
}
$number = (int)$number;

switch (true) {
case $number < 21:
Expand All @@ -108,7 +107,7 @@ private function convertNumbersToWords($number, $dictionary = null)
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$baseUnit = 1000 ** floor(log($number, 1000));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = $this->convertNumbersToWords($numBaseUnits, $dictionary) . ' ' . $dictionary[$baseUnit];
Expand All @@ -119,11 +118,11 @@ private function convertNumbersToWords($number, $dictionary = null)
break;
}

if (null !== $fraction && is_numeric($fraction)) {
if (is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
foreach (str_split((string) $fraction) as $n) {
$words[] = $dictionary[$n];
}
$string .= implode(' ', $words);
}
Expand Down
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,17 @@
"classmap": [
"./NumberToWords.php"
]
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"phpstan/phpstan": "^1.10",
"overtrue/phplint": "^9.0",
"squizlabs/php_codesniffer": "^3.7",
"slevomat/coding-standard": "^8.12"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Loading

0 comments on commit c8216c8

Please sign in to comment.