PipeQueue::get()   D
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 21.0591

Importance

Changes 6
Bugs 0 Features 6
Metric Value
dl 0
loc 33
ccs 13
cts 25
cp 0.52
rs 4.8196
c 6
b 0
f 6
cc 10
eloc 22
nc 14
nop 1
crap 21.0591

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
 * @author Jenner <[email protected]>
4
 * @blog http://www.huyanping.cn
5
 * @license https://opensource.org/licenses/MIT MIT
6
 * @datetime: 2015/11/24 18:38
7
 */
8
9
namespace Jenner\SimpleFork\Queue;
10
11
12
class PipeQueue implements QueueInterface
13
{
14
    /**
15
     * @var Pipe
16
     */
17
    protected $pipe;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $block;
23
24
    /**
25
     * @param string $filename fifo filename
26
     * @param int $mode
27
     * @param bool $block if blocking
0 ignored issues
show
Bug introduced by
There is no parameter named $block. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     */
29 3
    public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666)
30
    {
31 3
        $this->pipe = new Pipe($filename, $mode);
32 3
        $this->block = false;
33 3
        $this->pipe->setBlock($this->block);
34 3
    }
35
36
    /**
37
     * put value into the queue of channel
38
     *
39
     * @param $value
40
     * @return bool
41
     */
42 3
    public function put($value)
43
    {
44 3
        $len = strlen($value);
45 3
        if ($len > 2147483647) {
46
            throw new \RuntimeException('value is too long');
47
        }
48 3
        $raw = pack('N', $len) . $value;
49 3
        $write_len = $this->pipe->write($raw);
50
51 3
        return $write_len == strlen($raw);
52
    }
53
54
    /**
55
     * get value from the queue of channel
56
     *
57
     * @param bool $block if block when the queue is empty
58
     * @return bool|string
59
     */
60 3
    public function get($block = false)
61
    {
62 3
        if ($this->block != $block) {
63
            $this->pipe->setBlock($block);
64
            $this->block = $block;
65
        }
66 3
        $len = $this->pipe->read(4);
67 3
        if ($len === false) {
68
            throw new \RuntimeException('read pipe failed');
69
        }
70
71 3
        if (strlen($len) === 0) {
72
            return null;
73
        }
74 3
        $len = unpack('N', $len);
75 3
        if (empty($len) || !array_key_exists(1, $len) || empty($len[1])) {
76
            throw new \RuntimeException('data protocol error');
77
        }
78 3
        $len = intval($len[1]);
79
80 3
        $value = '';
81 3
        while (true) {
82 3
            $temp = $this->pipe->read($len);
83 3
            if (strlen($temp) == $len) {
84 3
                return $temp;
85
            }
86
            $value .= $temp;
87
            $len -= strlen($temp);
88
            if ($len == 0) {
89
                return $value;
90
            }
91
        }
92
    }
93
94
    /**
95
     * remove the queue resource
96
     *
97
     * @return bool
98
     */
99
    public function remove()
100
    {
101
        $this->pipe->close();
102
        $this->pipe->remove();
103
    }
104
}