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:
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', 2 => 'NS', 3 => 'MD', 4 => 'MF', 5 => 'CNAME', |
||
22 | 6 => 'SOA', 7 => 'MB', 8 => 'MG', 9 => 'MR', 10 => 'RR', |
||
23 | 11 => 'WKS', 12 => 'PTR', 13 => 'HINFO', 14 => 'MINFO', |
||
24 | 15 => 'MX', 16 => 'TXT', 17 => 'RP', 18 => 'AFSDB', |
||
25 | 19 => 'X25', 20 => 'ISDN', |
||
26 | 21 => 'RT', 22 => 'NSAP', 23 => 'NSAP-PTR', 24 => 'SIG', |
||
27 | 25 => 'KEY', 26 => 'PX', 27 => 'GPOS', 28 => 'AAAA', 29 => 'LOC', |
||
28 | 30 => 'NXT', 31 => 'EID', 32 => 'NIMLOC', 33 => 'SRV', |
||
29 | 34 => 'ATMA', 35 => 'NAPTR', 36 => 'KX', 37 => 'CERT', 38 => 'A6', |
||
30 | 39 => 'DNAME', 40 => 'SINK', 41 => 'OPT', 42 => 'APL', 43 => 'DS', |
||
31 | 44 => 'SSHFP', 45 => 'IPSECKEY', 46 => 'RRSIG', 47 => 'NSEC', |
||
32 | 48 => 'DNSKEY', 49 => 'DHCID', 50 => 'NSEC3', 51 => 'NSEC3PARAM', |
||
33 | 55 => 'HIP', 99 => 'SPF', 100 => 'UINFO', 101 => 'UID', 102 => 'GID', |
||
34 | 103 => 'UNSPEC', 249 => 'TKEY', 250 => 'TSIG', 251 => 'IXFR', |
||
35 | 252 => 'AXFR', 253 => 'MAILB', 254 => 'MAILA', 255 => 'ALL', |
||
36 | 32768 => 'TA', 32769 => 'DLV', |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * @var array Hosts file parsed [hostname => [addr, ...], ...] |
||
41 | */ |
||
42 | public $hosts = []; |
||
43 | |||
44 | /** |
||
45 | * @var \PHPDaemon\Core\ComplexJob Preloading ComplexJob |
||
46 | */ |
||
47 | public $preloading; |
||
48 | |||
49 | /** |
||
50 | * @var CappedStorageHits Resolve cache |
||
51 | */ |
||
52 | public $resolveCache; |
||
53 | |||
54 | /** |
||
55 | * @var array Classes [code => "class"] |
||
56 | */ |
||
57 | public static $class = [ |
||
58 | 1 => 'IN', |
||
59 | 3 => 'CH', |
||
60 | 255 => 'ANY', |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * @var array resolve.conf file parsed |
||
65 | */ |
||
66 | public $nameServers = []; |
||
67 | |||
68 | /** |
||
69 | * Constructor |
||
70 | */ |
||
71 | protected function init() |
||
75 | |||
76 | /** |
||
77 | * Setting default config options |
||
78 | * Overriden from NetworkClient::getConfigDefaults |
||
79 | * @return array |
||
80 | */ |
||
81 | protected function getConfigDefaults() |
||
103 | |||
104 | /** |
||
105 | * Applies config |
||
106 | * @return void |
||
107 | */ |
||
108 | public function applyConfig() |
||
146 | |||
147 | /** |
||
148 | * Resolves the host |
||
149 | * @param string $hostname Hostname |
||
150 | * @param callable $cb Callback |
||
151 | * @param boolean $noncache Noncache? |
||
152 | * @param array $nameServers |
||
153 | * @callback $cb ( array|string $addrs ) |
||
154 | * @return void |
||
155 | */ |
||
156 | public function resolve($hostname, $cb, $noncache = false, $nameServers = []) |
||
157 | { |
||
158 | View Code Duplication | if (!$this->preloading->hasCompleted()) { |
|
159 | $pool = $this; |
||
160 | $this->preloading->addListener(function ($job) use ($hostname, $cb, $noncache, $pool, $nameServers) { |
||
161 | $pool->resolve($hostname, $cb, $noncache, $nameServers); |
||
162 | }); |
||
163 | return; |
||
164 | } |
||
165 | $hostname = rtrim($hostname, '.') . '.'; |
||
166 | if (isset($this->hosts[$hostname])) { |
||
167 | $cb($this->hosts[$hostname]); |
||
168 | return; |
||
169 | } |
||
170 | if (!$noncache && ($item = $this->resolveCache->get($hostname))) { // cache hit |
||
171 | $ip = $item->getValue(); |
||
172 | if ($ip === null) { // operation in progress |
||
173 | $item->addListener($cb); |
||
174 | } else { // hit |
||
175 | $cb($ip); |
||
176 | } |
||
177 | return; |
||
178 | } elseif (!$noncache) { |
||
179 | $item = $this->resolveCache->put($hostname, null); |
||
180 | $item->addListener($cb); |
||
181 | } |
||
182 | $pool = $this; |
||
183 | $this->get($hostname, function ($response) use ($cb, $noncache, $hostname, $pool) { |
||
184 | if (!isset($response['A'])) { |
||
185 | if ($noncache) { |
||
186 | $cb(false); |
||
187 | } else { |
||
188 | $pool->resolveCache->put($hostname, false, 5); // 5 - TTL of unsuccessful request |
||
189 | } |
||
190 | return; |
||
191 | } |
||
192 | if (!isset($response['A']) && !isset($response['AAAA'])) { |
||
193 | $cb(false); |
||
194 | return; |
||
195 | } |
||
196 | $addrs = []; |
||
197 | $ttl = 0; |
||
198 | View Code Duplication | if (isset($response['A'])) { |
|
199 | foreach ($response['A'] as $r) { |
||
200 | $addrs[] = $r['ip']; |
||
201 | $ttl = $r['ttl']; |
||
202 | } |
||
203 | } |
||
204 | View Code Duplication | if (isset($response['AAAA'])) { |
|
205 | foreach ($response['AAAA'] as $r) { |
||
206 | $addrs[] = $r['ip']; |
||
207 | $ttl = $r['ttl']; |
||
208 | } |
||
209 | } |
||
210 | if (sizeof($addrs) === 1) { |
||
211 | $addrs = $addrs[0]; |
||
212 | } |
||
213 | if ($noncache) { |
||
214 | $cb($addrs); |
||
215 | } else { |
||
216 | $pool->resolveCache->put($hostname, $addrs, $ttl); |
||
217 | } |
||
218 | }, $noncache, $nameServers); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Gets the host information |
||
223 | * @param string $hostname Hostname |
||
224 | * @param callable $cb Callback |
||
225 | * @param boolean $noncache Noncache? |
||
226 | * @param array $nameServers |
||
227 | * @param string $proto |
||
228 | * @callback $cb ( ) |
||
229 | * @return void |
||
230 | */ |
||
231 | public function get($hostname, $cb, $noncache = false, $nameServers = [], $proto = 'udp') |
||
281 | } |
||
282 |
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.