Skip to content

Commit

Permalink
Merge pull request #37 from fawno/psalm-fix-tin
Browse files Browse the repository at this point in the history
Fix parse method on TIN class: https://psalm.dev/078
  • Loading branch information
drupol authored Jun 6, 2024
2 parents b2cbb19 + 8d90323 commit f86b0b6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020-2023 Thomas Portelange, Pol Dellaiera
Copyright (c) 2020-2024 Thomas Portelange, Pol Dellaiera

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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"infection/extension-installer": true,
"phpstan/extension-installer": true,
"phpro/grumphp": true,
"ergebnis/composer-normalize": true
"phpstan/extension-installer": true
}
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/TINException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
final class TINException extends Exception
{
public static function emptySlug(): TINException
{
return new self('Invalid Slug. Reason: Void string.');
}

public static function invalidCountry(string $countryCode): TINException
{
return new self(sprintf('No handler available for this country code: %s.', $countryCode));
Expand Down
16 changes: 11 additions & 5 deletions src/TIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,19 @@ private function getAlgorithm(string $country, ?string $tin = null): CountryHand
/**
* @throws TINException
*
* @return array<string, string>
* @return non-empty-array<'country'|'tin', string>
*/
private function parse(string $slug): array
{
return array_combine(
['country', 'tin'],
sscanf($slug, '%2s%s')
);
if ('' === $slug) {
throw TINException::emptySlug();
}

[$country, $tin] = sscanf($slug, '%2s%s') + ['', ''];

return [
'country' => (string) $country,
'tin' => (string) $tin,
];
}
}

0 comments on commit f86b0b6

Please sign in to comment.