Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c48922d

Browse files
committedNov 10, 2024·
update
1 parent 0145667 commit c48922d

File tree

16 files changed

+133
-478
lines changed

16 files changed

+133
-478
lines changed
 

‎.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: true
1111
matrix:
12-
php: [ '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
12+
php: [ '7.0', '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
1313
stability: [ prefer-stable ]
1414

1515
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

‎composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "recca0120/twzipcode",
33
"description": "twzipcode",
4-
"keywords": ["twzipcode"],
4+
"keywords": [
5+
"twzipcode"
6+
],
57
"license": "MIT",
68
"type": "library",
79
"require": {
8-
"php": ">=5.5.9",
10+
"php": ">=7.0",
911
"ext-zlib": "*",
1012
"recca0120/lodash": "^1.1"
1113
},

‎resources/converter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
$contents = preg_replace("/^\xEF\xBB\xBF/", '', $contents);
2121
$contents = trim(str_replace('Zip5,City,Area,Road,Scope', '', $contents));
2222

23-
$zip = new ZipArchive();
23+
$zip = new ZipArchive;
2424
$zip->open($file, ZipArchive::OVERWRITE);
2525
$zip->addFromString(pathinfo($file, PATHINFO_FILENAME).'.csv', $contents);
2626
$zip->close();
2727
}
2828

29-
(new File())->loadFile($file);
29+
(new File)->loadFile($file);
3030
echo 'benchmark: '.(microtime(true) - $start)."\n";

‎src/Address.php

+18-79
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,46 @@
66

77
class Address
88
{
9-
/**
10-
* NO.
11-
*
12-
* @var int
13-
*/
14-
const NO = 0;
9+
public const NO = 0;
1510

16-
/**
17-
* SUBNO.
18-
*
19-
* @var int
20-
*/
21-
const SUBNO = 1;
11+
public const SUBNO = 1;
2212

23-
/**
24-
* NAME.
25-
*
26-
* @var int
27-
*/
28-
const NAME = 2;
13+
public const NAME = 2;
2914

30-
/**
31-
* UNIT.
32-
*
33-
* @var int
34-
*/
35-
const UNIT = 3;
15+
public const UNIT = 3;
3616

37-
/**
38-
* $normalizer.
39-
*
40-
* @var Normalizer
41-
*/
17+
/** @var Normalizer */
4218
public $normalizer;
4319

44-
/**
45-
* @var Tricky
46-
*/
20+
/** @var Tricky */
4721
public $tricky;
4822

49-
/**
50-
* $tokens.
51-
*
52-
* @var JArray
53-
*/
23+
/** @var JArray */
5424
public $tokens = [];
5525

5626
/**
57-
* __construct.
58-
*
5927
* @param static|array $address
6028
*/
6129
public function __construct($address = '')
6230
{
63-
$this->tricky = new Tricky();
31+
$this->tricky = new Tricky;
6432
if (empty($address) === false) {
6533
$this->set($address);
6634
}
6735
}
6836

6937
/**
70-
* set.
71-
*
7238
* @param static|string $address
73-
* @return $this
7439
*/
75-
public function set($address)
40+
public function set($address): self
7641
{
7742
$this->normalizer = (new Normalizer($address))->normalize()->normalizeAddress();
7843
$this->tokens = $this->tokenize();
7944

8045
return $this;
8146
}
8247

83-
/**
84-
* tokenize.
85-
*
86-
* @return array
87-
*/
88-
private function tokenize()
48+
private function tokenize(): array
8949
{
9050
$units = [static::NO => 'no', static::SUBNO => 'subno', static::NAME => 'name', static::UNIT => 'unit'];
9151

@@ -109,33 +69,17 @@ private function tokenize()
10969
return $tokens;
11070
}
11171

112-
/**
113-
* __toString.
114-
*
115-
* @return string
116-
*/
11772
public function __toString()
11873
{
11974
return $this->normalizer->value();
12075
}
12176

122-
/**
123-
* tokens.
124-
*
125-
* @return JArray
126-
*/
127-
public function tokens()
77+
public function tokens(): JArray
12878
{
12979
return new JArray($this->tokens);
13080
}
13181

132-
/**
133-
* getPoint.
134-
*
135-
* @param string $index
136-
* @return Point
137-
*/
138-
public function getPoint($index)
82+
public function getPoint(string $index): Point
13983
{
14084
if (isset($this->tokens[$index]) === false) {
14185
return new Point(0, 0);
@@ -148,21 +92,16 @@ public function getPoint($index)
14892
);
14993
}
15094

151-
/**
152-
* flat.
153-
*
154-
* @param int $length
155-
* @param int $offset
156-
* @return string
157-
*/
158-
public function flat($length = null, $offset = 0)
95+
public function flat(?int $length = null, int $offset = 0): string
15996
{
16097
$tokens = $this->tokens();
16198
$length = $length ?: $tokens->length();
16299
$end = $offset + $length;
163100

164-
return (string) $tokens->slice($offset, $end)->map(function ($token) {
165-
return implode('', $token);
166-
})->join('');
101+
return (string) $tokens->slice($offset, $end)
102+
->map(function ($token) {
103+
return implode('', $token);
104+
})
105+
->join('');
167106
}
168107
}

‎src/Contracts/Storage.php

+5-33
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,13 @@
66

77
interface Storage
88
{
9-
/**
10-
* zip3.
11-
*
12-
* @return string
13-
*/
14-
public function zip3(Address $address);
9+
public function zip3(Address $address): ?string;
1510

16-
/**
17-
* rules.
18-
*
19-
* @param string $zip3
20-
* @return JString
21-
*/
22-
public function rules($zip3);
11+
public function rules(string $zip3): array;
2312

24-
/**
25-
* load.
26-
*
27-
* @param string $source
28-
* @return $this
29-
*/
30-
public function load($source);
13+
public function load(string $source): self;
3114

32-
/**
33-
* loadFile.
34-
*
35-
* @param string $file
36-
* @return $this
37-
*/
38-
public function loadFile($file = null);
15+
public function loadFile(?string $file = null): self;
3916

40-
/**
41-
* flush.
42-
*
43-
* @return $this
44-
*/
45-
public function flush();
17+
public function flush(): self;
4618
}

‎src/Normalizer.php

+4-24
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66

77
class Normalizer extends JString
88
{
9-
/**
10-
* normalizeAddress.
11-
*
12-
* @return static
13-
*/
14-
public function normalizeAddress()
9+
public function normalizeAddress(): self
1510
{
1611
return $this
1712
->replace([
@@ -48,34 +43,19 @@ public function normalizeAddress()
4843
]);
4944
}
5045

51-
/**
52-
* normalize.
53-
*
54-
* @return static
55-
*/
56-
public function normalize()
46+
public function normalize(): self
5747
{
5848
return $this->trim()->regularize()->digitize();
5949
}
6050

61-
/**
62-
* digitize.
63-
*
64-
* @return static
65-
*/
66-
public function digitize()
51+
public function digitize(): self
6752
{
6853
return $this->replace('/[一二三四五六七八九十百千]+(?=[段路街巷弄號樓])/u', function ($m) {
6954
return (new static($m[0]))->chineseToNumber();
7055
});
7156
}
7257

73-
/**
74-
* regularize.
75-
*
76-
* @return static
77-
*/
78-
public function regularize()
58+
public function regularize(): self
7959
{
8060
return $this
8161
->toHalfCase()

‎src/Point.php

+3-30
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,22 @@
44

55
class Point
66
{
7-
/**
8-
* $x.
9-
*
10-
* @var int
11-
*/
127
public $x = 0;
138

14-
/**
15-
* $y.
16-
*
17-
* @var int
18-
*/
199
public $y = 0;
2010

21-
/**
22-
* __construct.
23-
*
24-
* @param int $x
25-
* @param int $y
26-
*/
27-
public function __construct($x = 0, $y = 0)
11+
public function __construct(int $x = 0, int $y = 0)
2812
{
2913
$this->x = $x;
3014
$this->y = $y;
3115
}
3216

33-
/**
34-
* empty.
35-
*
36-
* @return bool
37-
*/
38-
public function isEmpty()
17+
public function isEmpty(): bool
3918
{
4019
return $this->x === 0 && $this->y === 0;
4120
}
4221

43-
/**
44-
* compare.
45-
*
46-
* @param string $operator
47-
* @return bool
48-
*/
49-
public function compare(self $point, $operator = '=')
22+
public function compare(self $point, string $operator = '='): bool
5023
{
5124
$sum = $this->x * 10 + $this->y;
5225
$sum2 = $point->x * 10 + $point->y;

‎src/Rule.php

+24-83
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,19 @@
77

88
class Rule
99
{
10-
/**
11-
* $zip3.
12-
*
13-
* @var string
14-
*/
10+
/** @var string */
1511
public $zip3;
1612

17-
/**
18-
* $zip5.
19-
*
20-
* @var string
21-
*/
13+
/** @var string */
2214
public $zip5;
2315

24-
/**
25-
* $address.
26-
*
27-
* @var Address
28-
*/
16+
/** @var Address */
2917
public $address;
3018

31-
/**
32-
* $tokens.
33-
*
34-
* @var array
35-
*/
19+
/** @var array */
3620
public $tokens;
3721

38-
/**
39-
* __construct.
40-
*
41-
* @param string $rule
42-
*/
43-
public function __construct($rule)
22+
public function __construct(string $rule)
4423
{
4524
if (preg_match('/^(\d+),?(.*)/', $rule, $m)) {
4625
$this->zip5 = $m[1];
@@ -53,43 +32,25 @@ public function __construct($rule)
5332
});
5433
}
5534

56-
/**
57-
* zip5.
58-
*
59-
* @return string
60-
*/
61-
public function zip5()
35+
public function zip5(): string
6236
{
6337
return $this->zip5;
6438
}
6539

66-
/**
67-
* zip.
68-
*
69-
* @return string
70-
*/
71-
public function zip()
40+
public function zip(): string
7241
{
7342
return $this->zip3();
7443
}
7544

76-
/**
77-
* zip3.
78-
*
79-
* @return string
80-
*/
81-
public function zip3()
45+
public function zip3(): string
8246
{
8347
return $this->zip3;
8448
}
8549

8650
/**
87-
* match.
88-
*
8951
* @param Address|string $address
90-
* @return bool
9152
*/
92-
public function match($address)
53+
public function match($address): bool
9354
{
9455
$ruleAddressTokens = $this->address->tokens();
9556
$address = $this->normalizeAddress(
@@ -141,23 +102,12 @@ public function match($address)
141102
return true;
142103
}
143104

144-
/**
145-
* tokens.
146-
*
147-
* @return JArray
148-
*/
149-
public function tokens()
105+
public function tokens(): JArray
150106
{
151107
return new JArray($this->tokens);
152108
}
153109

154-
/**
155-
* tokenize.
156-
*
157-
* @param string $rule
158-
* @return array
159-
*/
160-
private function tokenize($rule, Closure $addressResolver)
110+
private function tokenize(string $rule, Closure $addressResolver): array
161111
{
162112
$tokens = [];
163113

@@ -168,27 +118,22 @@ private function tokenize($rule, Closure $addressResolver)
168118
'[連至單雙全](?=[\d全]|$)',
169119
];
170120

171-
$addressResolver($this->normalize($rule)->replace('/'.implode('|', $pattern).'/u', function ($m) use (&$tokens) {
172-
$token = &$m[0];
173-
if ($token === '') {
174-
return '';
175-
}
121+
$addressResolver($this->normalize($rule)->replace('/'.implode('|', $pattern).'/u',
122+
function ($m) use (&$tokens) {
123+
$token = &$m[0];
124+
if ($token === '') {
125+
return '';
126+
}
176127

177-
$tokens[] = $token;
128+
$tokens[] = $token;
178129

179-
return $token === '附號全' ? '' : '';
180-
}));
130+
return $token === '附號全' ? '' : '';
131+
}));
181132

182133
return $tokens;
183134
}
184135

185-
/**
186-
* normalize.
187-
*
188-
* @param string $rule
189-
* @return Normalizer
190-
*/
191-
private function normalize($rule)
136+
private function normalize(string $rule): Normalizer
192137
{
193138
$pattern = '((?P<no>\d+)之)?\s*(?P<left>\d+)至之?\s*(?P<right>\d+)(?P<unit>\w)';
194139

@@ -207,20 +152,16 @@ private function normalize($rule)
207152
});
208153
}
209154

210-
/**
211-
* normalizeAddress.
212-
*
213-
* @return Address
214-
*/
215-
private function normalizeAddress(Address $address, JArray $ruleAddressTokens)
155+
private function normalizeAddress(Address $address, JArray $ruleAddressTokens): Address
216156
{
217157
$removeUnits = array_diff(['', '', '', ''], (array) $ruleAddressTokens->map(function ($token) {
218158
return isset($token[Address::UNIT]) === true ? $token[Address::UNIT] : '';
219159
})->values());
220160

221161
return new Address(
222162
new JArray($address->tokens()->filter(function ($token) use ($removeUnits) {
223-
return isset($token[Address::UNIT]) === true && in_array($token[Address::UNIT], $removeUnits, true) === false;
163+
return isset($token[Address::UNIT]) === true &&
164+
in_array($token[Address::UNIT], $removeUnits, true) === false;
224165
})->map(function ($token) {
225166
return implode('', $token);
226167
}))

‎src/Rules.php

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@
22

33
namespace Recca0120\Twzipcode;
44

5+
use Recca0120\Lodash\JArray;
56
use Recca0120\Twzipcode\Contracts\Storage;
67
use Recca0120\Twzipcode\Storages\File;
78

89
class Rules
910
{
10-
/**
11-
* $storage.
12-
*
13-
* @var Storage
14-
*/
11+
/** @var Storage */
1512
private $storage;
1613

17-
/**
18-
* __construct.
19-
*
20-
* @param Storage $storage
21-
*/
22-
public function __construct(Storage $storage = null)
14+
public function __construct(?Storage $storage = null)
2315
{
24-
$this->storage = $storage ?: new File();
16+
$this->storage = $storage ?: new File;
2517
}
2618

2719
/**
@@ -39,7 +31,7 @@ public function match($address)
3931
return;
4032
}
4133

42-
$rule = $this->storage->rules($zip3)->find(function ($rule) use ($address) {
34+
$rule = (new JArray($this->storage->rules($zip3)))->find(function ($rule) use ($address) {
4335
return $rule->match($address);
4436
});
4537

‎src/Storages/File.php

+39-127
Original file line numberDiff line numberDiff line change
@@ -11,163 +11,99 @@
1111

1212
class File implements Storage
1313
{
14-
/**
15-
* cached.
16-
*
17-
* @var array
18-
*/
19-
public static $cached = [
20-
'zip3' => null,
21-
'zip5' => null,
22-
];
14+
public static $cached = ['zip3' => null, 'zip5' => null];
2315

24-
/**
25-
* $path.
26-
*
27-
* @var string
28-
*/
16+
/** @var string */
2917
public $path;
3018

31-
/**
32-
* $suffix.
33-
*
34-
* @var string
35-
*/
3619
public $suffix = '.rules';
3720

38-
/**
39-
* __construct.
40-
*
41-
* @param string $path
42-
*/
43-
public function __construct($path = null)
21+
public function __construct(?string $path = null)
4422
{
45-
$this->path = ($path ?: dirname(dirname(__DIR__)).'/resources/data').'/';
23+
$this->path = ($path ?: dirname(__DIR__, 2).'/resources/data').'/';
4624
}
4725

48-
/**
49-
* zip3.
50-
*
51-
* @return JString
52-
*/
53-
public function zip3(Address $address)
26+
public function zip3(Address $address): ?string
5427
{
5528
$this->restore('zip3');
5629

5730
foreach ([2, 1] as $value) {
5831
$flat = $address->flat($value);
5932
if (isset(self::$cached['zip3'][$flat])) {
60-
$zip3 = self::$cached['zip3'][$flat];
61-
62-
return $zip3;
33+
return self::$cached['zip3'][$flat];
6334
}
6435
}
6536

6637
return null;
6738
}
6839

69-
/**
70-
* load.
71-
*
72-
* @param string $source
73-
* @return $this
74-
*/
75-
public function load($source)
40+
public function load(string $source): Storage
7641
{
7742
$zip5 = [];
7843
$zip3 = [];
79-
$this->each($this->prepareSource($source), function ($zipcode, $county, $district, $rules) use (&$zip5, &$zip3) {
80-
$zip5[$zipcode] = $this->compress(array_map(function ($rule) {
81-
return new Rule($rule);
82-
}, $rules));
83-
84-
if (isset($zip3[$county]) === false) {
85-
$zip3[$county] = substr($zipcode, 0, 1);
86-
}
44+
$this->each($this->prepareSource($source),
45+
function ($zipcode, $county, $district, $rules) use (&$zip5, &$zip3) {
46+
$zip5[$zipcode] = $this->compress(
47+
array_map(static function ($rule) {
48+
return new Rule($rule);
49+
}, $rules)
50+
);
51+
52+
if (isset($zip3[$county]) === false) {
53+
$zip3[$county] = substr($zipcode, 0, 1);
54+
}
8755

88-
if (isset($zip3[$county.$district]) === false) {
89-
$zip3[$county.$district] = substr($zipcode, 0, 3);
90-
}
91-
});
56+
if (isset($zip3[$county.$district]) === false) {
57+
$zip3[$county.$district] = substr($zipcode, 0, 3);
58+
}
59+
});
9260

9361
$this->store('zip3', $zip3);
9462
$this->store('zip5', $zip5);
9563

9664
return $this;
9765
}
9866

99-
/**
100-
* rules.
101-
*
102-
* @param string $zip3
103-
* @return JArray
104-
*/
105-
public function rules($zip3)
67+
public function rules(string $zip3): array
10668
{
10769
$this->restore('zip5');
10870

10971
return isset(self::$cached['zip5'][$zip3]) === true
110-
? new JArray($this->decompress(self::$cached['zip5'][$zip3]))
111-
: new JArray([]);
72+
? $this->decompress(self::$cached['zip5'][$zip3])
73+
: [];
11274
}
11375

114-
/**
115-
* loadFile.
116-
*
117-
* @param string $file
118-
* @return $this
119-
*/
120-
public function loadFile($file = null)
76+
public function loadFile(?string $file = null): Storage
12177
{
12278
$file = $file ?: $this->path.'../Zip32_utf8_10501_1.csv';
12379
$this->load($this->getSource($file));
12480

12581
return $this;
12682
}
12783

128-
/**
129-
* flush.
130-
*
131-
* @return $this
132-
*/
133-
public function flush()
84+
public function flush(): Storage
13485
{
135-
static::$cached = [
136-
'zip3' => null,
137-
'zip5' => null,
138-
];
86+
static::$cached = ['zip3' => null, 'zip5' => null];
13987

14088
return $this;
14189
}
14290

143-
/**
144-
* restore.
145-
*
146-
* @param string $filename
147-
* @return mixed
148-
*/
149-
private function restore($filename)
91+
private function restore(string $filename): void
15092
{
15193
if (self::$cached[$filename] !== null) {
152-
return self::$cached[$filename];
94+
return;
15395
}
15496

15597
if (file_exists($this->path.$filename.$this->suffix) === false) {
156-
return false;
98+
return;
15799
}
158100

159-
return self::$cached[$filename] = new JArray($this->decompress(
101+
self::$cached[$filename] = new JArray($this->decompress(
160102
file_get_contents($this->path.$filename.$this->suffix)
161103
));
162104
}
163105

164-
/**
165-
* getSource.
166-
*
167-
* @param string $file
168-
* @return string
169-
*/
170-
private function getSource($file)
106+
private function getSource(string $file): string
171107
{
172108
$extension = pathinfo($file, PATHINFO_EXTENSION);
173109

@@ -211,13 +147,7 @@ private function prepareSource($source)
211147
return $results;
212148
}
213149

214-
/**
215-
* each.
216-
*
217-
* @param array $ruleGroup
218-
* @param Closure $callback
219-
*/
220-
private function each($ruleGroup, $callback)
150+
private function each(array $ruleGroup, Closure $callback): void
221151
{
222152
foreach ($ruleGroup as $county => $districts) {
223153
foreach ($districts as $district => $addresses) {
@@ -230,40 +160,22 @@ private function each($ruleGroup, $callback)
230160

231161
/**
232162
* compress.
233-
*
234-
* @param array $array
235-
* @return string
236163
*/
237-
private function compress($array)
164+
private function compress(array $array): string
238165
{
239166
return gzcompress(serialize($array));
240167
}
241168

242169
/**
243170
* decompress.
244-
*
245-
* @param string $compressed
246-
* @return array
247171
*/
248-
private function decompress($compressed)
172+
private function decompress(string $compressed): array
249173
{
250-
return unserialize(gzuncompress($compressed));
174+
return unserialize(gzuncompress($compressed), ['max_depth' => 0]);
251175
}
252176

253-
/**
254-
* store.
255-
*
256-
* @param string $filename
257-
* @param JArray $data
258-
* @return $this
259-
*/
260-
private function store($filename, $data)
177+
private function store(string $filename, array $data): void
261178
{
262-
file_put_contents(
263-
$this->path.$filename.$this->suffix,
264-
$this->compress($data)
265-
);
266-
267-
return $this;
179+
file_put_contents($this->path.$filename.$this->suffix, $this->compress($data));
268180
}
269181
}

‎src/Tricky.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public function __construct()
4242
}
4343
}
4444

45-
/**
46-
* @return void
47-
*/
48-
private function init()
45+
private function init(): void
4946
{
5047
$tricky = ['', '', '', '', '', '', '', '', '', '新市', '阿里山', '鎮興里平'];
5148
self::$cached['hash'] = array_reduce($tricky, static function ($acc, $unit) {
@@ -71,20 +68,12 @@ private function init()
7168
];
7269
}
7370

74-
/**
75-
* @param Normalizer $normalizer
76-
* @return Normalizer
77-
*/
78-
public function hash($normalizer)
71+
public function hash(Normalizer $normalizer): Normalizer
7972
{
8073
return $normalizer->replace(self::$cached['replace']);
8174
}
8275

83-
/**
84-
* @param string $token
85-
* @return string
86-
*/
87-
public function flip($token)
76+
public function flip(string $token): string
8877
{
8978
return strtr($token, self::$cached['flip']);
9079
}

‎src/Zipcode.php

+10-52
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ class Zipcode
1818
'shortAddress' => null,
1919
];
2020

21-
/**
22-
* $address.
23-
*
24-
* @var Address
25-
*/
21+
/** @var Address */
2622
private $address;
2723

2824
/**
2925
* __construct.
30-
*
31-
* @param string $address
32-
* @param Rules $rules
3326
*/
34-
public function __construct($address, Rules $rules = null)
27+
public function __construct(string $address, ?Rules $rules = null)
3528
{
36-
$rules = $rules ?: new Rules();
29+
$rules = $rules ?: new Rules;
3730
$this->address = new Address($address);
3831
$zip = (string) $rules->match($this->address);
3932

@@ -55,72 +48,37 @@ public function __construct($address, Rules $rules = null)
5548
$this->attributes['address'] = $this->address->flat();
5649
}
5750

58-
/**
59-
* parse.
60-
*
61-
* @return static
62-
*/
63-
public static function parse($address, Rules $rules = null)
51+
public static function parse($address, ?Rules $rules = null): self
6452
{
6553
return new static($address, $rules);
6654
}
6755

68-
/**
69-
* zip3.
70-
*
71-
* @return string
72-
*/
73-
public function zip3()
56+
public function zip3(): ?string
7457
{
7558
return $this->attributes['zip3'];
7659
}
7760

78-
/**
79-
* zip5.
80-
*
81-
* @return string
82-
*/
83-
public function zip5()
61+
public function zip5(): ?string
8462
{
8563
return $this->attributes['zip5'];
8664
}
8765

88-
/**
89-
* county.
90-
*
91-
* @return string
92-
*/
93-
public function county()
66+
public function county(): ?string
9467
{
9568
return $this->attributes['county'];
9669
}
9770

98-
/**
99-
* district.
100-
*
101-
* @return string
102-
*/
103-
public function district()
71+
public function district(): ?string
10472
{
10573
return $this->attributes['district'];
10674
}
10775

108-
/**
109-
* address.
110-
*
111-
* @return string
112-
*/
113-
public function address()
76+
public function address(): ?string
11477
{
11578
return $this->attributes['address'];
11679
}
11780

118-
/**
119-
* shortAddress.
120-
*
121-
* @return string
122-
*/
123-
public function shortAddress()
81+
public function shortAddress(): ?string
12482
{
12583
return $this->attributes['shortAddress'];
12684
}

‎tests/Moskytw/stubs/Directory.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class Directory
99
{
1010
private $storage;
11+
1112
private $rules;
1213

1314
public function __construct($root)

‎tests/RulesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function testMatchGradually()
128128
public function testZip3North()
129129
{
130130
File::$cached = ['zip3' => null, 'zip5' => null];
131-
$rules = new Rules();
131+
$rules = new Rules;
132132
$this->assertSame('100', $rules->match('台北市中正區'.uniqid()));
133133
$this->assertSame('103', $rules->match('台北市大同區'.uniqid()));
134134
$this->assertSame('104', $rules->match('台北市中山區'.uniqid()));

‎tests/Storages/FileTest.php

+9-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
namespace Recca0120\Twzipcode\Tests\Storages;
44

5-
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
65
use Mockery as m;
6+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
77
use org\bovigo\vfs\vfsStream;
88
use PHPUnit\Framework\TestCase;
9-
use Recca0120\Twzipcode\Storages\File;
109
use Recca0120\Twzipcode\Address;
10+
use Recca0120\Twzipcode\Storages\File as Storage;
1111

1212
class FileTest extends TestCase
1313
{
1414
use MockeryPHPUnitIntegration;
1515

16+
private $root;
1617
private $storage;
1718

1819
protected function setUp(): void
1920
{
20-
$root = vfsStream::setup();
21-
$this->storage = new File($root->url());
21+
$this->root = vfsStream::setup();
22+
$this->storage = new Storage($this->root->url());
2223
$this->storage->flush()->load('
2324
10058,臺北市,中正區,八德路1段,全
2425
10079,臺北市,中正區,三元街,單全
@@ -87,7 +88,7 @@ protected function setUp(): void
8788

8889
public function testDefaultPath(): void
8990
{
90-
$storage = new File();
91+
$storage = new Storage;
9192
$this->assertEquals(realpath(__DIR__.'/../../resources/data').'/', $storage->path);
9293
}
9394

@@ -116,14 +117,9 @@ public function testRules(): void
116117

117118
public function testLoadResources(): void
118119
{
119-
File::$cached = [
120-
'zip3' => null,
121-
'zip5' => null,
122-
];
123-
$root = vfsStream::setup();
124-
$storage = new File($root->url());
125-
$storage->flush()
126-
->loadFile(__DIR__.'/../../resources/Zip32_utf8_10501_1.zip');
120+
Storage::$cached = ['zip3' => null, 'zip5' => null];
121+
$storage = new Storage($this->root->url());
122+
$storage->flush()->loadFile(__DIR__.'/../../resources/Zip32_utf8_10501_1.zip');
127123

128124
$address = m::mock(Address::class);
129125

‎tests/ZipcodeTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function setUp(): void
4747
public function testZipcode(): void
4848
{
4949
$address = '台中市大里區金城里14鄰塗城路9478巷9478弄9478號';
50-
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules());
50+
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules);
5151

5252
$this->assertSame('412', $zipcode->zip3());
5353
$this->assertSame('41275', $zipcode->zip5());
@@ -60,7 +60,7 @@ public function testZipcode(): void
6060
public function testZipcode2(): void
6161
{
6262
$address = '宜蘭縣宜蘭市金城里14鄰慶和街同興巷9478弄9478號';
63-
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules());
63+
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules);
6464

6565
$this->assertSame('260', $zipcode->zip3());
6666
$this->assertSame('26044', $zipcode->zip5());
@@ -73,7 +73,7 @@ public function testZipcode2(): void
7373
public function testZipcode3(): void
7474
{
7575
$address = '台北市中正區中華路1段25號';
76-
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules());
76+
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules);
7777

7878
$this->assertSame('100', $zipcode->zip3());
7979
$this->assertSame('10043', $zipcode->zip5());
@@ -86,7 +86,7 @@ public function testZipcode3(): void
8686
public function testZipcode4(): void
8787
{
8888
$address = '宜蘭縣壯圍鄉環市東路1段374號';
89-
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules());
89+
$zipcode = Zipcode::parse($address, $this->rules ?: new Rules);
9090

9191
$this->assertSame('260', $zipcode->zip3());
9292
$this->assertSame('26060', $zipcode->zip5());

0 commit comments

Comments
 (0)
Please sign in to comment.