Completed
Push — 2.0 ( 14c778...b5ef61 )
by Marco
11:18
created

ProcessUtils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 38.81 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 0
dl 26
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A kill() 15 15 3
A isRunning() 0 5 1
A getNiceness() 0 5 1
A setNiceness() 0 13 2
A filterNiceness() 11 11 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\Components;
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 ProcessUtils {
29
    
30
    /**
31
     * Kill a child process
32
     * 
33
     * @return  bool
34
     */
35 View Code Duplication
    public static function kill($pid, $lagger_timeout = 0) {
0 ignored issues
show
Unused Code introduced by
The parameter $lagger_timeout is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
36
37
        $kill_time = time() + self::$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 posix_kill($pid, SIGKILL);
48
49
    }
50
    
51
    /**
52
     * Return true if process is still running, false otherwise
53
     * 
54
     * @return  bool
55
     */
56
    public static function isRunning($pid) {
57
58
        return (pcntl_waitpid($pid, $status, WNOHANG) === 0);
59
60
    }
61
    
62
    public static function getNiceness($pid = null) {
63
        
64
        return pcntl_getpriority($pid);
65
        
66
    }
67
    
68
    public function setNiceness($niceness = null, $pid = null) {
69
70
        $niceness = self::filterNiceness($niceness);
71
72
        if ( is_null($pid) ) {
73
74
            return proc_nice($niceness);
75
76
        } 
77
        
78
        return pcntl_setpriority($pid, $$niceness);
79
80
    }
81
82 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...
83
84
        return filter_var($niceness, FILTER_VALIDATE_INT, array(
85
            'options' => array(
86
                'default' => 0,
87
                'min_range' => -10,
88
                'max_range' => 10
89
            )
90
        ));
91
92
    }
93
    
94
}