| Conditions | 21 |
| Paths | 150 |
| Total Lines | 85 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 20 | ||
| 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 |
||
| 8 | public function __construct(){ |
||
| 9 | |||
| 10 | $config = (new Config)->get(); |
||
| 11 | |||
| 12 | if(!isset($_SERVER['HTTP_ACCEPT'])) $_SERVER['HTTP_ACCEPT'] = '*/*'; |
||
| 13 | $acceptSrc = explode(',',$_SERVER['HTTP_ACCEPT']); |
||
| 14 | foreach($acceptSrc as $key=>$value){ |
||
| 15 | $value = explode('/',$value); |
||
| 16 | $value[1] = explode(';',$value[1]); |
||
| 17 | $name = array_shift($value[1]); |
||
| 18 | $data = []; |
||
| 19 | foreach($value[1] as $k=>$v){ |
||
| 20 | $v = explode('=',$v); |
||
| 21 | $data[$v[0]] = $v[1]; |
||
| 22 | } |
||
| 23 | $accept[$value[0]][$name] = $data; |
||
| 24 | } |
||
| 25 | |||
| 26 | unset($acceptSrc,$key,$value,$name,$data,$k,$v); |
||
| 27 | |||
| 28 | $requestUri = parse_url($_SERVER['REQUEST_URI']); |
||
| 29 | if($requestUri['path'] == '/javascript.js'){ |
||
| 30 | header('Content-Type: application/javascript'); |
||
| 31 | |||
| 32 | $tsstring = 'Sun, 03 Jan 2016 11:22:20 GMT'; |
||
| 33 | $etag = md5($tsstring); |
||
| 34 | $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; |
||
| 35 | $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false; |
||
| 36 | if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) && |
||
| 37 | ($if_modified_since && $if_modified_since == $tsstring)) |
||
| 38 | { |
||
| 39 | header('HTTP/1.1 304 Not Modified'); |
||
| 40 | exit(); |
||
| 41 | } |
||
| 42 | else |
||
| 43 | { |
||
| 44 | header("Last-Modified: $tsstring"); |
||
| 45 | header("ETag: $etag"); |
||
| 46 | } |
||
| 47 | |||
| 48 | if(isset($requestUri['query'])){ |
||
| 49 | $query = explode(':',$requestUri['query']); |
||
| 50 | (new Content)->js($query); |
||
| 51 | } |
||
| 52 | exit; |
||
| 53 | } |
||
| 54 | |||
| 55 | header('Access-Control-Allow-Origin: *'); |
||
| 56 | header('Access-Control-Max-Age: 31556926'); |
||
| 57 | header('Access-Control-Allow-Credentials: true'); |
||
| 58 | header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT'); |
||
| 59 | |||
| 60 | if(isset($accept['application']['view'])){ |
||
| 61 | header('Content-Type: text/html'); |
||
| 62 | (new RestApi)->view(); |
||
| 63 | } else if(isset($accept['application']['widget'])){ |
||
| 64 | header('Content-Type: text/html'); |
||
| 65 | (new RestApi)->widget(); |
||
| 66 | } else if(isset($accept['application']['apps'])){ |
||
| 67 | header('Content-Type: application/json'); |
||
| 68 | $query = json_decode(file_get_contents('php://input'),1); |
||
| 69 | $result = []; |
||
| 70 | foreach($query as $method=>$params){ |
||
| 71 | $stableParams = []; |
||
| 72 | $file = $config['core']['apps'].'/'.$method.'.php'; |
||
| 73 | $func = require_once($file); |
||
| 74 | $reflection = new \ReflectionFunction($func); |
||
| 75 | foreach($reflection->getParameters() as $key=>$value){ |
||
| 76 | if(isset($params[$value->name])) $stableParams[$value->name] = $params[$value->name]; |
||
| 77 | else $stableParams[$value->name] = null; |
||
| 78 | } |
||
| 79 | $name = explode('/',$method); |
||
| 80 | $count = count($name)-1; |
||
| 81 | $app = new Application; |
||
| 82 | foreach($name as $key=>$var){ |
||
| 83 | if($count == $key) $app = call_user_method_array($var,$app,$stableParams); |
||
| 84 | else $app = $app->{$var}; |
||
| 85 | } |
||
| 86 | $result[$method] = $app; |
||
| 87 | } |
||
| 88 | die(json_encode($result)); |
||
| 89 | } else { |
||
| 90 | (new Content)->page(); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 96 |
This check marks private properties in classes that are never used. Those properties can be removed.