From e5b3f21dc4bad52cfc2d02fd8c41b147ec6d50e2 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 23 Nov 2018 08:54:13 +0100 Subject: [PATCH 1/5] Remove unnesecary code --- src/LuhnAlgorithm.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/LuhnAlgorithm.php b/src/LuhnAlgorithm.php index c0abdb3..2b87e1c 100644 --- a/src/LuhnAlgorithm.php +++ b/src/LuhnAlgorithm.php @@ -44,11 +44,9 @@ public function isValid(NumberInterface $number): bool throw new \InvalidArgumentException("Check digit is null."); } - $checksum = $this->calcChecksum($number); - $sum = $checksum + $number->getCheckDigit(); + $checksum = $this->calcChecksum($number) + $number->getCheckDigit(); - // If the checksum is divisible by 10 it is valid. - return ($sum % 10) === 0; + return ($checksum % 10) === 0; } /** @@ -61,7 +59,6 @@ public function calcCheckDigit(NumberInterface $number): int // Get the last digit of the checksum. $checkDigit = $checksum % 10; - // If the check digit is not 0, then subtract the value from 10. return $checkDigit === 0 ? $checkDigit : 10 - $checkDigit; @@ -72,13 +69,11 @@ public function calcCheckDigit(NumberInterface $number): int */ public function calcChecksum(NumberInterface $number): int { - $number = (string) $number->getNumber(); - // Need to account for the check digit. - $nDigits = strlen($number) + 1; + $nDigits = strlen($number); $parity = $nDigits % 2; $checksum = 0; - for ($i = 0; $i < $nDigits - 1; $i++) { + for ($i = 0; $i < $nDigits; $i++) { $digit = (int) $number[$i]; // Every other digit, starting from the rightmost, From 53352589978399cab5f495397d80772b1813685a Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 23 Nov 2018 08:55:03 +0100 Subject: [PATCH 2/5] Ignore private constructor in code coverage --- src/LuhnAlgorithmFactory.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/LuhnAlgorithmFactory.php b/src/LuhnAlgorithmFactory.php index 1b791b9..f74143b 100644 --- a/src/LuhnAlgorithmFactory.php +++ b/src/LuhnAlgorithmFactory.php @@ -34,6 +34,9 @@ */ class LuhnAlgorithmFactory { + /** + * @codeCoverageIgnore + */ private function __construct() { // Only static methods. From 380e066d5f74dd56b58f6a1a12457f3c8f46c743 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 23 Nov 2018 08:58:27 +0100 Subject: [PATCH 3/5] Fix tests --- src/LuhnAlgorithm.php | 1 + src/LuhnAlgorithmFactory.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/LuhnAlgorithm.php b/src/LuhnAlgorithm.php index 2b87e1c..b7faf92 100644 --- a/src/LuhnAlgorithm.php +++ b/src/LuhnAlgorithm.php @@ -69,6 +69,7 @@ public function calcCheckDigit(NumberInterface $number): int */ public function calcChecksum(NumberInterface $number): int { + $number = $number->getNumber(); $nDigits = strlen($number); $parity = $nDigits % 2; $checksum = 0; diff --git a/src/LuhnAlgorithmFactory.php b/src/LuhnAlgorithmFactory.php index f74143b..b18a85f 100644 --- a/src/LuhnAlgorithmFactory.php +++ b/src/LuhnAlgorithmFactory.php @@ -35,7 +35,7 @@ class LuhnAlgorithmFactory { /** - * @codeCoverageIgnore + * @codeCoverageIgnore */ private function __construct() { From dcac99de05c24d0904c277ad087f2db913107647 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 23 Nov 2018 09:01:13 +0100 Subject: [PATCH 4/5] Fix tests --- src/LuhnAlgorithm.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LuhnAlgorithm.php b/src/LuhnAlgorithm.php index b7faf92..2526c64 100644 --- a/src/LuhnAlgorithm.php +++ b/src/LuhnAlgorithm.php @@ -69,13 +69,13 @@ public function calcCheckDigit(NumberInterface $number): int */ public function calcChecksum(NumberInterface $number): int { - $number = $number->getNumber(); - $nDigits = strlen($number); - $parity = $nDigits % 2; + $nDigits = strlen($number->getNumber()); + // Need to account for check digit + $parity = ($nDigits + 1) % 2; $checksum = 0; for ($i = 0; $i < $nDigits; $i++) { - $digit = (int) $number[$i]; + $digit = (int) $number->getNumber()[$i]; // Every other digit, starting from the rightmost, // shall be doubled. From 2b27ca5f12abf732e83ccec56ceaf6c994e19343 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 23 Nov 2018 09:02:50 +0100 Subject: [PATCH 5/5] Update copyright notice --- LICENSE.md | 2 +- src/Contract/LuhnAlgorithmInterface.php | 2 +- src/Contract/NumberInterface.php | 2 +- src/LuhnAlgorithm.php | 2 +- src/LuhnAlgorithmFactory.php | 2 +- src/Number.php | 2 +- tests/LuhnAlgorithmFactoryTest.php | 2 +- tests/LuhnAlgorithmTest.php | 2 +- tests/NumberTest.php | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 8a0bf0d..7e3b91a 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Niklas Ekman +Copyright (c) 2018 Niklas Ekman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/src/Contract/LuhnAlgorithmInterface.php b/src/Contract/LuhnAlgorithmInterface.php index 0b490d0..fba1ce2 100644 --- a/src/Contract/LuhnAlgorithmInterface.php +++ b/src/Contract/LuhnAlgorithmInterface.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/src/Contract/NumberInterface.php b/src/Contract/NumberInterface.php index c9e7a99..446d6a5 100644 --- a/src/Contract/NumberInterface.php +++ b/src/Contract/NumberInterface.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/src/LuhnAlgorithm.php b/src/LuhnAlgorithm.php index 2526c64..c75a6b9 100644 --- a/src/LuhnAlgorithm.php +++ b/src/LuhnAlgorithm.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/src/LuhnAlgorithmFactory.php b/src/LuhnAlgorithmFactory.php index b18a85f..c186d15 100644 --- a/src/LuhnAlgorithmFactory.php +++ b/src/LuhnAlgorithmFactory.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/src/Number.php b/src/Number.php index 979191b..a343e33 100644 --- a/src/Number.php +++ b/src/Number.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/tests/LuhnAlgorithmFactoryTest.php b/tests/LuhnAlgorithmFactoryTest.php index f887f3e..09dc8a3 100644 --- a/tests/LuhnAlgorithmFactoryTest.php +++ b/tests/LuhnAlgorithmFactoryTest.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/tests/LuhnAlgorithmTest.php b/tests/LuhnAlgorithmTest.php index dc5385a..8b0db14 100644 --- a/tests/LuhnAlgorithmTest.php +++ b/tests/LuhnAlgorithmTest.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in diff --git a/tests/NumberTest.php b/tests/NumberTest.php index 3dde9b5..ff215fe 100644 --- a/tests/NumberTest.php +++ b/tests/NumberTest.php @@ -3,7 +3,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014 Niklas Ekman + * Copyright (c) 2018 Niklas Ekman * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in