Skip to content

Commit 235b559

Browse files
authored
Minor bugfixes. (#8)
* Strict types. * Minor bug fix. * Add test for exception. * Update readme. * Update travis build process.
1 parent cd21b37 commit 235b559

8 files changed

+69
-18
lines changed

.travis.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
language: php
2+
23
php:
34
- '7.1'
45
- '7.2'
5-
before_script: composer install
6+
7+
install: composer install --no-interaction --classmap-authoritative
8+
69
script: vendor/bin/phpunit
10+
11+
notifications:
12+
email: false
13+
14+
cache:
15+
directories:
16+
- $HOME/.composer

README.md

+25-11
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,39 @@ composer require nekman/luhn-algorithm
1616

1717
## Usage
1818

19-
Use the class like this:
20-
19+
In order to instantiate a new instance of the library, use the factory:
20+
21+
```php
22+
use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
23+
24+
$luhn = LuhnAlgorithmFactory::create();
25+
```
26+
27+
You can find [the public interface of the library in the `LuhnAlgorithmInterface`](src/Contract/LuhnAlgorithmInterface.php).
28+
29+
[The `Number` class](src/Number.php) is a container class that holds the actual number and the check digit. It does no validation
30+
nor does it calculate the check digit. To simplify the process of validating a number you can use the
31+
named constructor `Number::fromString()` like this:
32+
2133
```php
22-
use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;
2334
use Nekman\LuhnAlgorithm\Number;
2435

25-
$luhn = LuhnAlgorithmFactory::create();
36+
// Assume $creditCard is from a form.
37+
$number = Number::fromString($creditCard);
2638

27-
// Validate a credit card number entered in a form.
28-
$ccNumber = Number::fromString($creditCard);
29-
if ($luhn->isValid($ccNumber)) {
30-
// Credit card number is valid.
39+
if ($luhn->isValid($number)) {
40+
// Number is valid.
3141
}
42+
```
43+
44+
Alternatively, if you want to calculate the checksum or check digit for a number:
45+
46+
```php
47+
use Nekman\LuhnAlgorithm\Number;
3248

33-
// These methods are used internally by the library. You're free
34-
// to make use of them as well.
3549
$number = new Number(12345);
3650

3751
$checksum = $luhn->calcChecksum($number);
3852

3953
$checkDigit = $luhn->calcCheckDigit($number);
40-
```
54+
```

src/Contract/LuhnAlgorithmInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
declare(strict_types=1);
27+
2628
namespace Nekman\LuhnAlgorithm\Contract;
2729

2830
/**

src/Contract/NumberInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
declare(strict_types=1);
27+
2628
namespace Nekman\LuhnAlgorithm\Contract;
2729

2830
/**

src/LuhnAlgorithm.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
declare(strict_types=1);
27+
2628
namespace Nekman\LuhnAlgorithm;
2729

2830
use Nekman\LuhnAlgorithm\Contract\LuhnAlgorithmInterface;
@@ -39,7 +41,7 @@ class LuhnAlgorithm implements LuhnAlgorithmInterface
3941
public function isValid(NumberInterface $number): bool
4042
{
4143
if ($number->getCheckDigit() === null) {
42-
throw new \InvalidArgumentException("Check digit cannot be null.");
44+
throw new \InvalidArgumentException("Check digit is null.");
4345
}
4446

4547
$checksum = $this->calcChecksum($number);
@@ -71,16 +73,17 @@ public function calcCheckDigit(NumberInterface $number): int
7173
public function calcChecksum(NumberInterface $number): int
7274
{
7375
$number = (string) $number->getNumber();
74-
$nDigits = strlen($number);
75-
$checksum = 0;
76+
// Need to account for the check digit.
77+
$nDigits = strlen($number) + 1;
7678
$parity = $nDigits % 2;
79+
$checksum = 0;
7780

78-
for ($i = 0; $i < $nDigits; $i++) {
81+
for ($i = 0; $i < $nDigits - 1; $i++) {
7982
$digit = (int) $number[$i];
8083

81-
// Every other digit, starting from the leftmost,
84+
// Every other digit, starting from the rightmost,
8285
// shall be doubled.
83-
if (($i % 2) !== $parity) {
86+
if (($i % 2) === $parity) {
8487
$digit *= 2;
8588

8689
if ($digit > 9) {

src/LuhnAlgorithmFactory.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
declare(strict_types=1);
27+
2628
namespace Nekman\LuhnAlgorithm;
2729

2830
use Nekman\LuhnAlgorithm\Contract\LuhnAlgorithmInterface;

src/Number.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26+
declare(strict_types=1);
27+
2628
namespace Nekman\LuhnAlgorithm;
2729

2830
use Nekman\LuhnAlgorithm\Contract\NumberInterface;

tests/LuhnAlgorithmTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ public function provideIsValid_success()
6060
];
6161
}
6262

63+
/**
64+
* @dataProvider provideIsValid_failure
65+
*/
66+
public function testIsValid_failure($number, $exception)
67+
{
68+
$this->expectException($exception);
69+
$this->luhn->isValid($number);
70+
}
71+
72+
public function provideIsValid_failure()
73+
{
74+
return [
75+
[new Number(123, null), \InvalidArgumentException::class],
76+
];
77+
}
78+
6379
/**
6480
* @dataProvider provideCalcChecksum_success
6581
*/

0 commit comments

Comments
 (0)