diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 23ab0e0..9589c50 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,15 +1,9 @@ - + string - - getNumberType($this->phoneNumber)]]> - - - PhoneNumberType::* - phoneNumber->getNationalNumber()]]> diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index 101110a..654b569 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -50,16 +50,16 @@ public static function parse(string $phoneNumber, ?string $regionCode = null) : } /** - * @param string $regionCode The region code. - * @param PhoneNumberType::* $phoneNumberType The phone number type, defaults to a fixed line. + * @param string $regionCode The region code. + * @param PhoneNumberType $phoneNumberType The phone number type, defaults to a fixed line. * * @return PhoneNumber * * @throws PhoneNumberException If no example number is available for this region and type. */ - public static function getExampleNumber(string $regionCode, int $phoneNumberType = PhoneNumberType::FIXED_LINE) : PhoneNumber + public static function getExampleNumber(string $regionCode, PhoneNumberType $phoneNumberType = PhoneNumberType::FIXED_LINE) : PhoneNumber { - $phoneNumber = PhoneNumberUtil::getInstance()->getExampleNumberForType($regionCode, $phoneNumberType); + $phoneNumber = PhoneNumberUtil::getInstance()->getExampleNumberForType($regionCode, $phoneNumberType->value); if ($phoneNumber === null) { throw new PhoneNumberException('No example number is available for the given region and type.'); @@ -165,24 +165,20 @@ public function isValidNumber() : bool /** * Returns the type of this phone number. - * - * @return PhoneNumberType::* */ - public function getNumberType() : int + public function getNumberType() : PhoneNumberType { - return PhoneNumberUtil::getInstance()->getNumberType($this->phoneNumber); + return PhoneNumberType::from( + PhoneNumberUtil::getInstance()->getNumberType($this->phoneNumber), + ); } /** * Returns a formatted string representation of this phone number. - * - * @param PhoneNumberFormat::* $format - * - * @return string */ - public function format(int $format) : string + public function format(PhoneNumberFormat $format) : string { - return PhoneNumberUtil::getInstance()->format($this->phoneNumber, $format); + return PhoneNumberUtil::getInstance()->format($this->phoneNumber, $format->value); } /** diff --git a/src/PhoneNumberFormat.php b/src/PhoneNumberFormat.php index 00f9480..9e071f8 100644 --- a/src/PhoneNumberFormat.php +++ b/src/PhoneNumberFormat.php @@ -5,9 +5,9 @@ namespace Brick\PhoneNumber; /** - * Constants for the phone number formats. + * Enum values for the phone number formats. */ -final class PhoneNumberFormat +enum PhoneNumberFormat: int { /** * The E164 format. @@ -17,7 +17,7 @@ final class PhoneNumberFormat * * Example: `+41446681800`. */ - public const E164 = 0; + case E164 = 0; /** * The international format. @@ -27,7 +27,7 @@ final class PhoneNumberFormat * * Example: `+41 44 668 1800`. */ - public const INTERNATIONAL = 1; + case INTERNATIONAL = 1; /** * The national format. @@ -37,7 +37,7 @@ final class PhoneNumberFormat * * Example: `044 668 1800`. */ - public const NATIONAL = 2; + case NATIONAL = 2; /** * The RFC 3966 format. @@ -46,5 +46,5 @@ final class PhoneNumberFormat * * Example: `tel:+41-44-668-1800`. */ - public const RFC3966 = 3; + case RFC3966 = 3; } diff --git a/src/PhoneNumberType.php b/src/PhoneNumberType.php index 228a0cf..73ae715 100644 --- a/src/PhoneNumberType.php +++ b/src/PhoneNumberType.php @@ -5,19 +5,19 @@ namespace Brick\PhoneNumber; /** - * Constants for the phone number types. + * Enum values for the phone number types. */ -final class PhoneNumberType +enum PhoneNumberType: int { /** * Fixed line number. */ - public const FIXED_LINE = 0; + case FIXED_LINE = 0; /** * Mobile number. */ - public const MOBILE = 1; + case MOBILE = 1; /** * Fixed line or mobile number. @@ -25,17 +25,17 @@ final class PhoneNumberType * In some regions (e.g. the USA), it is impossible to distinguish between fixed-line and * mobile numbers by looking at the phone number itself. */ - public const FIXED_LINE_OR_MOBILE = 2; + case FIXED_LINE_OR_MOBILE = 2; /** * Freephone number. */ - public const TOLL_FREE = 3; + case TOLL_FREE = 3; /** * Premium rate number. */ - public const PREMIUM_RATE = 4; + case PREMIUM_RATE = 4; /** * Shared cost number. @@ -45,14 +45,14 @@ final class PhoneNumberType * * @see http://en.wikipedia.org/wiki/Shared_Cost_Service */ - public const SHARED_COST = 5; + case SHARED_COST = 5; /** * Voice over IP number. * * This includes TSoIP (Telephony Service over IP). */ - public const VOIP = 6; + case VOIP = 6; /** * Personal number. @@ -62,19 +62,19 @@ final class PhoneNumberType * * @see http://en.wikipedia.org/wiki/Personal_Numbers */ - public const PERSONAL_NUMBER = 7; + case PERSONAL_NUMBER = 7; /** * Pager number. */ - public const PAGER = 8; + case PAGER = 8; /** * Universal Access Number or Company Number. * * The number may be further routed to specific offices, but allows one number to be used for a company. */ - public const UAN = 9; + case UAN = 9; /** * Unknown number type. @@ -82,25 +82,25 @@ final class PhoneNumberType * A phone number is of type UNKNOWN when it does not fit any of the known patterns * for a specific region. */ - public const UNKNOWN = 10; + case UNKNOWN = 10; /** * Emergency number. */ - public const EMERGENCY = 27; + case EMERGENCY = 27; /** * Voicemail number. */ - public const VOICEMAIL = 28; + case VOICEMAIL = 28; /** * Short code number. */ - public const SHORT_CODE = 29; + case SHORT_CODE = 29; /** * Standard rate number. */ - public const STANDARD_RATE = 30; + case STANDARD_RATE = 30; } diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php index 2aef32f..6132cd3 100644 --- a/tests/ConstantTest.php +++ b/tests/ConstantTest.php @@ -4,37 +4,41 @@ namespace Brick\PhoneNumber\Tests; +use BackedEnum; use Brick\PhoneNumber\PhoneNumberFormat; use Brick\PhoneNumber\PhoneNumberType; use PHPUnit\Framework\TestCase; /** - * Tests that the constants are up-to-date with libphonenumber. + * Tests that enums are up-to-date with libphonenumber constants. */ class ConstantTest extends TestCase { /** - * Compares the constants of two classes. - * - * @param string $classExpected The name or the reference class. - * @param string $classActual The name of the class to test against the reference class. + * @param class-string $classExpected The name or the reference libphonenumber class. + * @param class-string $enumClassActual The name of the enum class to test against the reference class. */ - private static function assertConstantsEqual(string $classExpected, string $classActual) : void + private static function assertEnumEqualsConstants(string $classExpected, string $enumClassActual) : void { - $expected = new \ReflectionClass($classExpected); - $actual = new \ReflectionClass($classActual); + $expected = (new \ReflectionClass($classExpected))->getConstants(); - self::assertSame($expected->getConstants(), $actual->getConstants()); + $actual = []; + + foreach ($enumClassActual::cases() as $enum) { + $actual[$enum->name] = $enum->value; + } + + self::assertSame($expected, $actual); } public function testPhoneNumberFormats() : void { - self::assertConstantsEqual(\libphonenumber\PhoneNumberFormat::class, PhoneNumberFormat::class); + self::assertEnumEqualsConstants(\libphonenumber\PhoneNumberFormat::class, PhoneNumberFormat::class); } public function testPhoneNumberTypes() : void { - self::assertConstantsEqual(\libphonenumber\PhoneNumberType::class, PhoneNumberType::class); + self::assertEnumEqualsConstants(\libphonenumber\PhoneNumberType::class, PhoneNumberType::class); } } diff --git a/tests/PhoneNumberTest.php b/tests/PhoneNumberTest.php index 2f5908d..6fa890a 100644 --- a/tests/PhoneNumberTest.php +++ b/tests/PhoneNumberTest.php @@ -57,12 +57,8 @@ class PhoneNumberTest extends TestCase /** * @dataProvider providerGetExampleNumber - * - * @param string $regionCode - * @param string $callingCode - * @param int|null $numberType */ - public function testGetExampleNumber(string $regionCode, string $callingCode, ?int $numberType = null) : void + public function testGetExampleNumber(string $regionCode, string $callingCode, ?PhoneNumberType $numberType = null) : void { if ($numberType === null) { $phoneNumber = PhoneNumber::getExampleNumber($regionCode); @@ -187,11 +183,8 @@ public static function providerGetRegionCode() : array /** * @dataProvider providerGetNumberType - * - * @param int $numberType - * @param string $phoneNumber */ - public function testGetNumberType(int $numberType, string $phoneNumber) : void + public function testGetNumberType(PhoneNumberType $numberType, string $phoneNumber) : void { self::assertSame($numberType, PhoneNumber::parse($phoneNumber)->getNumberType()); } @@ -374,12 +367,8 @@ public static function providerParseException() : array /** * @dataProvider providerFormatNumber - * - * @param string $expected - * @param string $phoneNumber - * @param int $numberFormat */ - public function testFormatNumber(string $expected, string $phoneNumber, int $numberFormat) : void + public function testFormatNumber(string $expected, string $phoneNumber, PhoneNumberFormat $numberFormat) : void { self::assertSame($expected, PhoneNumber::parse($phoneNumber)->format($numberFormat)); }