Address   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 242
ccs 94
cts 94
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __toString() 0 4 1
A tokens() 0 4 1
A set() 0 10 1
A flat() 0 9 2
A getPoint() 0 12 4
B tokenize() 0 107 5
1
<?php
2
3
namespace Recca0120\Twzipcode;
4
5
use Recca0120\Lodash\JArray;
6
7
class Address
8
{
9
    /**
10
     * NO.
11
     *
12
     * @var int
13
     */
14
    const NO = 0;
15
16
    /**
17
     * SUBNO.
18
     *
19
     * @var int
20
     */
21
    const SUBNO = 1;
22
23
    /**
24
     * NAME.
25
     *
26
     * @var int
27
     */
28
    const NAME = 2;
29
30
    /**
31
     * UNIT.
32
     *
33
     * @var int
34
     */
35
    const UNIT = 3;
36
37
    /**
38
     * $normalizer.
39
     *
40
     * @var \Recca0120\Twzipcode\Normalizer
41
     */
42
    public $normalizer;
43
44
    /**
45
     * $tokens.
46
     *
47
     * @var \Recca0120\Lodash\JArray
48
     */
49
    public $tokens = [];
50
51
    /**
52
     * __construct.
53
     *
54
     * @param static|array $address
55
     */
56 73
    public function __construct($address = '')
57
    {
58 73
        if (empty($address) === false) {
59 73
            $this->set($address);
60 73
        }
61 73
    }
62
63
    /**
64
     * __toString.
65
     *
66
     * @return string
67
     */
68 5
    public function __toString()
69
    {
70 5
        return $this->normalizer->value();
71
    }
72
73
    /**
74
     * set.
75
     *
76
     * @param static|string $address
77
     * @return $this
78
     */
79 73
    public function set($address)
80
    {
81 73
        $this->normalizer = (new Normalizer($address))
0 ignored issues
show
Bug introduced by
It seems like $address defined by parameter $address on line 79 can also be of type object<Recca0120\Twzipcode\Address>; however, Recca0120\Lodash\JString::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82 73
            ->normalize()
83 73
            ->normalizeAddress();
84
85 73
        $this->tokens = $this->tokenize();
86
87 73
        return $this;
88
    }
89
90
    /**
91
     * tokens.
92
     *
93
     * @return \Recca0120\Lodash\JArray
94
     */
95 67
    public function tokens()
96
    {
97 67
        return $this->tokens;
98
    }
99
100
    /**
101
     * getPoint.
102
     *
103
     * @param string $index
104
     * @return \Recca0120\Twzipcode\Point
105
     */
106 37
    public function getPoint($index)
107
    {
108 37
        if (isset($this->tokens[$index]) === false) {
109 9
            return new Point(0, 0);
110
        }
111 37
        $token = $this->tokens[$index];
112
113 37
        return new Point(
114 37
            (int) $token[static::NO] ?: 0,
115 37
            (int) str_replace('之', '', $token[static::SUBNO] ?: '0')
116 37
        );
117
    }
118
119
    /**
120
     * flat.
121
     *
122
     * @param int $length
123
     * @param int $offset
124
     * @return string
125
     */
126 7
    public function flat($length = null, $offset = 0)
127
    {
128 7
        $length = $length ?: $this->tokens->length();
129 7
        $end = $offset + $length;
130
131 7
        return (string) $this->tokens->slice($offset, $end)->map(function ($token) {
132 7
            return implode('', $token);
133 7
        })->join('');
134
    }
135
136
    /**
137
     * tokenize.
138
     *
139
     * @return \Recca0120\Lodash\JArray
140
     */
141 73
    protected function tokenize()
142
    {
143 73
        $tokens = [];
144
145
        $units = [
146 73
            static::NO => 'no',
147 73
            static::SUBNO => 'subno',
148 73
            static::NAME => 'name',
149 73
            static::UNIT => 'unit',
150 73
        ];
151
152 73
        $patterns = implode('', [
153 73
            '(?:(?P<no>\d+)(?P<subno>之\d+)?(?=[巷弄號樓]|$)|(?P<name>.+?))',
154 73
            '(?:(?P<unit>([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\d+(?:之\d+)?[巷弄號樓]|$))',
155 73
        ]);
156
157
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
         * 20742,新北市,萬里區,二坪,全
159
         * 21042,連江縣,北竿鄉,坂里村,全
160
         * 24944,新北市,八里區,八里大道,全
161
         * 32058,桃園市,中壢區,華夏一村市場,全
162
         * 32464,桃園市,平鎮區,三和路,全
163
         * 32460,桃園市,平鎮區,鎮興里平鎮,連 123號至 139號
164
         * 41273,臺中市,大里區,三民一街,全
165
         * 42147,臺中市,后里區,七星街,全
166
         * 51547,彰化縣,大村鄉,大仁路1段,全
167
         * 52441,彰化縣,溪州鄉,村市路,全
168
         * 54544,南投縣,埔里鎮,一新一巷,全
169
         * 55347,南投縣,水里鄉,一廍路,全
170
         * 60243,嘉義縣,番路鄉,三橋仔,全
171
         * 60541,嘉義縣,阿里山鄉,二萬平,全
172
         * 60845,嘉義縣,水上鄉,鄉村世界,全
173
         * 71342,臺南市,左鎮區,二寮,全
174
         * 72270,臺南市,佳里區,下廍,全
175
         * 74145,臺南市,新市區,大利一路,全
176
         * 80652,高雄市,前鎮區,一心一路,單 239號以下
177
         * 83043,高雄市,鳳山區,海光四村市場,全
178
         * 90542,屏東縣,里港鄉,八德路,全
179
         * 96341,臺東縣,太麻里鄉,千禧街,全
180
         * 98191,花蓮縣,玉里鎮,三民,全
181
         * 98342,花蓮縣,富里鄉,三台,全
182
         * 98392,花蓮縣,富里鄉,東里村復興,全
183
         * 89442,金門縣,烈嶼鄉,二擔,全
184
         */
185
186
        $trickies = [
187 73
            '島' => md5('島'),
188 73
            '嶼' => md5('嶼'),
189 73
            '鄉' => md5('鄉'),
190 73
            '市' => md5('市'),
191 73
            '鎮' => md5('鎮'),
192 73
            '區' => md5('區'),
193 73
            '村' => md5('村'),
194 73
            '里' => md5('里'),
195 73
            '路' => md5('路'),
196 73
            '新市' => md5('新市'),
197 73
            '阿里山' => md5('阿里山'),
198 73
            '鎮興里平' => md5('鎮興里平'),
199 73
        ];
200
201
        $map = [
202 73
            '島鄉' => $trickies['島'].'鄉',
203 73
            '嶼鄉' => $trickies['嶼'].'鄉',
204 73
            '村鄉' => $trickies['村'].'鄉',
205 73
            '里鄉' => $trickies['里'].'鄉',
206 73
            '村市' => $trickies['村'].'市',
207 73
            '里區' => $trickies['里'].'區',
208 73
            '鎮區' => $trickies['鎮'].'區',
209 73
            '里鎮' => $trickies['里'].'鎮',
210 73
            '里村' => $trickies['里'].'村',
211 73
            '鄉村' => $trickies['鄉'].'村',
212 73
            '路鄉' => $trickies['路'].'鄉',
213 73
            '新市區' => $trickies['新市'].'區',
214 73
            '阿里山鄉' => $trickies['阿里山'].'鄉',
215 73
            '鎮興里平鎮' => $trickies['鎮興里平'].'鎮',
216 73
        ];
217
218
        $flip = [
219 73
            $trickies['島'] => '島',
220 73
            $trickies['嶼'] => '嶼',
221 73
            $trickies['鄉'] => '鄉',
222 73
            $trickies['市'] => '市',
223 73
            $trickies['鎮'] => '鎮',
224 73
            $trickies['區'] => '區',
225 73
            $trickies['村'] => '村',
226 73
            $trickies['里'] => '里',
227 73
            $trickies['路'] => '路',
228 73
            $trickies['新市'] => '新市',
229 73
            $trickies['阿里山'] => '阿里山',
230 73
            $trickies['鎮興里平'] => '鎮興里平',
231 73
        ];
232
233 73
        $address = $this->normalizer->replace($map)->value();
234 73
        $matches = [];
235 73
        if (preg_match_all('/'.$patterns.'/u', $address, $matches, PREG_SET_ORDER) !== false) {
236 73
            foreach ($matches as $values) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
237 73
                $temp = [];
238 73
                foreach ($units as $key => $unit) {
239 73
                    $temp[$key] = isset($values[$unit]) === true ? $values[$unit] : '';
240 73
                }
241 73
                $temp[static::NAME] = strtr($temp[static::NAME], $flip);
242 73
                $tokens[] = $temp;
243 73
            }
244 73
        }
245
246 73
        return new JArray($tokens);
247
    }
248
}
249