Skip to content

Commit

Permalink
Convert PhoneNumberType & PhoneNumberFormat to enums
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 13, 2023
1 parent 218c898 commit ee0309a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 78 deletions.
8 changes: 1 addition & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.7.5@5390c212bab06ee230c8720c2e9c54b823db00c8">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/PhoneNumber.php">
<InvalidNullableReturnType>
<code>string</code>
</InvalidNullableReturnType>
<LessSpecificReturnStatement>
<code><![CDATA[PhoneNumberUtil::getInstance()->getNumberType($this->phoneNumber)]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code>PhoneNumberType::*</code>
</MoreSpecificReturnType>
<NullableReturnStatement>
<code><![CDATA[$this->phoneNumber->getNationalNumber()]]></code>
</NullableReturnStatement>
Expand Down
24 changes: 10 additions & 14 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/PhoneNumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,7 +17,7 @@ final class PhoneNumberFormat
*
* Example: `+41446681800`.
*/
public const E164 = 0;
case E164 = 0;

/**
* The international format.
Expand All @@ -27,7 +27,7 @@ final class PhoneNumberFormat
*
* Example: `+41 44 668 1800`.
*/
public const INTERNATIONAL = 1;
case INTERNATIONAL = 1;

/**
* The national format.
Expand All @@ -37,7 +37,7 @@ final class PhoneNumberFormat
*
* Example: `044 668 1800`.
*/
public const NATIONAL = 2;
case NATIONAL = 2;

/**
* The RFC 3966 format.
Expand All @@ -46,5 +46,5 @@ final class PhoneNumberFormat
*
* Example: `tel:+41-44-668-1800`.
*/
public const RFC3966 = 3;
case RFC3966 = 3;
}
34 changes: 17 additions & 17 deletions src/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
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.
*
* 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.
Expand All @@ -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.
Expand All @@ -62,45 +62,45 @@ 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.
*
* 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;
}
26 changes: 15 additions & 11 deletions tests/ConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<BackedEnum> $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);
}
}
26 changes: 3 additions & 23 deletions tests/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,9 +77,6 @@ public function testGetExampleNumber(string $regionCode, string $callingCode, ?i
self::assertSame($regionCode, $phoneNumber->getRegionCode());
}

/**
* @return array
*/
public function providerGetExampleNumber() : array
{
return [
Expand Down Expand Up @@ -199,18 +192,12 @@ public 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());
}

/**
* @return array
*/
public function providerGetNumberType() : array
{
return [
Expand Down Expand Up @@ -401,19 +388,12 @@ public 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));
}

/**
* @return array
*/
public function providerFormatNumber() : array
{
return [
Expand Down

0 comments on commit ee0309a

Please sign in to comment.