Issues (24)

src/Chips/Dispatcher/Responsive.php (3 issues)

1
<?php
2
/**
3
 * Response parser
4
 * User: moyo
5
 * Date: 2018/5/30
6
 * Time: 9:58 AM
7
 */
8
9
namespace Carno\Web\Chips\Dispatcher;
10
11
use Carno\HTTP\Standard\Response;
12
use Carno\HTTP\Standard\ServerRequest;
13
use Carno\HTTP\Standard\Streams\Body;
14
use Carno\Web\Exception\RespondingCodecException;
15
use Google\Protobuf\Internal\Message;
0 ignored issues
show
The type Google\Protobuf\Internal\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use ArrayObject;
17
18
trait Responsive
19
{
20
    /**
21
     * @param ServerRequest $request
22
     * @param Response $response
23
     * @param mixed $result
24
     * @return Response
25
     */
26
    protected function responding(ServerRequest $request, Response $response, $result) : Response
27
    {
28
        switch (gettype($result)) {
29
            case 'array':
30
                if (($cc = $this->codec($request)) === 'json') {
0 ignored issues
show
The assignment to $cc is dead and can be removed.
Loading history...
31
                    return $this->ccJson(
32
                        $response,
33
                        json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
34
                    );
35
                }
36
                throw new RespondingCodecException('Unknown responding codec');
37
            case 'string':
38
                return $response->withBody(new Body($result));
39
            case 'object':
40
                if ($result instanceof Response) {
41
                    return $result;
42
                } elseif ($result instanceof Message) {
43
                    return $this->ccJson($response, $result->serializeToJsonString());
44
                } elseif ($result instanceof ArrayObject) {
45
                    return $this->ccJson(
46
                        $response,
47
                        json_encode((array)$result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
48
                    );
49
                } elseif (method_exists($result, '__toString')) {
50
                    return $response->withBody(new Body((string)$result));
51
                }
52
                throw new RespondingCodecException('Unacceptable object');
53
            case 'NULL':
54
                return $response;
55
            default:
56
                return $response->withBody(new Body((string)$result));
57
        }
58
    }
59
60
    /**
61
     * @param ServerRequest $request
62
     * @return string
63
     */
64
    private function codec(ServerRequest $request) : string
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    private function codec(/** @scrutinizer ignore-unused */ ServerRequest $request) : string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        // TODO currently forced to "json"
67
        return 'json';
68
    }
69
70
    /**
71
     * @param Response $response
72
     * @param string $data
73
     * @return Response
74
     */
75
    private function ccJson(Response $response, string $data) : Response
76
    {
77
        $response = $response->withBody(new Body($data));
78
        $response->withHeader('Content-Type', 'application/json');
79
        return $response;
80
    }
81
}
82