Skip to content

Commit 69d5ce5

Browse files
committed
Apply ECS
1 parent ee491df commit 69d5ce5

File tree

4 files changed

+88
-97
lines changed

4 files changed

+88
-97
lines changed

src/PhoneNumber.php

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
namespace Brick\PhoneNumber;
66

77
use JsonSerializable;
8-
use Override;
9-
use Stringable;
108
use libphonenumber;
119
use libphonenumber\geocoding\PhoneNumberOfflineGeocoder;
10+
use libphonenumber\NumberParseException;
1211
use libphonenumber\PhoneNumberToCarrierMapper;
1312
use libphonenumber\PhoneNumberToTimeZonesMapper;
14-
use libphonenumber\NumberParseException;
1513
use libphonenumber\PhoneNumberUtil;
14+
use Override;
15+
use Stringable;
16+
17+
use function assert;
18+
use function substr;
1619

1720
/**
1821
* A phone number.
@@ -38,15 +41,13 @@ private function __construct(libphonenumber\PhoneNumber $phoneNumber)
3841
* @param string $phoneNumber The phone number to parse.
3942
* @param string|null $regionCode The region code to assume, if the number is not in international format.
4043
*
41-
* @return PhoneNumber
42-
*
4344
* @throws PhoneNumberParseException
4445
*/
45-
public static function parse(string $phoneNumber, ?string $regionCode = null) : PhoneNumber
46+
public static function parse(string $phoneNumber, ?string $regionCode = null): PhoneNumber
4647
{
4748
try {
4849
return new PhoneNumber(
49-
PhoneNumberUtil::getInstance()->parse($phoneNumber, $regionCode)
50+
PhoneNumberUtil::getInstance()->parse($phoneNumber, $regionCode),
5051
);
5152
} catch (NumberParseException $e) {
5253
throw new PhoneNumberParseException($e);
@@ -57,11 +58,9 @@ public static function parse(string $phoneNumber, ?string $regionCode = null) :
5758
* @param string $regionCode The region code.
5859
* @param PhoneNumberType $phoneNumberType The phone number type, defaults to a fixed line.
5960
*
60-
* @return PhoneNumber
61-
*
6261
* @throws PhoneNumberException If no example number is available for this region and type.
6362
*/
64-
public static function getExampleNumber(string $regionCode, PhoneNumberType $phoneNumberType = PhoneNumberType::FIXED_LINE) : PhoneNumber
63+
public static function getExampleNumber(string $regionCode, PhoneNumberType $phoneNumberType = PhoneNumberType::FIXED_LINE): PhoneNumber
6564
{
6665
$phoneNumber = PhoneNumberUtil::getInstance()->getExampleNumberForType(
6766
$regionCode,
@@ -79,10 +78,8 @@ public static function getExampleNumber(string $regionCode, PhoneNumberType $pho
7978
* Returns the country code of this PhoneNumber.
8079
*
8180
* The country code is a series of 1 to 3 digits, as defined per the E.164 recommendation.
82-
*
83-
* @return string
8481
*/
85-
public function getCountryCode() : string
82+
public function getCountryCode(): string
8683
{
8784
$countryCode = $this->phoneNumber->getCountryCode();
8885
assert($countryCode !== null);
@@ -101,10 +98,8 @@ public function getCountryCode() : string
10198
* - some geographical numbers have no area codes.
10299
*
103100
* If this number has no area code, an empty string is returned.
104-
*
105-
* @return string
106101
*/
107-
public function getGeographicalAreaCode() : string
102+
public function getGeographicalAreaCode(): string
108103
{
109104
$phoneNumberUtil = PhoneNumberUtil::getInstance();
110105

@@ -119,10 +114,8 @@ public function getGeographicalAreaCode() : string
119114
* Returns the national number of this PhoneNumber.
120115
*
121116
* The national number is a series of digits.
122-
*
123-
* @return string
124117
*/
125-
public function getNationalNumber() : string
118+
public function getNationalNumber(): string
126119
{
127120
$nationalNumber = $this->phoneNumber->getNationalNumber();
128121
assert($nationalNumber !== null);
@@ -140,7 +133,7 @@ public function getNationalNumber() : string
140133
*
141134
* @return string|null The region code, or null if the number does not map to a geographic region.
142135
*/
143-
public function getRegionCode() : ?string
136+
public function getRegionCode(): ?string
144137
{
145138
$regionCode = PhoneNumberUtil::getInstance()->getRegionCodeForNumber($this->phoneNumber);
146139

@@ -155,10 +148,8 @@ public function getRegionCode() : ?string
155148
* Returns whether this phone number is a possible number.
156149
*
157150
* Note this provides a more lenient and faster check than `isValidNumber()`.
158-
*
159-
* @return bool
160151
*/
161-
public function isPossibleNumber() : bool
152+
public function isPossibleNumber(): bool
162153
{
163154
return PhoneNumberUtil::getInstance()->isPossibleNumber($this->phoneNumber);
164155
}
@@ -168,18 +159,16 @@ public function isPossibleNumber() : bool
168159
*
169160
* Note this doesn't verify the number is actually in use,
170161
* which is impossible to tell by just looking at a number itself.
171-
*
172-
* @return bool
173162
*/
174-
public function isValidNumber() : bool
163+
public function isValidNumber(): bool
175164
{
176165
return PhoneNumberUtil::getInstance()->isValidNumber($this->phoneNumber);
177166
}
178167

179168
/**
180169
* Returns the type of this phone number.
181170
*/
182-
public function getNumberType() : PhoneNumberType
171+
public function getNumberType(): PhoneNumberType
183172
{
184173
return PhoneNumberType::from(
185174
PhoneNumberUtil::getInstance()->getNumberType($this->phoneNumber)->value,
@@ -189,7 +178,7 @@ public function getNumberType() : PhoneNumberType
189178
/**
190179
* Returns a formatted string representation of this phone number.
191180
*/
192-
public function format(PhoneNumberFormat $format) : string
181+
public function format(PhoneNumberFormat $format): string
193182
{
194183
return PhoneNumberUtil::getInstance()->format(
195184
$this->phoneNumber,
@@ -200,11 +189,9 @@ public function format(PhoneNumberFormat $format) : string
200189
/**
201190
* Formats this phone number for out-of-country dialing purposes.
202191
*
203-
* @param string $regionCode The ISO 3166-1 alpha-2 country code
204-
*
205-
* @return string
192+
* @param string $regionCode The ISO 3166-1 alpha-2 country code.
206193
*/
207-
public function formatForCallingFrom(string $regionCode) : string
194+
public function formatForCallingFrom(string $regionCode): string
208195
{
209196
return PhoneNumberUtil::getInstance()->formatOutOfCountryCallingNumber($this->phoneNumber, $regionCode);
210197
}
@@ -259,15 +246,13 @@ public function jsonSerialize(): string
259246
* @param string|null $userRegion The region code for a given user. This region will be omitted from the description
260247
* if the phone number comes from this region. It is a two-letter uppercase CLDR
261248
* region code.
262-
*
263-
* @return string|null
264249
*/
265-
public function getDescription(string $locale, ?string $userRegion = null) : ?string
250+
public function getDescription(string $locale, ?string $userRegion = null): ?string
266251
{
267252
$description = PhoneNumberOfflineGeocoder::getInstance()->getDescriptionForNumber(
268253
$this->phoneNumber,
269254
$locale,
270-
$userRegion
255+
$userRegion,
271256
);
272257

273258
if ($description === '') {
@@ -327,11 +312,9 @@ public function getTimeZones(): array
327312

328313
/**
329314
* Returns a string representation of this phone number in international E164 format.
330-
*
331-
* @return string
332315
*/
333316
#[Override]
334-
public function __toString() : string
317+
public function __toString(): string
335318
{
336319
return $this->format(PhoneNumberFormat::E164);
337320
}

src/PhoneNumberException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Brick\PhoneNumber;
66

7+
use Exception;
8+
79
/**
810
* Base class for phone number exceptions.
911
*/
10-
class PhoneNumberException extends \Exception
12+
class PhoneNumberException extends Exception
1113
{
1214
}

tests/EnumTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,35 @@
88
use Brick\PhoneNumber\PhoneNumberFormat;
99
use Brick\PhoneNumber\PhoneNumberParseErrorType;
1010
use Brick\PhoneNumber\PhoneNumberType;
11+
use libphonenumber\NumberParseException;
1112
use PHPUnit\Framework\TestCase;
13+
use ReflectionClass;
1214

1315
/**
1416
* Tests that enums are up-to-date with libphonenumber constants.
1517
*/
1618
class EnumTest extends TestCase
1719
{
20+
public function testPhoneNumberFormats(): void
21+
{
22+
self::assertEnumEqualsEnum(\libphonenumber\PhoneNumberFormat::class, PhoneNumberFormat::class);
23+
}
24+
25+
public function testPhoneNumberTypes(): void
26+
{
27+
self::assertEnumEqualsEnum(\libphonenumber\PhoneNumberType::class, PhoneNumberType::class);
28+
}
29+
30+
public function testPhoneNumberParseErrorTypes(): void
31+
{
32+
self::assertEnumEqualsConstants(NumberParseException::class, PhoneNumberParseErrorType::class);
33+
}
34+
1835
/**
1936
* @param class-string<BackedEnum> $enumClassExpected The name or the reference libphonenumber enum class.
2037
* @param class-string<BackedEnum> $enumClassActual The name of the enum class to test against the reference class.
2138
*/
22-
private static function assertEnumEqualsEnum(string $enumClassExpected, string $enumClassActual) : void
39+
private static function assertEnumEqualsEnum(string $enumClassExpected, string $enumClassActual): void
2340
{
2441
self::assertSame(
2542
self::enumToMap($enumClassExpected),
@@ -31,9 +48,9 @@ private static function assertEnumEqualsEnum(string $enumClassExpected, string $
3148
* @param class-string $classExpected The name or the reference libphonenumber class.
3249
* @param class-string<BackedEnum> $enumClassActual The name of the enum class to test against the reference class.
3350
*/
34-
private static function assertEnumEqualsConstants(string $classExpected, string $enumClassActual) : void
51+
private static function assertEnumEqualsConstants(string $classExpected, string $enumClassActual): void
3552
{
36-
$expected = (new \ReflectionClass($classExpected))->getConstants();
53+
$expected = (new ReflectionClass($classExpected))->getConstants();
3754
$actual = self::enumToMap($enumClassActual);
3855

3956
self::assertSame($expected, $actual);
@@ -54,19 +71,4 @@ private static function enumToMap(string $enumClass): array
5471

5572
return $values;
5673
}
57-
58-
public function testPhoneNumberFormats() : void
59-
{
60-
self::assertEnumEqualsEnum(\libphonenumber\PhoneNumberFormat::class, PhoneNumberFormat::class);
61-
}
62-
63-
public function testPhoneNumberTypes() : void
64-
{
65-
self::assertEnumEqualsEnum(\libphonenumber\PhoneNumberType::class, PhoneNumberType::class);
66-
}
67-
68-
public function testPhoneNumberParseErrorTypes() : void
69-
{
70-
self::assertEnumEqualsConstants(\libphonenumber\NumberParseException::class, PhoneNumberParseErrorType::class);
71-
}
7274
}

0 commit comments

Comments
 (0)