Passed
Pull Request — psr-implementation (#45)
by kenny
03:45
created

stream_for()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 28
nc 11
nop 2
dl 0
loc 38
rs 6.6166
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
3
namespace One;
4
5
use One\Http\Stream;
6
use One\Http\PumpStream;
7
use Psr\Http\Message\StreamInterface;
8
9
/**
10
 * createUriFromString
11
 *
12
 * @param string $uri
13
 * @return \Psr\Http\Message\UriInterface
14
 */
15
function createUriFromString($uri)
16
{
17
    $parts = parse_url($uri);
18
    $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
19
    $user = isset($parts['user']) ? $parts['user'] : '';
20
    $pass = isset($parts['pass']) ? $parts['pass'] : '';
21
    $host = isset($parts['host']) ? $parts['host'] : '';
22
    $port = isset($parts['port']) ? $parts['port'] : null;
23
    $path = isset($parts['path']) ? $parts['path'] : '';
24
    $query = isset($parts['query']) ? $parts['query'] : '';
25
    $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
26
    return new Uri(
27
        $scheme,
28
        $host,
29
        $port,
30
        $path,
31
        $query,
32
        $fragment,
33
        $user,
34
        $pass
35
    );
36
}
37
38
/**
39
 * createuriFromServer
40
 *
41
 * @return \Psr\Http\Message\UriInterface
42
 */
43
44
function createuriFromServer()
45
{
46
    $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
47
    $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
48
    $port = empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
49
    $path = (string) parse_url('http://www.example.com/' . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
50
    $query = empty($_SERVER['QUERY_STRING']) ? parse_url('http://example.com' . $_SERVER['REQUEST_URI'], PHP_URL_QUERY) : $_SERVER['QUERY_STRING'];
51
    $fragment = '';
52
    $user = !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
53
    $password = !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
54
    if (empty($user) && empty($password) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
55
        list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
56
    }
57
58
    return new Uri(
59
        $scheme,
60
        $host,
61
        $port,
62
        $path,
63
        $query,
64
        $fragment,
65
        $user,
66
        $password
67
    );
68
}
69
70
/**
71
 * Create a new stream based on the input type.
72
 *
73
 * Options is an associative array that can contain the following keys:
74
 * - metadata: Array of custom metadata.
75
 * - size: Size of the stream.
76
 *
77
 * @param resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data
78
 * @param array                                                                  $options  Additional options
79
 *
80
 * @return StreamInterface
81
 * @throws \InvalidArgumentException if the $resource arg is not valid.
82
 */
83
function stream_for($resource = '', array $options = [])
84
{
85
    if (is_scalar($resource)) {
86
        $stream = fopen('php://temp', 'r+');
87
        if ($resource !== '' && $stream !== false) {
88
            fwrite($stream, $resource);
89
            fseek($stream, 0);
90
        }
91
        return new Stream($stream, $options);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $stream of One\Http\Stream::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

91
        return new Stream(/** @scrutinizer ignore-type */ $stream, $options);
Loading history...
92
    }
93
    switch (gettype($resource)) {
94
        case 'resource':
95
            return new Stream($resource, $options);
96
        case 'object':
97
            if ($resource instanceof StreamInterface) {
98
                return $resource;
99
            } elseif ($resource instanceof \Iterator) {
0 ignored issues
show
introduced by
$resource is always a sub-type of Iterator.
Loading history...
100
                return new PumpStream(function () use ($resource) {
101
                    if (!$resource->valid()) {
102
                        return false;
103
                    }
104
                    $result = $resource->current();
105
                    $resource->next();
106
                    return $result;
107
                }, $options);
108
            } elseif (method_exists($resource, '__toString')) {
109
                return stream_for((string) $resource, $options);
110
            }
111
            break;
112
        case 'NULL':
113
            return new Stream(fopen('php://temp', 'r+'), $options);
114
    }
115
116
    if (is_callable($resource) && !is_null($resource)) {
117
        return new \One\Http\PumpStream($resource, $options);
118
    }
119
120
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
121
}
122