Passed
Pull Request — 5.1 (#1748)
by guanguans
09:27
created

Unix::readAndWrite()   F

Complexity

Conditions 27
Paths 1802

Size

Total Lines 83
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 47
nc 1802
nop 2
dl 0
loc 83
rs 0
c 0
b 0
f 0

How to fix   Long Method    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
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
12
namespace think\process\pipes;
13
14
use think\Process;
15
16
class Unix extends Pipes
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
17
{
18
19
    /** @var bool */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
20
    private $ttyMode;
0 ignored issues
show
Coding Style introduced by
Private member variable "ttyMode" must be prefixed with an underscore
Loading history...
21
    /** @var bool */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    private $ptyMode;
0 ignored issues
show
Coding Style introduced by
Private member variable "ptyMode" must be prefixed with an underscore
Loading history...
23
    /** @var bool */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
24
    private $disableOutput;
0 ignored issues
show
Coding Style introduced by
Private member variable "disableOutput" must be prefixed with an underscore
Loading history...
25
26
    public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
27
    {
28
        $this->ttyMode       = (bool) $ttyMode;
29
        $this->ptyMode       = (bool) $ptyMode;
30
        $this->disableOutput = (bool) $disableOutput;
31
32
        if (is_resource($input)) {
33
            $this->input = $input;
34
        } else {
35
            $this->inputBuffer = (string) $input;
36
        }
37
    }
38
39
    public function __destruct()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
40
    {
41
        $this->close();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    public function getDescriptors()
48
    {
49
        if ($this->disableOutput) {
50
            $nullstream = fopen('/dev/null', 'c');
51
52
            return [
53
                ['pipe', 'r'],
54
                $nullstream,
55
                $nullstream,
56
            ];
57
        }
58
59
        if ($this->ttyMode) {
60
            return [
61
                ['file', '/dev/tty', 'r'],
62
                ['file', '/dev/tty', 'w'],
63
                ['file', '/dev/tty', 'w'],
64
            ];
65
        }
66
67
        if ($this->ptyMode && Process::isPtySupported()) {
68
            return [
69
                ['pty'],
70
                ['pty'],
71
                ['pty'],
72
            ];
73
        }
74
75
        return [
76
            ['pipe', 'r'],
77
            ['pipe', 'w'], // stdout
78
            ['pipe', 'w'], // stderr
79
        ];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85
    public function getFiles()
86
    {
87
        return [];
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $blocking should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $close should have a doc-comment as per coding-style.
Loading history...
91
     * {@inheritdoc}
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function readAndWrite($blocking, $close = false)
94
    {
95
96
        if (1 === count($this->pipes) && [0] === array_keys($this->pipes)) {
97
            fclose($this->pipes[0]);
98
            unset($this->pipes[0]);
99
        }
100
101
        if (empty($this->pipes)) {
102
            return [];
103
        }
104
105
        $this->unblock();
106
107
        $read = [];
108
109
        if (null !== $this->input) {
110
            $r = array_merge($this->pipes, ['input' => $this->input]);
111
        } else {
112
            $r = $this->pipes;
113
        }
114
115
        unset($r[0]);
116
117
        $w = isset($this->pipes[0]) ? [$this->pipes[0]] : null;
118
        $e = null;
119
120
        if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
0 ignored issues
show
Bug introduced by
It seems like $w can also be of type null; however, parameter $write of stream_select() does only seem to accept array, 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

120
        if (false === $n = @stream_select($r, /** @scrutinizer ignore-type */ $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
Loading history...
Bug introduced by
$e of type null is incompatible with the type array expected by parameter $except of stream_select(). ( Ignorable by Annotation )

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

120
        if (false === $n = @stream_select($r, $w, /** @scrutinizer ignore-type */ $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
Loading history...
Bug introduced by
It seems like $blocking ? think\Proces...ECISION * 1000000.0 : 0 can also be of type double; however, parameter $tv_usec of stream_select() does only seem to accept integer, 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

120
        if (false === $n = @stream_select($r, $w, $e, 0, /** @scrutinizer ignore-type */ $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
Loading history...
121
122
            if (!$this->hasSystemCallBeenInterrupted()) {
123
                $this->pipes = [];
124
            }
125
126
            return $read;
127
        }
128
129
        if (0 === $n) {
130
            return $read;
131
        }
132
133
        foreach ($r as $pipe) {
134
135
            $type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input';
136
            $data = '';
137
            while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
138
                $data .= $dataread;
139
            }
140
141
            if ('' !== $data) {
142
                if ('input' === $type) {
143
                    $this->inputBuffer .= $data;
144
                } else {
145
                    $read[$type] = $data;
146
                }
147
            }
148
149
            if (false === $data || (true === $close && feof($pipe) && '' === $data)) {
150
                if ('input' === $type) {
151
                    $this->input = null;
152
                } else {
153
                    fclose($this->pipes[$type]);
154
                    unset($this->pipes[$type]);
155
                }
156
            }
157
        }
158
159
        if (null !== $w && 0 < count($w)) {
160
            while (strlen($this->inputBuffer)) {
161
                $written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k
162
                if ($written > 0) {
163
                    $this->inputBuffer = (string) substr($this->inputBuffer, $written);
164
                } else {
165
                    break;
166
                }
167
            }
168
        }
169
170
        if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
171
            fclose($this->pipes[0]);
172
            unset($this->pipes[0]);
173
        }
174
175
        return $read;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
181
    public function areOpen()
182
    {
183
        return (bool) $this->pipes;
184
    }
185
186
    /**
187
     * 创建一个新的 UnixPipes 实例
188
     * @param Process         $process
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
189
     * @param string|resource $input
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
190
     * @return self
191
     */
192
    public static function create(Process $process, $input)
193
    {
194
        return new static($process->isTty(), $process->isPty(), $input, $process->isOutputDisabled());
195
    }
196
}
197