Conditions | 10 |
Paths | 12 |
Total Lines | 47 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
65 | public function search($address, $page = 1) |
||
66 | { |
||
67 | $cache = $this->file . preg_replace('/[^a-zA-Z0-9_]/', '_', $address) . '_' . strtolower($this->config->getLocale()) . '.data'; |
||
68 | |||
69 | $this->response = $this->cacheValue($cache); |
||
70 | |||
71 | if ($this->response == null) { |
||
72 | $response = $this->_get( |
||
73 | 'v4/Address/address-free-text', |
||
74 | [ |
||
75 | 'addressstring' => $address, |
||
76 | 'page' => $page, |
||
77 | ], |
||
78 | false |
||
79 | ); |
||
80 | if ($response['success'] == false) { |
||
81 | return; |
||
82 | } |
||
83 | $addresses = $response['Addresses']; |
||
84 | $pages = (int) ceil($response['totalSearchResults'] / 20); |
||
85 | if ($page == 1 && ($pages > 1)) { |
||
86 | for ($i = 2; $i <= $pages; $i++) { |
||
87 | // 1 call allowed per 5 seconds (on Development subscription) |
||
88 | ($this->config->getApiSubscription() == 'Development') ? sleep(5) : ''; |
||
89 | $pageData = $this->_get( |
||
90 | 'v4/Address/address-free-text', |
||
91 | [ |
||
92 | 'addressstring' => $address, |
||
93 | 'page' => $i, |
||
94 | ], |
||
95 | false |
||
96 | ); |
||
97 | |||
98 | $pageData = $pageData['Addresses']; |
||
99 | $pageData = array_combine(range(($i * 10), count($pageData) + (($i * 10) - 1)), array_values($pageData)); |
||
100 | $addresses += $pageData; |
||
101 | } |
||
102 | } |
||
103 | if($this->config->getCache()){ |
||
104 | (!file_exists($this->cacheDir)) ? |
||
|
|||
105 | mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache)); |
||
106 | file_put_contents($cache, serialize($addresses)); |
||
107 | } |
||
108 | $this->response = $addresses; |
||
109 | } |
||
110 | |||
111 | return $this; |
||
112 | } |
||
195 |