Passed
Pull Request — master (#41)
by kenny
02:45
created

stream_for()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 11
nop 2
dl 0
loc 39
rs 7.3166
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
/**
6
 * createUriFromString
7
 *
8
 * @param string $uri
9
 * @return \Psr\Http\Message\UriInterface
10
 */
11
function createUriFromString($uri)
12
{
13
    $parts = parse_url($uri);
14
    $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
15
    $user = isset($parts['user']) ? $parts['user'] : '';
16
    $pass = isset($parts['pass']) ? $parts['pass'] : '';
17
    $host = isset($parts['host']) ? $parts['host'] : '';
18
    $port = isset($parts['port']) ? $parts['port'] : null;
19
    $path = isset($parts['path']) ? $parts['path'] : '';
20
    $query = isset($parts['query']) ? $parts['query'] : '';
21
    $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
22
    return new Uri(
23
        $scheme,
24
        $host,
25
        $port,
26
        $path,
27
        $query,
28
        $fragment,
29
        $user,
30
        $pass
31
    );
32
}
33
34
/**
35
 * createuriFromServer
36
 *
37
 * @return \Psr\Http\Message\UriInterface
38
 */
39
40
function createuriFromServer()
41
{
42
    $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
43
    $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
44
    $port = empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
45
    $path = (string) parse_url('http://www.example.com/' . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
46
    $query = empty($_SERVER['QUERY_STRING']) ? parse_url('http://example.com' . $_SERVER['REQUEST_URI'], PHP_URL_QUERY) : $_SERVER['QUERY_STRING'];
47
    $fragment = '';
48
    $user = !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
49
    $password = !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
50
    if (empty($user) && empty($password) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
51
        list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
52
    }
53
54
    return new Uri(
55
        $scheme,
56
        $host,
57
        $port,
58
        $path,
59
        $query,
60
        $fragment,
61
        $user,
62
        $password
63
    );
64
}
65
66
function stream_for($resource = '', array $options = [])
67
{
68
    if (is_scalar($resource)) {
69
        $stream = fopen('php://temp', 'r+');
70
        if ($resource !== '') {
71
            fwrite($stream, $resource);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fwrite() 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

71
            fwrite(/** @scrutinizer ignore-type */ $stream, $resource);
Loading history...
72
            fseek($stream, 0);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fseek() 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

72
            fseek(/** @scrutinizer ignore-type */ $stream, 0);
Loading history...
73
        }
74
        return new \One\Http\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

74
        return new \One\Http\Stream(/** @scrutinizer ignore-type */ $stream, $options);
Loading history...
75
    }
76
77
    switch (gettype($resource)) {
78
        case 'resource':
79
            return new \One\Http\Stream($resource, $options);
80
        case 'object':
81
            if ($resource instanceof StreamInterface) {
0 ignored issues
show
Bug introduced by
The type One\StreamInterface 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...
82
                return $resource;
83
            } elseif ($resource instanceof \Iterator) {
84
                return new \One\Http\PumpStream(function () use ($resource) {
85
                    if (!$resource->valid()) {
86
                        return false;
87
                    }
88
                    $result = $resource->current();
89
                    $resource->next();
90
                    return $result;
91
                }, $options);
92
            } elseif (method_exists($resource, '__toString')) {
93
                return stream_for((string) $resource, $options);
94
            }
95
            break;
96
        case 'NULL':
97
            return new \One\Http\Stream(fopen('php://temp', 'r+'), $options);
98
    }
99
100
    if (is_callable($resource)) {
101
        return new \One\Http\PumpStream($resource, $options);
102
    }
103
104
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
105
}
106