Completed
Push — master ( bd5bad...380d98 )
by Marco
04:57
created

PosixSignals   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 140
Duplicated Lines 12.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 36.17%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 18
loc 140
ccs 17
cts 47
cp 0.3617
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 10 6
A sigNo() 0 7 2
A sigName() 0 5 2
A on() 0 11 3
A any() 0 7 1
A call() 0 16 4
A setDefault() 0 15 3
A mask() 9 9 2
A unmask() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Daemon\Utils;
2
3
use \Comodojo\Foundation\DataAccess\IteratorTrait;
4
use \Comodojo\Foundation\DataAccess\CountableTrait;
5
use \Comodojo\Foundation\DataAccess\ArrayAccessTrait;
6
use \Iterator;
7
use \Countable;
8
use \ArrayAccess;
9
10
/**
11
 * @package     Comodojo Daemon
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class PosixSignals implements Iterator, Countable, ArrayAccess {
27
28
    use IteratorTrait;
29
    use CountableTrait;
30
    use ArrayAccessTrait;
31
32
    protected $data = [
33
        SIGHUP => 'SIGHUP',
34
        SIGINT => 'SIGINT',
35
        SIGQUIT => 'SIGQUIT',
36
        SIGILL => 'SIGILL',
37
        SIGTRAP => 'SIGTRAP',
38
        SIGABRT => 'SIGABRT',
39
        SIGIOT => 'SIGIOT',
40
        SIGBUS => 'SIGBUS',
41
        SIGFPE => 'SIGFPE',
42
        // Noone can catch SIGKILL!
43
        // SIGKILL => 'SIGKILL',
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        SIGUSR1 => 'SIGUSR1',
45
        SIGSEGV => 'SIGSEGV',
46
        SIGUSR2 => 'SIGUSR2',
47
        SIGPIPE => 'SIGPIPE',
48
        SIGALRM => 'SIGALRM',
49
        SIGTERM => 'SIGTERM',
50
        SIGCHLD => 'SIGCHLD',
51
        SIGCONT => 'SIGCONT',
52
        SIGTSTP => 'SIGTSTP',
53
        SIGTTIN => 'SIGTTIN',
54
        SIGTTOU => 'SIGTTOU',
55
        SIGURG => 'SIGURG',
56
        SIGXCPU => 'SIGXCPU',
57
        SIGXFSZ => 'SIGXFSZ',
58
        SIGVTALRM => 'SIGVTALRM',
59
        SIGPROF => 'SIGPROF',
60
        SIGWINCH => 'SIGWINCH',
61
        SIGSYS => 'SIGSYS',
62
        SIGBABY => 'SIGBABY'
63
    ];
64
65
    protected $pointer = [];
66
67 1
    public function __construct() {
68
69 1
        if ( defined('SIGPOLL') ) $this->data[SIGPOLL] = 'SIGPOLL';
70 1
        if ( defined('SIGPWR') ) $this->data[SIGPWR] = 'SIGPWR';
71 1
        if ( defined('SIGSTKFLT') ) $this->data[SIGSTKFLT] = 'SIGSTKFLT';
72
        // if ( defined('SIGSTOP') ) $this->data[SIGSTOP] = 'SIGSTOP';
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73 1
        if ( defined('SIGIO') ) $this->data[SIGIO] = 'SIGIO';
74 1
        if ( defined('SIGCLD') ) $this->data[SIGCLD] = 'SIGCLD';
75
76 1
    }
77
78
    public function sigNo($signame) {
79
80
        $reverse_signals = array_flip($this->data);
81
82
        return array_key_exists($signame, $reverse_signals) ? $reverse_signals[$signame] : null;
83
84
    }
85
86
    public function sigName($signo) {
87
88
        return array_key_exists($signo, $this->data) ? $this->data[$signo] : null;
89
90
    }
91
92
    public function on(...$signals) {
93
94
        foreach ($signals as $signal) {
95
            if ( !isset($this[$signal]) ) throw new Exception("Signal $signal not supported");
96
        }
97
98
        $this->pointer = $signals;
99
100
        return $this;
101
102
    }
103
104 1
    public function any() {
105
106 1
        $this->pointer = [];
107
108 1
        return $this;
109
110
    }
111
112 1
    public function call($callable) {
113
114 1
        if ( empty($this->pointer) ) {
115 1
            $result = [];
116 1
            foreach ($this as $signo => $signame) {
117 1
                $result[] = $re = pcntl_signal($signo, $callable);
118 1
                if (!$re) echo "\n>>>Signal $signo (".$this->sigName($signo).") failed\n";
119
            }
120 1
            return !in_array(false, $result);
121
        }
122
123
        return array_map(function($signal) {
124
            return pcntl_signal($signal, $callable);
0 ignored issues
show
Bug introduced by
The variable $callable does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
125
        }, $this->pointer);
126
127
    }
128
129
    public function setDefault() {
130
131
        if ( empty($this->pointer) ) {
132
            $result = [];
133
            foreach ($this as $signo => $signame) {
134
                $result[] = pcntl_signal($signo, SIG_DFL);
135
            }
136
            return !in_array(false, $result);
137
        }
138
139
        return array_map(function($signal) {
140
            return pcntl_signal($signal, SIG_DFL);
141
        }, $this->pointer);
142
143
    }
144
145 View Code Duplication
    public function mask() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
147
        if ( empty($this->pointer) ) {
148
            return pcntl_sigprocmask(SIG_SETMASK, (array) $this);
149
        }
150
151
        return pcntl_sigprocmask(SIG_BLOCK, $this->pointer);
152
153
    }
154
155 View Code Duplication
    public function unmask() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
157
        if ( empty($this->pointer) ) {
158
            return pcntl_sigprocmask(SIG_SETMASK, []);
159
        }
160
161
        return pcntl_sigprocmask(SIG_UNBLOCK, $this->pointer);
162
163
    }
164
165
}
166