Passed
Pull Request — master (#41)
by kenny
06:25
created

FnStream   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A isReadable() 0 3 1
A read() 0 3 1
A isWritable() 0 3 1
A __construct() 0 6 2
A getContents() 0 3 1
A __get() 0 4 1
A __destruct() 0 4 2
A rewind() 0 3 1
A __wakeup() 0 3 1
A seek() 0 3 1
A getSize() 0 3 1
A decorate() 0 8 2
A isSeekable() 0 3 1
A eof() 0 3 1
A __toString() 0 3 1
A getMetadata() 0 3 1
A write() 0 3 1
A detach() 0 3 1
A tell() 0 3 1
A close() 0 3 1
1
<?php
2
3
namespace One\Http;
4
5
class FnStream implements \Psr\Http\Message\StreamInterface
6
{
7
    /** @var array */
8
    private $methods;
9
    /** @var array Methods that must be implemented in the given array */
10
    private static $slots = ['__toString', 'close', 'detach', 'rewind',
11
        'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write',
12
        'isReadable', 'read', 'getContents', 'getMetadata'];
13
    /**
14
     * @param array $methods Hash of method name to a callable.
15
     */
16
    public function __construct(array $methods)
17
    {
18
        $this->methods = $methods;
19
        // Create the functions on the class
20
        foreach ($methods as $name => $fn) {
21
            $this->{'_fn_' . $name} = $fn;
22
        }
23
    }
24
    /**
25
     * Lazily determine which methods are not implemented.
26
     * @throws \BadMethodCallException
27
     */
28
    public function __get($name)
29
    {
30
        throw new \BadMethodCallException(str_replace('_fn_', '', $name)
31
            . '() is not implemented in the FnStream');
32
    }
33
    /**
34
     * The close method is called on the underlying stream only if possible.
35
     */
36
    public function __destruct()
37
    {
38
        if (isset($this->_fn_close)) {
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_close does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
39
            call_user_func($this->_fn_close);
40
        }
41
    }
42
    /**
43
     * An unserialize would allow the __destruct to run when the unserialized value goes out of scope.
44
     * @throws \LogicException
45
     */
46
    public function __wakeup()
47
    {
48
        throw new \LogicException('FnStream should never be unserialized');
49
    }
50
    /**
51
     * Adds custom functionality to an underlying stream by intercepting
52
     * specific method calls.
53
     *
54
     * @param StreamInterface $stream  Stream to decorate
0 ignored issues
show
Bug introduced by
The type One\Http\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...
55
     * @param array           $methods Hash of method name to a closure
56
     *
57
     * @return FnStream
58
     */
59
    public static function decorate(\Psr\Http\Message\StreamInterface $stream, array $methods)
60
    {
61
        // If any of the required methods were not provided, then simply
62
        // proxy to the decorated stream.
63
        foreach (array_diff(self::$slots, array_keys($methods)) as $diff) {
64
            $methods[$diff] = [$stream, $diff];
65
        }
66
        return new self($methods);
67
    }
68
    public function __toString()
69
    {
70
        return call_user_func($this->_fn___toString);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn___toString does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
71
    }
72
    public function close()
73
    {
74
        return call_user_func($this->_fn_close);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_close does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
    }
76
    public function detach()
77
    {
78
        return call_user_func($this->_fn_detach);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_detach does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
79
    }
80
    public function getSize()
81
    {
82
        return call_user_func($this->_fn_getSize);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_getSize does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
    }
84
    public function tell()
85
    {
86
        return call_user_func($this->_fn_tell);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_tell does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
    }
88
    public function eof()
89
    {
90
        return call_user_func($this->_fn_eof);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_eof does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
    }
92
    public function isSeekable()
93
    {
94
        return call_user_func($this->_fn_isSeekable);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_isSeekable does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
    }
96
    public function rewind()
97
    {
98
        call_user_func($this->_fn_rewind);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_rewind does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
99
    }
100
    public function seek($offset, $whence = SEEK_SET)
101
    {
102
        call_user_func($this->_fn_seek, $offset, $whence);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_seek does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
    }
104
    public function isWritable()
105
    {
106
        return call_user_func($this->_fn_isWritable);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_isWritable does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
107
    }
108
    public function write($string)
109
    {
110
        return call_user_func($this->_fn_write, $string);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_write does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
111
    }
112
    public function isReadable()
113
    {
114
        return call_user_func($this->_fn_isReadable);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_isReadable does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
115
    }
116
    public function read($length)
117
    {
118
        return call_user_func($this->_fn_read, $length);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_read does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
119
    }
120
    public function getContents()
121
    {
122
        return call_user_func($this->_fn_getContents);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_getContents does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
123
    }
124
    public function getMetadata($key = null)
125
    {
126
        return call_user_func($this->_fn_getMetadata, $key);
0 ignored issues
show
Bug Best Practice introduced by
The property _fn_getMetadata does not exist on One\Http\FnStream. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
    }
128
}
129