Conditions | 16 |
Paths | 896 |
Total Lines | 56 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
18 | public function search($query, array $options = []) |
||
19 | { |
||
20 | // Max number of records to fetch. Set to 0 to fetch all. |
||
21 | $limit = array_key_exists('limit', $options) ? $options['limit'] : 0; |
||
22 | |||
23 | // Set to true to do a phrase search |
||
24 | $phrase = array_key_exists('phrase', $options) ? $options['phrase'] : false; |
||
25 | |||
26 | // Set to true to expand all query results to full records. |
||
27 | // Please note that this will make queries significantly slower! |
||
28 | $expand = array_key_exists('expand', $options) ? $options['expand'] : false; |
||
29 | |||
30 | // Number of records to fetch each batch. Usually no need to change this. |
||
31 | $batchSize = array_key_exists('batchSize', $options) ? $options['batchSize'] : 10; |
||
32 | |||
33 | if ($limit != 0 && $limit < $batchSize) $batchSize = $limit; |
||
34 | |||
35 | // The API will throw a 400 response if you include properly encoded spaces, |
||
36 | // but underscores work as a substitute. |
||
37 | $query = explode(' AND ', $query); |
||
38 | $query = $phrase ? str_replace(' ', '_', $query) : str_replace(' ', ',', $query); |
||
39 | $query = implode(' AND ', $query); |
||
40 | |||
41 | $offset = 0; |
||
42 | while (true) { |
||
43 | $response = $this->client->getJSON('/users', ['q' => $query, 'limit' => $batchSize, 'offset' => $offset]); |
||
44 | |||
45 | if ($response->total_record_count == 0) { |
||
46 | break; |
||
47 | } |
||
48 | |||
49 | foreach ($response->user as $data) { |
||
50 | |||
51 | // Contacts without a primary identifier will have the primary_id |
||
52 | // field populated with something weird like "no primary id (123456789023)". |
||
53 | // We ignore those. |
||
54 | // See: https://github.com/scriptotek/php-alma-client/issues/6 |
||
55 | if (strpos($data->primary_id, 'no primary id') === 0) { |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | $user = User::fromResponse($this->client, $data); |
||
60 | if ($expand) { |
||
61 | $user->fetch(); |
||
62 | } |
||
63 | yield $user; |
||
64 | $offset++; |
||
65 | } |
||
66 | if ($offset >= $response->total_record_count) { |
||
67 | break; |
||
68 | } |
||
69 | if ($limit != 0 && $offset >= $limit) { |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 |