Complex classes like Ares 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 Ares, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Ares |
||
17 | { |
||
18 | const URL_BAS = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=%s'; |
||
19 | const URL_RES = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_res.cgi?ICO=%s'; |
||
20 | const URL_TAX = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_es.cgi?ico=%s&filtr=0'; |
||
21 | const URL_FIND = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_es.cgi?obch_jm=%s&obec=%s&filtr=0'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $cacheStrategy = 'YW'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $cacheDir = null; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $debug; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $balancer = null; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $contextOptions = [ |
||
47 | 'ssl' => [ |
||
48 | 'verify_peer' => false, |
||
49 | 'verify_peer_name' => false, |
||
50 | ], |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $lastUrl; |
||
57 | |||
58 | /** |
||
59 | * @param null $cacheDir |
||
60 | * @param bool $debug |
||
61 | */ |
||
62 | 3 | public function __construct($cacheDir = null, $debug = false, $balancer = null) |
|
80 | |||
81 | /** |
||
82 | * @param $balancer |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 1 | public function setBalancer($balancer) |
|
92 | |||
93 | /** |
||
94 | * @param $url |
||
95 | * |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 3 | private function wrapUrl($url) |
|
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | 1 | public function getLastUrl() |
|
116 | |||
117 | /** |
||
118 | * @param $id |
||
119 | * |
||
120 | * @throws InvalidArgumentException |
||
121 | * @throws Ares\AresException |
||
122 | * |
||
123 | * @return AresRecord |
||
124 | */ |
||
125 | 3 | public function findByIdentificationNumber($id) |
|
126 | { |
||
127 | 3 | $id = Lib::toInteger($id); |
|
128 | 3 | $this->ensureIdIsInteger($id); |
|
129 | |||
130 | 3 | $cachedFileName = $id.'_'.date($this->cacheStrategy).'.php'; |
|
131 | 3 | $cachedFile = $this->cacheDir.'/bas_'.$cachedFileName; |
|
132 | 3 | $cachedRawFile = $this->cacheDir.'/bas_raw_'.$cachedFileName; |
|
133 | |||
134 | 3 | if (is_file($cachedFile)) { |
|
135 | return unserialize(file_get_contents($cachedFile)); |
||
136 | } |
||
137 | |||
138 | // Sestaveni URL |
||
139 | 3 | $url = $this->wrapUrl(sprintf(self::URL_BAS, $id)); |
|
140 | |||
141 | try { |
||
142 | 3 | $aresRequest = file_get_contents($url, null, stream_context_create($this->contextOptions)); |
|
143 | 2 | if ($this->debug) { |
|
144 | file_put_contents($cachedRawFile, $aresRequest); |
||
145 | } |
||
146 | 2 | $aresResponse = simplexml_load_string($aresRequest); |
|
147 | |||
148 | 2 | if ($aresResponse) { |
|
149 | 2 | $ns = $aresResponse->getDocNamespaces(); |
|
150 | 2 | $data = $aresResponse->children($ns['are']); |
|
151 | 2 | $elements = $data->children($ns['D'])->VBAS; |
|
|
|||
152 | |||
153 | 2 | $ico = (int) $elements->ICO; |
|
154 | 2 | if ($ico !== $id) { |
|
155 | throw new AresException('IČ firmy nebylo nalezeno.'); |
||
156 | } |
||
157 | |||
158 | 2 | $record = new AresRecord(); |
|
159 | |||
160 | 2 | $record->setCompanyId($id); |
|
161 | 2 | $record->setTaxId(strval($elements->DIC)); |
|
162 | 2 | $record->setCompanyName(strval($elements->OF)); |
|
163 | 2 | $record->setStreet(strval($elements->AA->NU)); |
|
164 | |||
165 | 1 | if (strval($elements->AA->CO)) { |
|
166 | 1 | $record->setStreetHouseNumber(strval($elements->AA->CD)); |
|
167 | 1 | $record->setStreetOrientationNumber(strval($elements->AA->CO)); |
|
168 | } else { |
||
169 | $record->setStreetHouseNumber(strval($elements->AA->CD)); |
||
170 | } |
||
171 | |||
172 | 1 | if (strval($elements->AA->NCO)) { |
|
173 | 1 | $record->setTown(strval($elements->AA->N.' - '.strval($elements->AA->NCO))); |
|
174 | } else { |
||
175 | $record->setTown(strval($elements->AA->N)); |
||
176 | } |
||
177 | |||
178 | 1 | $record->setZip(strval($elements->AA->PSC)); |
|
179 | } else { |
||
180 | 1 | throw new AresException('Databáze ARES není dostupná.'); |
|
181 | } |
||
182 | 2 | } catch (\Exception $e) { |
|
183 | 2 | throw new AresException($e->getMessage()); |
|
184 | } |
||
185 | |||
186 | 1 | file_put_contents($cachedFile, serialize($record)); |
|
187 | |||
188 | 1 | return $record; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param $id |
||
193 | * |
||
194 | * @throws InvalidArgumentException |
||
195 | * @throws Ares\AresException |
||
196 | * |
||
197 | * @return AresRecord |
||
198 | */ |
||
199 | public function findInResById($id) |
||
250 | |||
251 | /** |
||
252 | * @param $id |
||
253 | * |
||
254 | * @throws InvalidArgumentException |
||
255 | * @throws \Exception |
||
256 | * |
||
257 | * @return TaxRecord|mixed |
||
258 | */ |
||
259 | public function findVatById($id) |
||
304 | |||
305 | /** |
||
306 | * @param $name |
||
307 | * @param null $city |
||
308 | * |
||
309 | * @throws InvalidArgumentException |
||
310 | * @throws \Exception |
||
311 | * |
||
312 | * @return array|AresRecord[]|AresRecords |
||
313 | */ |
||
314 | public function findByName($name, $city = null) |
||
366 | |||
367 | /** |
||
368 | * @param string $cacheStrategy |
||
369 | */ |
||
370 | public function setCacheStrategy($cacheStrategy) |
||
374 | |||
375 | /** |
||
376 | * @param bool $debug |
||
377 | */ |
||
378 | public function setDebug($debug) |
||
382 | |||
383 | /** |
||
384 | * @param mixed $id |
||
385 | */ |
||
386 | 3 | private function ensureIdIsInteger($id) |
|
392 | } |
||
393 |
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.