Completed
Push — master ( 74fc93...195e55 )
by Sandro
06:11
created

TestStream   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 0
dl 0
loc 93
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_open() 0 8 1
A stream_read() 0 7 2
A stream_write() 0 8 1
A stream_tell() 0 4 1
A stream_eof() 0 4 1
C stream_seek() 0 34 8
A stream_metadata() 0 12 3
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
6
 * @copyright Copyright (c) 2017-2017 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace InteropTest\Config\TestAsset;
11
12
/**
13
 * Helper class to catch outputs and inputs to emulate CLI in unit tests
14
 *
15
 * @see http://no2.php.net/manual/en/stream.streamwrapper.example-1.php
16
 */
17
class TestStream
18
{
19
    private $position;
20
    private $varname;
21
22
    public static $data = [];
23
    public static $inputStack = [];
24
25
    public function stream_open($path, $mode, $options, &$opened_path)
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $opened_path is not used and could be removed.

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

Loading history...
Coding Style introduced by
Method name "TestStream::stream_open" is not in camel caps format
Loading history...
26
    {
27
        $this->varname = parse_url($path)['host'];
28
        $this->position = 0;
29
        self::$data[$this->varname] = '';
30
31
        return true;
32
    }
33
34
    public function stream_read($count)
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed.

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

Loading history...
Coding Style introduced by
Method name "TestStream::stream_read" is not in camel caps format
Loading history...
35
    {
36
        if (!empty(self::$inputStack)) {
37
            return array_shift(self::$inputStack);
38
        }
39
        return '';
40
    }
41
42
    public function stream_write($data)
0 ignored issues
show
Coding Style introduced by
Method name "TestStream::stream_write" is not in camel caps format
Loading history...
43
    {
44
        $left = substr(self::$data[$this->varname], 0, $this->position);
45
        $right = substr(self::$data[$this->varname], $this->position + strlen($data));
46
        self::$data[$this->varname] = $left . $data . $right;
47
        $this->position += strlen($data);
48
        return strlen($data);
49
    }
50
51
    public function stream_tell()
0 ignored issues
show
Coding Style introduced by
Method name "TestStream::stream_tell" is not in camel caps format
Loading history...
52
    {
53
        return $this->position;
54
    }
55
56
    public function stream_eof()
0 ignored issues
show
Coding Style introduced by
Method name "TestStream::stream_eof" is not in camel caps format
Loading history...
57
    {
58
        return $this->position >= strlen(self::$data[$this->varname]);
59
    }
60
61
    public function stream_seek($offset, $whence)
0 ignored issues
show
Coding Style introduced by
Method name "TestStream::stream_seek" is not in camel caps format
Loading history...
62
    {
63
        switch ($whence) {
64
            case SEEK_SET:
65
                if ($offset < strlen(self::$data[$this->varname]) && $offset >= 0) {
66
                    $this->position = $offset;
67
                    return true;
68
                } else {
69
                    return false;
70
                }
71
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
72
73
            case SEEK_CUR:
74
                if ($offset >= 0) {
75
                    $this->position += $offset;
76
                    return true;
77
                } else {
78
                    return false;
79
                }
80
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
82
            case SEEK_END:
83
                if (strlen(self::$data[$this->varname]) + $offset >= 0) {
84
                    $this->position = strlen(self::$data[$this->varname]) + $offset;
85
                    return true;
86
                } else {
87
                    return false;
88
                }
89
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
90
91
            default:
92
                return false;
93
        }
94
    }
95
96
    public function stream_metadata($path, $option, $var)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

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

Loading history...
Coding Style introduced by
Method name "TestStream::stream_metadata" is not in camel caps format
Loading history...
97
    {
98
        if ($option == STREAM_META_TOUCH) {
99
            $url = parse_url($path);
100
            $varname = $url['host'];
101
            if (!isset(self::$data[$varname])) {
102
                self::$data[$varname] = '';
103
            }
104
            return true;
105
        }
106
        return false;
107
    }
108
109
}
110