Conditions | 7 |
Paths | 6 |
Total Lines | 74 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
84 | public function getResults($phrase, $parameters = array()) |
||
85 | { |
||
86 | |||
87 | /** |
||
88 | * Check required parameters |
||
89 | */ |
||
90 | if ($phrase == '') { |
||
91 | return array(); |
||
92 | } |
||
93 | |||
94 | if ($this->engineId == '') { |
||
95 | throw new \Exception('You must specify a engineId'); |
||
96 | } |
||
97 | |||
98 | if ($this->apiKey == '') { |
||
99 | throw new \Exception('You must specify a apiKey'); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Create search aray |
||
104 | */ |
||
105 | $searchArray = http_build_query(array_merge( |
||
106 | ['key' => $this->apiKey], |
||
107 | ['q' => $phrase], |
||
108 | $parameters |
||
109 | )); |
||
110 | |||
111 | /** |
||
112 | * Add unencoded search engine id |
||
113 | */ |
||
114 | $searchArray = '?cx=' . $this->engineId . '&' . $searchArray; |
||
115 | |||
116 | /** |
||
117 | * Prepare CUrl and get result |
||
118 | */ |
||
119 | $ch = curl_init(); |
||
120 | |||
121 | curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/customsearch/v1" . $searchArray); |
||
122 | curl_setopt($ch, CURLOPT_HEADER, 0); |
||
123 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
124 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
||
125 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
||
126 | curl_setopt($ch, CURLOPT_POST, 0); |
||
127 | |||
128 | $output = curl_exec($ch); |
||
129 | |||
130 | $info = curl_getinfo($ch); |
||
131 | |||
132 | curl_close($ch); |
||
133 | |||
134 | /** |
||
135 | * Check HTTP code of the result |
||
136 | */ |
||
137 | if ($output === false || $info['http_code'] != 200) { |
||
138 | |||
139 | throw new \Exception("No data returned, code [". $info['http_code']. "] - " . curl_error($ch)); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Convert JSON format to object and save |
||
144 | */ |
||
145 | $this->originalResponse = json_decode($output); |
||
146 | |||
147 | /** |
||
148 | * If there are some results, return them, otherwise return blank array |
||
149 | */ |
||
150 | if(isset($this->originalResponse->items)){ |
||
151 | return $this->originalResponse->items; |
||
152 | } |
||
153 | else{ |
||
154 | return array(); |
||
155 | } |
||
156 | |||
157 | } |
||
158 | |||
199 |