Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Pool 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 Pool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Pool extends Client |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var array Record Types [code => "name", ...] |
||
19 | */ |
||
20 | public static $type = [ |
||
21 | 1 => 'A', |
||
22 | 2 => 'NS', |
||
23 | 3 => 'MD', |
||
24 | 4 => 'MF', |
||
25 | 5 => 'CNAME', |
||
26 | 6 => 'SOA', |
||
27 | 7 => 'MB', |
||
28 | 8 => 'MG', |
||
29 | 9 => 'MR', |
||
30 | 10 => 'RR', |
||
31 | 11 => 'WKS', |
||
32 | 12 => 'PTR', |
||
33 | 13 => 'HINFO', |
||
34 | 14 => 'MINFO', |
||
35 | 15 => 'MX', |
||
36 | 16 => 'TXT', |
||
37 | 17 => 'RP', |
||
38 | 18 => 'AFSDB', |
||
39 | 19 => 'X25', |
||
40 | 20 => 'ISDN', |
||
41 | 21 => 'RT', |
||
42 | 22 => 'NSAP', |
||
43 | 23 => 'NSAP-PTR', |
||
44 | 24 => 'SIG', |
||
45 | 25 => 'KEY', |
||
46 | 26 => 'PX', |
||
47 | 27 => 'GPOS', |
||
48 | 28 => 'AAAA', |
||
49 | 29 => 'LOC', |
||
50 | 30 => 'NXT', |
||
51 | 31 => 'EID', |
||
52 | 32 => 'NIMLOC', |
||
53 | 33 => 'SRV', |
||
54 | 34 => 'ATMA', |
||
55 | 35 => 'NAPTR', |
||
56 | 36 => 'KX', |
||
57 | 37 => 'CERT', |
||
58 | 38 => 'A6', |
||
59 | 39 => 'DNAME', |
||
60 | 40 => 'SINK', |
||
61 | 41 => 'OPT', |
||
62 | 42 => 'APL', |
||
63 | 43 => 'DS', |
||
64 | 44 => 'SSHFP', |
||
65 | 45 => 'IPSECKEY', |
||
66 | 46 => 'RRSIG', |
||
67 | 47 => 'NSEC', |
||
68 | 48 => 'DNSKEY', |
||
69 | 49 => 'DHCID', |
||
70 | 50 => 'NSEC3', |
||
71 | 51 => 'NSEC3PARAM', |
||
72 | 55 => 'HIP', |
||
73 | 99 => 'SPF', |
||
74 | 100 => 'UINFO', |
||
75 | 101 => 'UID', |
||
76 | 102 => 'GID', |
||
77 | 103 => 'UNSPEC', |
||
78 | 249 => 'TKEY', |
||
79 | 250 => 'TSIG', |
||
80 | 251 => 'IXFR', |
||
81 | 252 => 'AXFR', |
||
82 | 253 => 'MAILB', |
||
83 | 254 => 'MAILA', |
||
84 | 255 => 'ALL', |
||
85 | 32768 => 'TA', |
||
86 | 32769 => 'DLV', |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * @var array Hosts file parsed [hostname => [addr, ...], ...] |
||
91 | */ |
||
92 | public $hosts = []; |
||
93 | |||
94 | /** |
||
95 | * @var \PHPDaemon\Core\ComplexJob Preloading ComplexJob |
||
96 | */ |
||
97 | public $preloading; |
||
98 | |||
99 | /** |
||
100 | * @var CappedStorageHits Resolve cache |
||
101 | */ |
||
102 | public $resolveCache; |
||
103 | |||
104 | /** |
||
105 | * @var array Classes [code => "class"] |
||
106 | */ |
||
107 | public static $class = [ |
||
108 | 1 => 'IN', |
||
109 | 3 => 'CH', |
||
110 | 255 => 'ANY', |
||
111 | ]; |
||
112 | |||
113 | /** |
||
114 | * @var array resolve.conf file parsed |
||
115 | */ |
||
116 | public $nameServers = []; |
||
117 | |||
118 | /** |
||
119 | * Constructor |
||
120 | */ |
||
121 | protected function init() |
||
125 | |||
126 | /** |
||
127 | * Setting default config options |
||
128 | * Overriden from NetworkClient::getConfigDefaults |
||
129 | * @return array |
||
130 | */ |
||
131 | protected function getConfigDefaults() |
||
153 | |||
154 | /** |
||
155 | * Applies config |
||
156 | * @return void |
||
157 | */ |
||
158 | public function applyConfig() |
||
159 | { |
||
160 | parent::applyConfig(); |
||
161 | $pool = $this; |
||
162 | if (!isset($this->preloading)) { |
||
163 | $this->preloading = new ComplexJob(); |
||
164 | } |
||
165 | $job = $this->preloading; |
||
166 | $job->addJob('resolvfile', function ($jobname, $job) use ($pool) { |
||
167 | FileSystem::readfile($pool->config->resolvfile->value, function ($file, $data) use ($pool, $job, $jobname) { |
||
168 | if ($file) { |
||
169 | foreach (explode("\n", $data) as $line) { |
||
170 | $line = trim($line); |
||
171 | if ($line !== '' && $line[0] !== '#' && preg_match('~nameserver ([^\r\n;]+)~i', $line, $m)) { |
||
172 | $pool->nameServers[] = $m[1]; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | $job->setResult($jobname); |
||
177 | }); |
||
178 | }); |
||
179 | $job->addJob('hostsfile', function ($jobname, $job) use ($pool) { |
||
180 | FileSystem::readfile($pool->config->hostsfile->value, function ($file, $data) use ($pool, $job, $jobname) { |
||
181 | if ($file) { |
||
182 | preg_match_all('~^(\S+)\s+([^\r\n]+)\s*~m', $data, $m, PREG_SET_ORDER); |
||
183 | $pool->hosts = []; |
||
184 | foreach ($m as $h) { |
||
185 | $hosts = preg_split('~\s+~', $h[2]); |
||
186 | $ip = $h[1]; |
||
187 | foreach ($hosts as $host) { |
||
188 | $host = rtrim($host, '.') . '.'; |
||
189 | $pool->hosts[$host][] = $ip; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | $job->setResult($jobname); |
||
194 | }); |
||
195 | }); |
||
196 | $job(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Resolves the host |
||
201 | * @param string $hostname Hostname |
||
202 | * @param callable $cb Callback |
||
203 | * @param boolean $noncache Noncache? |
||
204 | * @param array $nameServers |
||
205 | * @callback $cb ( array|string $addrs ) |
||
206 | * @return void |
||
207 | */ |
||
208 | public function resolve($hostname, $cb, $noncache = false, $nameServers = []) |
||
272 | |||
273 | /** |
||
274 | * Gets the host information |
||
275 | * @param string $hostname Hostname |
||
276 | * @param callable $cb Callback |
||
277 | * @param boolean $noncache Noncache? |
||
278 | * @param array $nameServers |
||
279 | * @param string $proto |
||
280 | * @callback $cb ( ) |
||
281 | * @return void |
||
282 | */ |
||
283 | public function get($hostname, $cb, $noncache = false, $nameServers = [], $proto = 'udp') |
||
343 | } |
||
344 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.