Complex classes like Rule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Rule |
||
9 | { |
||
10 | /** |
||
11 | * $zip3. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | public $zip3; |
||
16 | |||
17 | /** |
||
18 | * $zip5. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | public $zip5; |
||
23 | |||
24 | /** |
||
25 | * $tokens. |
||
26 | * |
||
27 | * @var \Recca0120\Twzipcode\Address |
||
28 | */ |
||
29 | public $address; |
||
30 | |||
31 | /** |
||
32 | * $tokens. |
||
33 | * |
||
34 | * @var \Recca0120\Lodash\JArray |
||
35 | */ |
||
36 | public $tokens; |
||
37 | |||
38 | /** |
||
39 | * __construct. |
||
40 | * |
||
41 | * @param string $rule |
||
42 | */ |
||
43 | 62 | public function __construct($rule) |
|
44 | { |
||
45 | 62 | if (preg_match('/^(\d+),?(.*)/', $rule, $m)) { |
|
46 | 51 | $this->zip5 = $m[1]; |
|
47 | 51 | $this->zip3 = substr($this->zip5, 0, 3); |
|
48 | 51 | $rule = $m[2]; |
|
49 | } |
||
50 | |||
51 | 62 | $this->tokens = $this->tokenize( |
|
52 | $rule, |
||
53 | function ($address) { |
||
54 | 62 | $this->address = new Address($address); |
|
55 | 62 | } |
|
56 | ); |
||
57 | 62 | } |
|
58 | |||
59 | /** |
||
60 | * zip3. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 1 | public function zip3() |
|
68 | |||
69 | /** |
||
70 | * zip5. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 23 | public function zip5() |
|
78 | |||
79 | /** |
||
80 | * zip. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function zip() |
||
88 | |||
89 | /** |
||
90 | * tokens. |
||
91 | * |
||
92 | * @return \Recca0120\Lodash\JArray |
||
93 | */ |
||
94 | 18 | public function tokens() |
|
98 | |||
99 | /** |
||
100 | * match. |
||
101 | * |
||
102 | * @param Address|string $address |
||
103 | * @return bool |
||
104 | */ |
||
105 | 38 | public function match($address) |
|
155 | |||
156 | /** |
||
157 | * normalizeAddress. |
||
158 | * |
||
159 | * @param Address $ruleAddressTokens |
||
160 | * @param \Recca0120\Lodash\JArray $ruleAddressTokens |
||
161 | * @return array |
||
162 | */ |
||
163 | 38 | protected function normalizeAddress(Address $address, JArray $ruleAddressTokens) |
|
177 | |||
178 | /** |
||
179 | * equalsToken. |
||
180 | * |
||
181 | * @param \Recca0120\Lodash\JArray $ruleAddressTokens |
||
182 | * @param \Recca0120\Lodash\JArray $addressTokens |
||
183 | * @param int $cur |
||
184 | * @return bool |
||
185 | */ |
||
186 | 38 | protected function equalsToken($ruleAddressTokens, $addressTokens, $cur) |
|
202 | |||
203 | /** |
||
204 | * normalize. |
||
205 | * |
||
206 | * @param string $rule |
||
207 | * @return \Recca0120\Twzipcode\Normalizer |
||
208 | */ |
||
209 | 62 | protected function normalize($rule) |
|
227 | |||
228 | /** |
||
229 | * tokenize. |
||
230 | * |
||
231 | * @param string $rule |
||
232 | * @param \Closure $addressResolver |
||
233 | * @return \Recca0120\Lodash\JArray |
||
234 | */ |
||
235 | 62 | protected function tokenize($rule, Closure $addressResolver) |
|
259 | } |
||
260 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.