Skip to content

Commit c640763

Browse files
committed
Removed intval() from toInteger() and minor fix
1 parent 8259bec commit c640763

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

LuhnAlgorithm.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function isValid() {
6565
* @return int Checksum
6666
*/
6767
public static function calculateChecksum($number, $length = 0) {
68-
$number = strval(self::stringToInteger($number));
68+
$number = strval(self::toInteger($number));
6969

7070
if ($length === 0) {
7171
$length = strlen($number);
@@ -80,7 +80,7 @@ public static function calculateChecksum($number, $length = 0) {
8080

8181
// If a 2 digit number, split and add togheter
8282
if ($tmp > 9) {
83-
$tmp = intval($tmp / 10) + intval($tmp % 10);
83+
$tmp = ($tmp / 10) + ($tmp % 10);
8484
}
8585

8686
// Sum it upp
@@ -147,12 +147,13 @@ public function getCheckDigit() {
147147
}
148148

149149
/**
150-
* Fix a string so that it only contains numbers
151-
* @param string $integer String to convert to integer
152-
* @return int An integer
150+
* Remove all but numbers from a string
151+
*
152+
* @param string $string String to "convert" to integer
153+
* @return string String containing only numbers
153154
*/
154-
public static function stringToInteger($integer) {
155-
return intval(preg_replace("/[^0-9]/", "", $integer));
155+
public static function toInteger($string) {
156+
return preg_replace("/[^\d]/", "", $string);
156157
}
157158

158159
/**
@@ -162,7 +163,7 @@ public static function stringToInteger($integer) {
162163
* included in $number?
163164
*/
164165
public function setNumber($number, $withCheckDigit = true) {
165-
$number = strval(self::stringToInteger($number));
166+
$number = strval(self::toInteger($number));
166167
$length = strlen($number);
167168

168169
// If number does not include checkdigit, calculate it!

tests/LuhnAlgorithmTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testIsValid($number) {
5050
* @dataProvider provider
5151
*/
5252
public function testCalculateChecksum($number) {
53-
$number = \LuhnAlgorithm::stringToInteger($number);
53+
$number = \LuhnAlgorithm::toInteger($number);
5454
$checkSum = \LuhnAlgorithm::calculateChecksum($number);
5555
$this->assertEquals(0, $checkSum % 10);
5656
}
@@ -60,7 +60,7 @@ public function testCalculateChecksum($number) {
6060
* @dataProvider provider
6161
*/
6262
public function testCalculcateCheckDigit($number) {
63-
$number = strval(\LuhnAlgorithm::stringToInteger($number));
63+
$number = strval(\LuhnAlgorithm::toInteger($number));
6464
$last = strlen($number) - 1;
6565

6666
// Check digit is the last number
@@ -95,7 +95,7 @@ public function testIsCompletelyValid($number) {
9595
*/
9696
public function testGetNumber($number) {
9797
$this->object->setNumber($number, true);
98-
$number = \LuhnAlgorithm::stringToInteger($number);
98+
$number = \LuhnAlgorithm::toInteger($number);
9999
$this->assertEquals($number, $this->object->getNumber());
100100
}
101101

@@ -115,7 +115,7 @@ public function testGetCheckDigit($number) {
115115
* @dataProvider provider
116116
*/
117117
public function testStringToInteger($number) {
118-
$int = \LuhnAlgorithm::stringToInteger($number);
118+
$int = \LuhnAlgorithm::toInteger($number);
119119
$this->assertTrue(is_int($int));
120120
}
121121

0 commit comments

Comments
 (0)