| Conditions | 18 |
| Paths | 1024 |
| Total Lines | 71 |
| 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 |
||
| 56 | public function search($query, array $options = []) |
||
| 57 | { |
||
| 58 | // Max number of records to fetch. Set to 0 to fetch all. |
||
| 59 | $limit = array_key_exists('limit', $options) ? $options['limit'] : 0; |
||
| 60 | |||
| 61 | // Set to true to do a phrase search |
||
| 62 | $phrase = array_key_exists('phrase', $options) ? $options['phrase'] : false; |
||
| 63 | |||
| 64 | // Set to true to expand all query results to full records. |
||
| 65 | // Please note that this will make queries significantly slower! |
||
| 66 | $expand = array_key_exists('expand', $options) ? $options['expand'] : false; |
||
| 67 | |||
| 68 | // Number of records to fetch each batch. Usually no need to change this. |
||
| 69 | $batchSize = array_key_exists('batchSize', $options) ? $options['batchSize'] : 10; |
||
| 70 | |||
| 71 | if ($limit != 0 && $limit < $batchSize) { |
||
| 72 | $batchSize = $limit; |
||
| 73 | } |
||
| 74 | |||
| 75 | // The API will throw a 400 response if you include properly encoded spaces, |
||
| 76 | // but underscores work as a substitute. |
||
| 77 | $query = explode(' AND ', $query); |
||
| 78 | $query = $phrase ? str_replace(' ', '_', $query) : str_replace(' ', ',', $query); |
||
| 79 | $query = implode(' AND ', $query); |
||
| 80 | |||
| 81 | $offset = 0; |
||
| 82 | while (true) { |
||
| 83 | $response = $this->client->getJSON('/users', ['q' => $query, 'limit' => $batchSize, 'offset' => $offset]); |
||
| 84 | |||
| 85 | // The API sometimes returns total_record_count: -1, with no further error message. |
||
| 86 | // Seems to indicate that the query was not understood. |
||
| 87 | // See: https://github.com/scriptotek/php-alma-client/issues/8 |
||
| 88 | if ($response->total_record_count == -1) { |
||
| 89 | throw new InvalidQuery($query); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($response->total_record_count == 0) { |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!isset($response->user)) { |
||
| 97 | // We cannot trust the value in 'total_record_count', so if there are no more records, |
||
| 98 | // we have to assume the result set is depleted. |
||
| 99 | // See: https://github.com/scriptotek/php-alma-client/issues/7 |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | |||
| 103 | foreach ($response->user as $data) { |
||
| 104 | // Contacts without a primary identifier will have the primary_id |
||
| 105 | // field populated with something weird like "no primary id (123456789023)". |
||
| 106 | // We ignore those. |
||
| 107 | // See: https://github.com/scriptotek/php-alma-client/issues/6 |
||
| 108 | if (strpos($data->primary_id, 'no primary id') === 0) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | $user = User::make($this->client, $data->primary_id) |
||
| 112 | ->init($data); |
||
| 113 | if ($expand) { |
||
| 114 | $user->init(); |
||
| 115 | } |
||
| 116 | yield $user; |
||
| 117 | $offset++; |
||
| 118 | } |
||
| 119 | if ($offset >= $response->total_record_count) { |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | if ($limit != 0 && $offset >= $limit) { |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 |