| Conditions | 13 |
| Paths | 132 |
| Total Lines | 62 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 93 | public function exec (string $method, string $path, array $curlOpts = []) { |
||
| 94 | $this->getLogger()->log(LOG_DEBUG, "{$method} {$path}", $curlOpts); |
||
| 95 | $ch = curl_init(); |
||
| 96 | curl_setopt_array($ch, [ |
||
| 97 | CURLOPT_CUSTOMREQUEST => $method, |
||
| 98 | CURLOPT_URL => "https://{$this->key}:{$this->password}@{$this->domain}/admin/api/2020-04/{$path}", |
||
| 99 | CURLOPT_FOLLOWLOCATION => true, |
||
| 100 | CURLOPT_HEADER => true, |
||
| 101 | CURLOPT_RETURNTRANSFER => true, |
||
| 102 | CURLOPT_USERAGENT => 'hfw/shopify' |
||
| 103 | ]); |
||
| 104 | $curlOpts[CURLOPT_HTTPHEADER][] = 'Accept: application/json'; |
||
| 105 | $curlOpts[CURLOPT_HTTPHEADER][] = 'Expect:'; // prevent http 100 |
||
| 106 | curl_setopt_array($ch, $curlOpts); |
||
| 107 | RETRY: |
||
| 108 | $res = explode("\r\n\r\n", curl_exec($ch), 2); |
||
| 109 | $info = curl_getinfo($ch); |
||
| 110 | switch ($info['http_code']) { |
||
| 111 | case 0: |
||
| 112 | throw new ShopifyError(curl_errno($ch), curl_error($ch), $info); |
||
| 113 | case 200: |
||
| 114 | case 201: |
||
| 115 | case 202: |
||
| 116 | return json_decode($res[1], true, 512, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR); |
||
| 117 | case 404: |
||
| 118 | return null; |
||
| 119 | case 429: |
||
| 120 | preg_match('/^Retry-After:\h*(\d+)/im', $res[0], $retry); |
||
| 121 | $this->getLogger()->log(LOG_DEBUG, $retry[0]); |
||
| 122 | sleep($retry[1]); |
||
| 123 | goto RETRY; |
||
| 124 | default: |
||
| 125 | $error = new ShopifyError($info['http_code'], $res[1], $info); |
||
| 126 | $this->getLogger()->log(LOG_ERR, "Shopify {$info['http_code']}: {$error->getMessage()}"); |
||
| 127 | throw $error; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param Api|Data $caller |
||
| 133 | * @param string $class |
||
| 134 | * @param array $data |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function factory ($caller, string $class, array $data = []) { |
||
| 138 | return new $class($caller, $data); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param Api|Data $caller |
||
| 143 | * @param string $class |
||
| 144 | * @param array[] $list |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function factoryAll ($caller, string $class, array $list) { |
||
| 148 | return array_map(function(array $each) use ($caller, $class) { |
||
| 149 | return $this->factory($caller, $class, $each); |
||
| 150 | }, $list); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $path |
||
| 155 | * @param array $query |
||
| 266 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.