Conditions | 7 |
Paths | 6 |
Total Lines | 54 |
Code Lines | 33 |
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 |
||
29 | public function getResults($query) |
||
30 | { |
||
31 | $searchResults = []; |
||
32 | |||
33 | if ($query == '') { |
||
34 | return $searchResults; |
||
35 | } |
||
36 | |||
37 | if ($this->searchEngineId == '') { |
||
38 | throw new \Exception('You must specify a searchEngineId'); |
||
39 | } |
||
40 | |||
41 | $client = new Client(); |
||
42 | $result = $client->get('http://www.google.com/cse', ['query' => ['cx' => $this->searchEngineId, |
||
43 | 'client' => 'google-csbe', |
||
44 | 'num' => 20, |
||
45 | 'output' => 'xml_no_dtd', |
||
46 | 'q' => $query, |
||
47 | ], |
||
48 | ]); |
||
49 | |||
50 | if ($result->getStatusCode() != 200) { |
||
51 | throw new Exception('Resultcode was not ok: '.$result->getStatusCode()); |
||
52 | } |
||
53 | |||
54 | $xml = simplexml_load_string($result->getBody()); |
||
55 | |||
56 | if ($xml->ERROR) { |
||
57 | throw new Exception('XML indicated service error: '.$xml->ERROR); |
||
58 | } |
||
59 | |||
60 | if ($xml->RES->R) { |
||
61 | $i = 0; |
||
62 | foreach ($xml->RES->R as $item) { |
||
63 | $searchResults[$i]['name'] = (string) $item->T; |
||
64 | $searchResults[$i]['url'] = (string) $item->U; |
||
65 | $searchResults[$i]['snippet'] = (string) $item->S; |
||
66 | $searchResults[$i]['image'] = $this->getPageMapProperty($item, 'cse_image', 'src'); |
||
67 | |||
68 | $searchResults[$i]['product']['name'] = $this->getPageMapProperty($item, 'product', 'name'); |
||
69 | $searchResults[$i]['product']['brand'] = $this->getPageMapProperty($item, 'product', 'brand'); |
||
70 | $searchResults[$i]['product']['price'] = $this->getPageMapProperty($item, 'product', 'price'); |
||
71 | $searchResults[$i]['product']['image'] = $this->getPageMapProperty($item, 'product', 'image'); |
||
72 | $searchResults[$i]['product']['identifier'] = $this->getPageMapProperty($item, 'product', 'identifier'); |
||
73 | |||
74 | $searchResults[$i]['offer']['price'] = $this->getPageMapProperty($item, 'offer', 'price'); |
||
75 | $searchResults[$i]['offer']['pricecurrency'] = $this->getPageMapProperty($item, 'offer', 'pricecurrency'); |
||
76 | |||
77 | ++$i; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $searchResults; |
||
82 | } |
||
83 | |||
104 |