Skip to content

Commit

Permalink
Minor optimizations (direct cast instead of function converts).
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Tegnér <[email protected]>
  • Loading branch information
Johannestegner committed Jan 17, 2024
1 parent 67d5438 commit 17529db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/Personnummer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function isMale(): bool
$parts = $this->parts;
$genderDigit = substr($parts['num'], -1);

return boolval($genderDigit % 2);
return (bool)($genderDigit % 2);
}

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ public function isCoordinationNumber(): bool
{
$parts = $this->parts;

return checkdate(intval($parts['month']), $parts['day'] - 60, $parts['fullYear']);
return checkdate((int)$parts['month'], $parts['day'] - 60, $parts['fullYear']);
}

public static function valid(string $ssn, array $options = []): bool
Expand Down Expand Up @@ -132,7 +132,7 @@ private static function getParts(string $ssn): array
$parts = array_filter($match, 'is_string', ARRAY_FILTER_USE_KEY);

if (!empty($parts['century'])) {
if (date('Y') - intval(strval($parts['century']) . strval($parts['year'])) < 100) {
if (date('Y') - (int)((string)$parts['century'] . (string)$parts['year']) < 100) {
$parts['sep'] = '-';
} else {
$parts['sep'] = '+';
Expand Down Expand Up @@ -163,8 +163,9 @@ private static function luhn(string $str): int
{
$sum = 0;

for ($i = 0; $i < strlen($str); $i++) {
$v = intval($str[$i]);
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$v = (int)$str[$i];
$v *= 2 - ($i % 2);

if ($v > 9) {
Expand All @@ -174,7 +175,7 @@ private static function luhn(string $str): int
$sum += $v;
}

return intval(ceil($sum / 10) * 10 - $sum);
return (int)(ceil($sum / 10) * 10 - $sum);
}

/**
Expand Down Expand Up @@ -206,7 +207,7 @@ public function getAge(): int
{
$parts = $this->parts;

$day = intval($parts['day']);
$day = (int)$parts['day'];
if ($this->isCoordinationNumber()) {
$day -= 60;
}
Expand Down Expand Up @@ -257,7 +258,7 @@ private function isValid(): bool
}

$checkStr = $parts['year'] . $parts['month'] . $parts['day'] . $parts['num'];
$validCheck = self::luhn($checkStr) === intval($parts['check']);
$validCheck = self::luhn($checkStr) === (int)$parts['check'];

return $validDate && $validCheck;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PersonnummerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class PersonnummerException extends Exception
* @param null|Exception $previous
*/
public function __construct(
$message = 'Invalid swedish social security number',
$code = 400,
$previous = null
string $message = 'Invalid swedish social security number',
int $code = 400,
?Exception $previous = null
) {
parent::__construct($message, $code, $previous);
}
Expand Down

0 comments on commit 17529db

Please sign in to comment.