Completed
Push — 2.0 ( dd3689...ee0168 )
by Marco
12:16
created

ProcessTools   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 13.1 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 0
cbo 0
dl 11
loc 84
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A term() 0 15 3
A kill() 0 5 1
A isRunning() 0 5 1
A getNiceness() 0 5 1
A setNiceness() 0 13 2
A filterNiceness() 11 11 1
A getPid() 0 5 1

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\Extender\Utils;
2
3
use \Exception;
4
5
/**
6
 * Basic signaling for extender components
7
 *
8
 * @package     Comodojo Framework
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     GPL-3.0+
11
 *
12
 * LICENSE:
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 */
27
28
class ProcessTools {
29
30
    /**
31
     * Terminate a process
32
     *
33
     * @return  bool
34
     */
35
    public static function term($pid, $lagger_timeout = 0) {
36
37
        $kill_time = time() + $lagger_timeout;
38
39
        $term = posix_kill($pid, SIGTERM);
40
41
        while ( time() < $kill_time ) {
42
43
            if ( !self::isRunning($pid) ) return $term;
44
45
        }
46
47
        return self::kill($pid);
48
49
    }
50
51
    /**
52
     * Kill a process
53
     *
54
     * @return  bool
55
     */
56
    public static function kill($pid) {
57
58
        return posix_kill($pid, SIGKILL);
59
60
    }
61
62
    /**
63
     * Return true if process is still running, false otherwise
64
     *
65
     * @return  bool
66
     */
67
    public static function isRunning($pid) {
68
69
        return (pcntl_waitpid($pid, $status, WNOHANG) === 0);
70
71
    }
72
73
    public static function getNiceness($pid = null) {
74
75
        return pcntl_getpriority($pid);
76
77
    }
78
79
    public static function setNiceness($niceness = null, $pid = null) {
80
81
        $niceness = self::filterNiceness($niceness);
82
83
        if ( is_null($pid) ) {
84
85
            return proc_nice($niceness);
86
87
        }
88
89
        return pcntl_setpriority($pid, $$niceness);
90
91
    }
92
93 View Code Duplication
    public static function filterNiceness($niceness=null) {
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...
94
95
        return filter_var($niceness, FILTER_VALIDATE_INT, array(
96
            'options' => array(
97
                'default' => 0,
98
                'min_range' => -10,
99
                'max_range' => 10
100
            )
101
        ));
102
103
    }
104
105
    public static function getPid() {
106
107
        return posix_getpid();
108
109
    }
110
111
}
112