Skip to content

Commit dd9c761

Browse files
committed
Code review changes.
1 parent 71dd3bd commit dd9c761

11 files changed

+458
-425
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
index.php
22
nbproject/
33
.idea/
4+
.php_cs.cache
45

56
# Created by http://www.gitignore.io
67

.php_cs.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRules([
5+
'@PSR2' => true,
6+
'ordered_imports' => true,
7+
'phpdoc_order' => true,
8+
'simplified_null_return' => false,
9+
'no_unused_imports' => true,
10+
])->setFinder(
11+
PhpCsFixer\Finder::create()
12+
->in(__DIR__.'/src')
13+
->in(__DIR__.'/tests')
14+
);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"php": "^7.1"
3535
},
3636
"require-dev": {
37-
"phpunit/phpunit": "^6.5"
37+
"phpunit/phpunit": "^6.5",
38+
"friendsofphp/php-cs-fixer": "^2.10"
3839
}
3940
}
Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
1-
<?php
2-
3-
/*
4-
* The MIT License (MIT)
5-
*
6-
* Copyright (c) 2014 Niklas Ekman
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9-
* this software and associated documentation files (the "Software"), to deal in
10-
* the Software without restriction, including without limitation the rights to
11-
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12-
* the Software, and to permit persons to whom the Software is furnished to do so,
13-
* subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in all
16-
* copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20-
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21-
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22-
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23-
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
25-
26-
namespace Nekman\LuhnAlgorithm\Contract;
27-
28-
/**
29-
* Handles the Luhn Algorithm.
30-
*
31-
* @link http://en.wikipedia.org/wiki/Luhn_algorithm
32-
*/
33-
interface LuhnAlgorithmInterface {
34-
/**
35-
* Determine if a number is valid according to the Luhn Algorithm.
36-
*
37-
* @param NumberInterface $number The number to validate.
38-
*
39-
* @return bool true if number is valid, false otherwise.
40-
*/
41-
public function isValid(NumberInterface $number): bool;
42-
43-
/**
44-
* Calculate the check digit for an input.
45-
*
46-
* @param NumberInterface $number The number, without check digit, to calculate the check digit for.
47-
*
48-
* @return int The check digit.
49-
*
50-
*/
51-
public function calcCheckDigit(NumberInterface $number): int;
52-
53-
/**
54-
* Calulates the checksum for number.
55-
*
56-
* @param NumberInterface $number The number, without check digit, to calculate the checksum for.
57-
*
58-
* @return int The checksum.
59-
*
60-
*/
61-
public function calcChecksum(NumberInterface $number): int;
62-
}
1+
<?php
2+
3+
/*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Niklas Ekman
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
* this software and associated documentation files (the "Software"), to deal in
10+
* the Software without restriction, including without limitation the rights to
11+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12+
* the Software, and to permit persons to whom the Software is furnished to do so,
13+
* subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
namespace Nekman\LuhnAlgorithm\Contract;
27+
28+
/**
29+
* Handles the Luhn Algorithm.
30+
*
31+
* @link http://en.wikipedia.org/wiki/Luhn_algorithm
32+
*/
33+
interface LuhnAlgorithmInterface
34+
{
35+
/**
36+
* Determine if a number is valid according to the Luhn Algorithm.
37+
*
38+
* @param NumberInterface $number The number to validate.
39+
*
40+
* @return bool true if number is valid, false otherwise.
41+
*/
42+
public function isValid(NumberInterface $number): bool;
43+
44+
/**
45+
* Calculate the check digit for an input.
46+
*
47+
* @param NumberInterface $number The number to calculate the check digit for.
48+
*
49+
* @return int The check digit.
50+
*
51+
*/
52+
public function calcCheckDigit(NumberInterface $number): int;
53+
54+
/**
55+
* Calulates the checksum for number.
56+
*
57+
* @param NumberInterface $number The number to calculate the checksum for.
58+
*
59+
* @return int The checksum.
60+
*
61+
*/
62+
public function calcChecksum(NumberInterface $number): int;
63+
}

src/Contract/NumberInterface.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
<?php
2-
3-
/*
4-
* The MIT License (MIT)
5-
*
6-
* Copyright (c) 2014 Niklas Ekman
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9-
* this software and associated documentation files (the "Software"), to deal in
10-
* the Software without restriction, including without limitation the rights to
11-
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12-
* the Software, and to permit persons to whom the Software is furnished to do so,
13-
* subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in all
16-
* copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20-
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21-
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22-
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23-
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
25-
26-
namespace Nekman\LuhnAlgorithm\Contract;
27-
28-
/**
29-
* Describes the structure of a number in the Luhn Algorithm.
30-
*/
31-
interface NumberInterface
32-
{
33-
/**
34-
* Get the number, without check digit.
35-
*
36-
* @return int
37-
*/
38-
public function getNumber(): int;
39-
40-
/**
41-
* Get the check digit for the number.
42-
*
43-
* @return int|null The check digit or null if it has not been calculated yet.
44-
*/
45-
public function getCheckDigit(): ?int;
46-
}
1+
<?php
2+
3+
/*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Niklas Ekman
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
* this software and associated documentation files (the "Software"), to deal in
10+
* the Software without restriction, including without limitation the rights to
11+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12+
* the Software, and to permit persons to whom the Software is furnished to do so,
13+
* subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
namespace Nekman\LuhnAlgorithm\Contract;
27+
28+
/**
29+
* Describes the structure of a number in the Luhn Algorithm.
30+
*/
31+
interface NumberInterface
32+
{
33+
/**
34+
* Get the number, without check digit.
35+
*
36+
* @return int
37+
*/
38+
public function getNumber(): int;
39+
40+
/**
41+
* Get the check digit for the number.
42+
*
43+
* @return int|null The check digit or null if it has not been calculated yet.
44+
*/
45+
public function getCheckDigit(): ?int;
46+
}

src/LuhnAlgorithm.php

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,57 @@
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
class LuhnAlgorithm implements LuhnAlgorithmInterface {
35-
/**
36-
* {@inheritDoc}
37-
*/
38-
public function isValid(NumberInterface $number): bool {
39-
if ($number->getCheckDigit() === null) {
40-
throw new \InvalidArgumentException("Check digit cannot be null.");
34+
class LuhnAlgorithm implements LuhnAlgorithmInterface
35+
{
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function isValid(NumberInterface $number): bool
40+
{
41+
if ($number->getCheckDigit() === null) {
42+
throw new \InvalidArgumentException("Check digit cannot be null.");
4143
}
4244

4345
$checksum = $this->calcChecksum($number);
44-
$sum = $checksum + $number->getCheckDigit();
46+
$sum = $checksum + $number->getCheckDigit();
4547

46-
// If the checksum is divisible by 10 it is valid.
47-
return ($sum % 10) === 0;
48-
}
48+
// If the checksum is divisible by 10 it is valid.
49+
return ($sum % 10) === 0;
50+
}
4951

50-
/**
51-
* {@inheritDoc}
52-
*/
53-
public function calcCheckDigit(NumberInterface $number): int {
54-
$checksum = $this->calcChecksum($number);
55-
56-
// Get the last digit of the checksum.
57-
$checkDigit = $checksum % 10;
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
public function calcCheckDigit(NumberInterface $number): int
56+
{
57+
$checksum = $this->calcChecksum($number);
58+
59+
// Get the last digit of the checksum.
60+
$checkDigit = $checksum % 10;
5861

59-
// If the check digit is not 0, then subtract the value from 10.
60-
return $checkDigit === 0
62+
// If the check digit is not 0, then subtract the value from 10.
63+
return $checkDigit === 0
6164
? $checkDigit
6265
: 10 - $checkDigit;
63-
}
66+
}
6467

65-
/**
66-
* {@inheritDoc}
67-
*/
68-
public function calcChecksum(NumberInterface $number): int {
69-
$number = (string) $number->getNumber();
70-
$nDigits = strlen($number);
71-
$checksum = 0;
72-
$parity = $nDigits % 2;
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function calcChecksum(NumberInterface $number): int
72+
{
73+
$number = (string) $number->getNumber();
74+
$nDigits = strlen($number);
75+
$checksum = 0;
76+
$parity = $nDigits % 2;
7377

74-
for ($i = 0; $i < $nDigits; $i++) {
75-
$digit = (int) $number[$i];
78+
for ($i = 0; $i < $nDigits; $i++) {
79+
$digit = (int) $number[$i];
7680

77-
// Every other digit, starting from the leftmost,
81+
// Every other digit, starting from the leftmost,
7882
// shall be doubled.
79-
if (($i % 2) !== $parity) {
80-
$digit *= 2;
83+
if (($i % 2) !== $parity) {
84+
$digit *= 2;
8185

8286
if ($digit > 9) {
8387
$digit -= 9;
@@ -87,6 +91,6 @@ public function calcChecksum(NumberInterface $number): int {
8791
$checksum += $digit;
8892
}
8993

90-
return $checksum;
91-
}
94+
return $checksum;
95+
}
9296
}

0 commit comments

Comments
 (0)