BurningFlipside /
CommonCode
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Http\Rest; |
||
| 3 | |||
| 4 | use \Psr\Http\Message\ServerRequestInterface as Request; |
||
| 5 | use \Psr\Http\Message\ResponseInterface as Response; |
||
| 6 | |||
| 7 | require 'vendor/autoload.php'; |
||
| 8 | |||
| 9 | class SerializationMiddleware |
||
| 10 | { |
||
| 11 | protected $format = null; |
||
| 12 | |||
| 13 | private function getFormatFromHeader($request) |
||
| 14 | { |
||
| 15 | $mimeType = $request->getHeader('Accept'); |
||
| 16 | switch($mimeType) |
||
| 17 | { |
||
| 18 | case 'text/csv': |
||
| 19 | return 'csv'; |
||
| 20 | case 'text/x-vCard': |
||
| 21 | return 'vcard'; |
||
| 22 | default: |
||
| 23 | return 'json'; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | private function getParamFromArrayIfSet($array, $param, $default = null) |
||
| 28 | { |
||
| 29 | if(isset($array[$param])) |
||
| 30 | { |
||
| 31 | return $array[$param]; |
||
| 32 | } |
||
| 33 | return $default; |
||
| 34 | } |
||
| 35 | |||
| 36 | private function getFormat($request, $response) |
||
| 37 | { |
||
| 38 | $params = $request->getQueryParams(); |
||
| 39 | $this->format = $this->getParamFromArrayIfSet($params, 'fmt'); |
||
| 40 | if($this->format === null) |
||
| 41 | { |
||
| 42 | $this->format = $this->getParamFromArrayIfSet($params, '$format'); |
||
| 43 | if($this->format === null) |
||
| 44 | { |
||
| 45 | $this->format = $this->getFormatFromHeader($request); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | if(strstr($this->format, 'odata.streaming=true')) |
||
| 49 | { |
||
| 50 | return $response->withStatus(406); |
||
| 51 | } |
||
| 52 | return $response; |
||
| 53 | } |
||
| 54 | |||
| 55 | protected function reserializeBody($response, $serializer) |
||
| 56 | { |
||
| 57 | $body = $response->getBody(); |
||
| 58 | $body->rewind(); |
||
| 59 | $data = json_decode($body->getContents()); |
||
| 60 | $serializer = new $serializer(); |
||
| 61 | $res = $serializer->serializeData($this->format, $data); |
||
| 62 | $response = $response->withBody(new \Slim\Http\Body(fopen('php://temp', 'r+'))); |
||
| 63 | $response->getBody()->write($res); |
||
| 64 | return $response->withHeader('Content-Type', $this->format); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function __invoke($request, $response, $next) |
||
| 68 | { |
||
| 69 | $response = $this->getFormat($request, $response); |
||
| 70 | if($response->getStatusCode() !== 200) |
||
| 71 | { |
||
| 72 | return $response; |
||
| 73 | } |
||
| 74 | $response = $next($request, $response); |
||
| 75 | switch($this->format) |
||
| 76 | { |
||
| 77 | case 'application/json': |
||
| 78 | case 'application/x-javascript': |
||
| 79 | case 'text/javascript': |
||
| 80 | case 'text/x-javascript': |
||
| 81 | case 'text/x-json': |
||
| 82 | case 'json': |
||
| 83 | return $response; |
||
| 84 | case 'xml': |
||
| 85 | case 'application/xml': |
||
| 86 | case 'text/xml': |
||
| 87 | return $this->reserializeBody($response, '\Serialize\XMLSerializer'); |
||
| 88 | case 'csv': |
||
| 89 | case 'text/csv': |
||
| 90 | return $this->reserializeBody($response, '\Serialize\CSVSerializer'); |
||
| 91 | case 'xlsx': |
||
| 92 | case 'xls': |
||
| 93 | case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': |
||
| 94 | case 'application/vnd.ms-excel': |
||
| 95 | return $this->reserializeBody($response, '\Serialize\ExcelSerializer'); |
||
| 96 | default: |
||
| 97 | print_r($this->format); die(); |
||
| 98 | break; |
||
|
0 ignored issues
–
show
|
|||
| 99 | } |
||
| 100 | return $response; |
||
| 101 | } |
||
| 102 | } |
||
| 103 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.