| Conditions | 15 |
| Paths | 96 |
| Total Lines | 50 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| 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 |
||
| 25 | public static function json(array $data, array $params = []) |
||
| 26 | { |
||
| 27 | $f3 = \Base::instance(); |
||
| 28 | |||
| 29 | $body = json_encode($data, JSON_PRETTY_PRINT); |
||
| 30 | $headers = array_key_exists('headers', $params) ? $params['headers'] : []; |
||
| 31 | |||
| 32 | // set ttl |
||
| 33 | $ttl = (int) array_key_exists('ttl', $params) ? $params['ttl'] : 0; // cache for $ttl seconds |
||
| 34 | |||
| 35 | $headers = array_merge($headers, [ |
||
| 36 | 'Content-type' => 'application/json; charset=utf-8', |
||
| 37 | 'Expires' => Time::http(time() + $ttl), |
||
| 38 | 'Access-Control-Max-Age' => $ttl, |
||
| 39 | 'Access-Control-Expose-Headers' => array_key_exists('acl_expose_headers', $params) ? $params['acl_expose_headers'] : null, |
||
| 40 | 'Access-Control-Allow-Methods' => array_key_exists('acl_http_methods', $params) ? $params['acl_http_methods'] : null, |
||
| 41 | 'Access-Control-Allow-Origin' => array_key_exists('acl_origin', $params) ? $params['acl_origin'] : '*', |
||
| 42 | 'Access-Control-Allow-Credentials' => array_key_exists('acl_credentials', $params) && !empty($params['acl_credentials']) ? 'true' : 'false', |
||
| 43 | 'ETag' => array_key_exists('etag', $params) ? $params['etag'] : md5($body), |
||
| 44 | 'Content-Length' => \UTF::instance()->strlen($body), |
||
| 45 | ]); |
||
| 46 | |||
| 47 | // send the headers + data |
||
| 48 | |||
| 49 | if (empty($ttl)) { |
||
| 50 | $f3->expire(0); |
||
| 51 | $ttl = 0; |
||
| 52 | } |
||
| 53 | |||
| 54 | // default status is 200 - OK |
||
| 55 | $f3->status(array_key_exists('http_status', $params) ? $params['http_status'] : 200); |
||
| 56 | |||
| 57 | // do not send session cookie |
||
| 58 | if (!array_key_exists('cookie', $params)) { |
||
| 59 | header_remove('Set-Cookie'); // prevent php session |
||
| 60 | } |
||
| 61 | |||
| 62 | ksort($headers); |
||
| 63 | foreach ($headers as $header => $value) { |
||
| 64 | if (!isset($value)) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | header($header . ': ' . $value); |
||
| 68 | } |
||
| 69 | |||
| 70 | // HEAD request should be identical headers to GET request but no body |
||
| 71 | if ('HEAD' !== $f3->get('VERB')) { |
||
| 72 | echo $body; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.