Skip to content

Commit d0950a6

Browse files
committed
Better number overflow handling
Also we check for edge cases within the unit tests.
1 parent 8530427 commit d0950a6

File tree

6 files changed

+68
-25
lines changed

6 files changed

+68
-25
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ env:
1818
# Define the php versions against we want to test our code
1919
php:
2020
- 7.0
21+
- 7.1
2122

2223
matrix:
2324
fast_finish: true
@@ -40,9 +41,9 @@ install:
4041
# Testing the app (see phpunit.xml), generating Code Coverage report
4142
script:
4243
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
43-
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
4444

45-
#after_failure:
45+
after_script:
46+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
4647

4748
# Tell Travis CI to monitor only 'master' branch
4849
branches:

src/Exception/NTWIndiaException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace NTWIndia\Exception;
3+
4+
/**
5+
* NTWIndiaInvalidNumber
6+
*
7+
* Exception raised within the NTWIndia class when invalid number is passed
8+
*/
9+
class NTWIndiaInvalidNumber extends \Exception {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace NTWIndia\Exception;
3+
4+
/**
5+
* NTWIndiaInvalidNumber
6+
*
7+
* Exception raised within the NTWIndia class when number is greater than acceptable value
8+
*/
9+
class NTWIndiaNumberOverflow extends \Exception {
10+
11+
}

src/NTWIndia.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ class NTWIndia {
123123
* The number supplied has to be greater than 0. Negative numbers aren't
124124
* supported.
125125
*
126-
* @param integer|float $number The number to convert
126+
* @param integer|float $number The number to
127+
* convert
127128
*
128-
* @throws NTWIndiaException When passed variable is not numeric
129+
* @throws Exception\NTWIndiaInvalidNumber When passed variable is not numeric
130+
* @throws Exception\NTWIndiaNumberOverflow When passed number is greater than system maximum
129131
*
130-
* @return string The covnerted value
132+
* @return string The covnerted value
131133
*/
132134
public function numToWord( $number ) {
133135
/**
@@ -136,7 +138,12 @@ public function numToWord( $number ) {
136138
* If not then log a warning
137139
*/
138140
if ( ! is_numeric( $number ) ) {
139-
throw new Exception\NTWIndiaException( 'Valid number not given' );
141+
throw new Exception\NTWIndiaInvalidNumber( 'Valid number not given.' );
142+
}
143+
144+
// Check if number is exceeding the system maximum
145+
if ( $number > PHP_INT_MAX ) {
146+
throw new Exception\NTWIndiaNumberOverflow( 'Number is greater than system maximum.' );
140147
}
141148

142149
// Convert to the absolute value
@@ -252,23 +259,24 @@ private function convertNumber( $number ) {
252259
* It can be called when you know the number is less than 100 to reduce
253260
* memory and calculation
254261
*
255-
* @param int $number The number
262+
* @param int $number The number
256263
*
257-
* @throws NTWIndia\Exception\NTWIndiaException When a valid number is not given
264+
* @throws Exception\NTWIndiaInvalidNumber When a valid number is not given
265+
* @throws Exception\NTWIndiaNumberOverflow When number is greater than 99
258266
*
259-
* @return string Word value of the number
267+
* @return string Word value of the number
260268
*/
261269
public function numToWordSmall( $number ) {
262270
// Check if number is numeric
263271
if ( ! is_numeric( $number ) ) {
264-
throw new Exception\NTWIndiaException( 'Valid number not given.' );
272+
throw new Exception\NTWIndiaInvalidNumber( 'Valid number not given.' );
265273
}
266274
$number = floor( abs( $number ) );
267275

268276
// Check if number is greater than 99
269-
// If so, then further breaking is needed
277+
// If so, then just throw an exception
270278
if ( $number > 99 ) {
271-
return $this->convertNumber( $number );
279+
throw new Exception\NTWIndiaNumberOverflow( 'Number is greater than 99. Use numToWord method.' );
272280
}
273281

274282
// Calculate the last character beforehand

tests/NTW_India_Test.php renamed to tests/NTWIndiaTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function testZeroValue() {
5151
$this->assertEquals( $word, $this->ntw->numToWord( $num ) );
5252
}
5353

54+
public function testFloatingValue() {
55+
$num = 100.24;
56+
$word = 'One Hundred And 24/100';
57+
$this->assertEquals( $word, $this->ntw->numToWord( $num ) );
58+
$num = 123.00;
59+
$word = 'One Hundred And Twenty Three';
60+
$this->assertEquals( $word, $this->ntw->numToWord( $num ) );
61+
}
62+
5463
public function testNumToWdSmall() {
5564
$num_to_wd = $this->getNumToWord();
5665
foreach ( $num_to_wd as $num => $wd ) {
@@ -59,19 +68,33 @@ public function testNumToWdSmall() {
5968
}
6069

6170
/**
62-
* @expectedException NTWIndia\Exception\NTWIndiaException
71+
* @expectedException NTWIndia\Exception\NTWIndiaInvalidNumber
6372
*/
6473
public function testNumToWordException() {
6574
$this->ntw->numToWord( 'foo' );
6675
}
6776

6877
/**
69-
* @expectedException NTWIndia\Exception\NTWIndiaException
78+
* @expectedException NTWIndia\Exception\NTWIndiaNumberOverflow
79+
*/
80+
public function testNumToWordOverflowException() {
81+
$this->ntw->numToWord( 92233720368547758070 );
82+
}
83+
84+
/**
85+
* @expectedException NTWIndia\Exception\NTWIndiaInvalidNumber
7086
*/
7187
public function testNumToWordSmallException() {
7288
$this->ntw->numToWordSmall( 'bar' );
7389
}
7490

91+
/**
92+
* @expectedException NTWIndia\Exception\NTWIndiaNumberOverflow
93+
*/
94+
public function testNumToWordSmallOverflowException() {
95+
$this->ntw->numToWordSmall( 200 );
96+
}
97+
7598
private function getNumToWord() {
7699
return array(
77100
'0' => 'Zero',

0 commit comments

Comments
 (0)