Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f1f0c2f

Browse filesBrowse files
minor #49216 [Validator] LuhnValidator code optimization (maxbeckers)
This PR was merged into the 6.3 branch. Discussion ---------- [Validator] LuhnValidator code optimization | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Just a minor code optimization. This way it get's a little bit more performance. Commits ------- 7c99a1f [Validator] LuhnValidator code optimization
2 parents ea7cc20 + 7c99a1f commit f1f0c2f
Copy full SHA for f1f0c2f

File tree

Expand file treeCollapse file tree

1 file changed

+17
-17
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-17
lines changed

‎src/Symfony/Component/Validator/Constraints/LuhnValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LuhnValidator.php
+17-17Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ public function validate(mixed $value, Constraint $constraint)
6060
$checkSum = 0;
6161
$length = \strlen($value);
6262

63-
// Starting with the last digit and walking left, add every second
64-
// digit to the check sum
65-
// e.g. 7 9 9 2 7 3 9 8 7 1 3
66-
// ^ ^ ^ ^ ^ ^
67-
// = 7 + 9 + 7 + 9 + 7 + 3
68-
for ($i = $length - 1; $i >= 0; $i -= 2) {
69-
$checkSum += $value[$i];
70-
}
71-
72-
// Starting with the second last digit and walking left, double every
73-
// second digit and add it to the check sum
74-
// For doubles greater than 9, sum the individual digits
75-
// e.g. 7 9 9 2 7 3 9 8 7 1 3
76-
// ^ ^ ^ ^ ^
77-
// = 1+8 + 4 + 6 + 1+6 + 2
78-
for ($i = $length - 2; $i >= 0; $i -= 2) {
79-
$checkSum += array_sum(str_split((int) $value[$i] * 2));
63+
for ($i = $length - 1; $i >= 0; --$i) {
64+
if (($i % 2) ^ ($length % 2)) {
65+
// Starting with the last digit and walking left, add every second
66+
// digit to the check sum
67+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
68+
// ^ ^ ^ ^ ^ ^
69+
// = 7 + 9 + 7 + 9 + 7 + 3
70+
$checkSum += (int) $value[$i];
71+
} else {
72+
// Starting with the second last digit and walking left, double every
73+
// second digit and add it to the check sum
74+
// For doubles greater than 9, sum the individual digits
75+
// e.g. 7 9 9 2 7 3 9 8 7 1 3
76+
// ^ ^ ^ ^ ^
77+
// = 1+8 + 4 + 6 + 1+6 + 2
78+
$checkSum += (((int) (2 * $value[$i] / 10)) + (2 * $value[$i]) % 10);
79+
}
8080
}
8181

8282
if (0 === $checkSum || 0 !== $checkSum % 10) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.