Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

WanduResponsifier::responsify()   C

Complexity

Conditions 15
Paths 41

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 27
nc 41
nop 1
dl 0
loc 43
ccs 33
cts 33
cp 1
crap 15
rs 5.0504
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
namespace Wandu\Router\Responsifier;
3
4
use Generator;
5
use Psr\Http\Message\ResponseInterface;
6
use RuntimeException;
7
use Wandu\Router\Contracts\ResponsifierInterface;
8
use function Wandu\Http\response;
9
10
class WanduResponsifier implements ResponsifierInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 36
    public function responsify($response)
16
    {
17 36
        if ($response instanceof ResponseInterface) {
18 5
            return $response;
19
        }
20 35
        if (!isset($response)) {
21 1
            $response = '';
22 1
        }
23 35
        while (is_callable($response)) {
24 3
            $nextResponse = call_user_func($response);
25 3
            $response = $nextResponse;
26 3
        }
27
        // int, float, boolean, string
28 35
        if (is_scalar($response)) {
29 29
            if ($response === true) {
30 1
                $response = 'true';
31 29
            } elseif ($response === false) {
32 1
                $response = 'false';
33 1
            }
34 29
            return response()->create((string)$response);
35
        }
36 6
        if ($response instanceof Generator) {
37 2
            return response()->generator($response);
38
        }
39 4
        if (is_array($response) || is_object($response)) {
40 2
            return response()->json($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type object; however, Wandu\Http\Factory\ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41
        }
42 2
        if (is_resource($response)) {
43 2
            if ('stream' === get_resource_type($response)) {
44 2
                $mode = stream_get_meta_data($response)['mode'];
45
                // @todo use Stream
46 2
                if (strpos($mode, 'r') !== false || strpos($mode, '+') !== false) {
47 1
                    $contents = '';
48 1
                    while (!feof($response)) {
49 1
                        $contents .= fread($response, 1024);
50 1
                    }
51 1
                    return response()->create($contents);
52
                }
53 1
            }
54 1
        }
55
56 1
        throw new RuntimeException('Unsupported Type of Response.');
57
    }
58
}
59