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

stream_for()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 27
nc 8
nop 2
dl 0
loc 36
rs 6.9666
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);
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 (method_exists($resource, '__toString')) {
100
                return stream_for((string) $resource, $options);
101
            }return new PumpStream(function () use ($resource) {
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
102
                if (!$resource->valid()) {
103
                    return false;
104
                }
105
                $result = $resource->current();
106
                $resource->next();
107
                return $result;
108
            }, $options);
109
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110
        case 'NULL':
111
            return new Stream(fopen('php://temp', 'r+'), $options);
112
    }
113
114
    if (is_callable($resource) && !is_null($resource)) {
115
        return new \One\Http\PumpStream($resource, $options);
116
    }
117
118
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
119
}
120