| Conditions | 13 |
| Paths | 48 |
| Total Lines | 54 |
| Code Lines | 31 |
| 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 |
||
| 78 | public function getObjectListByPrefix(string $keyPrefix, string $sortBy = null, int $limit = 0): array |
||
| 79 | { |
||
| 80 | $results = []; |
||
| 81 | $continuationToken = ''; |
||
| 82 | $options = [ |
||
| 83 | 'Bucket' => $this->bucket, |
||
| 84 | 'EncodingType' => 'url', |
||
| 85 | 'Prefix' => $keyPrefix, |
||
| 86 | 'RequestPayer' => 'requester' |
||
| 87 | ]; |
||
| 88 | |||
| 89 | // We can add a query limit here only when we don't want any special sorting. |
||
| 90 | // The default chunk limit is 1000. Probably the guys at the AWS know why not recommended to go over this limit |
||
| 91 | // so I won't do either. |
||
| 92 | if (empty($sortBy) && $limit > 0 && $limit < self::AWS_DEFAULT_LIST_LIMIT) { |
||
| 93 | $options['MaxKeys'] = $limit; |
||
| 94 | // Set the parameter to 0 to avoid the unnecessary array_chunk later. |
||
| 95 | $limit = 0; |
||
| 96 | } |
||
| 97 | |||
| 98 | do { |
||
| 99 | if (!empty($continuationToken)) { |
||
| 100 | $options['ContinuationToken'] = $continuationToken; |
||
| 101 | } |
||
| 102 | |||
| 103 | $response = $this->s3Client->listObjectsV2($options); |
||
| 104 | |||
| 105 | if (empty($response['Contents'])) { |
||
| 106 | break; |
||
| 107 | } |
||
| 108 | |||
| 109 | $results[] = $response['Contents']; |
||
| 110 | $continuationToken = $response['NextContinuationToken']; |
||
| 111 | $isTruncated = $response['IsTruncated']; |
||
| 112 | usleep(50000); // 50 ms pause to avoid CPU spikes |
||
| 113 | } while ($isTruncated); |
||
| 114 | |||
| 115 | $results = array_merge([], ...$results); |
||
| 116 | |||
| 117 | if (!empty($sortBy) && in_array($sortBy, $this->validSortByKeys, true)) { |
||
| 118 | $direction = $sortBy[0] === '^' ? 'asc' : 'desc'; |
||
| 119 | $sortByKey = substr($sortBy, 1); |
||
| 120 | |||
| 121 | usort($results, static function ($a, $b) use ($direction, $sortByKey) { |
||
| 122 | $cmp = strcmp($a[$sortByKey], $b[$sortByKey]); |
||
| 123 | return $direction === 'asc' ? $cmp : -$cmp; |
||
| 124 | }); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!empty($results) && $limit > 0) { |
||
| 128 | $results = array_chunk($results, $limit)[0]; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $results; |
||
| 132 | } |
||
| 134 |