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 |
||
26 | class ProcessLauncher |
||
27 | { |
||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $running = false; |
||
32 | |||
33 | /** |
||
34 | * @var float |
||
35 | */ |
||
36 | private $checkInterval = 0.1; |
||
37 | |||
38 | /** |
||
39 | * Returns whether the launcher is supported on the current system. |
||
40 | * |
||
41 | * @return bool Whether the launcher is supported on the current system. |
||
42 | */ |
||
43 | public function isSupported() |
||
44 | { |
||
45 | return function_exists('proc_open'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns whether the launcher is currently running. |
||
50 | * |
||
51 | * @return bool Whether the launcher is running. |
||
52 | */ |
||
53 | public function isRunning() |
||
54 | { |
||
55 | return $this->running; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Returns the interval used to check whether the process is still alive. |
||
60 | * |
||
61 | * By default, the interval is 1 second. |
||
62 | * |
||
63 | * @param float $checkInterval The check interval. |
||
64 | */ |
||
65 | 2 | public function setCheckInterval($checkInterval) |
|
66 | { |
||
67 | 2 | $this->checkInterval = $checkInterval; |
|
68 | 2 | } |
|
69 | |||
70 | /** |
||
71 | * Launches a process in the foreground. |
||
72 | * |
||
73 | * @param string $command The command to execute. |
||
74 | * @param string[] $arguments Arguments to be quoted and inserted into the |
||
75 | * command. Each key "key" in the array should |
||
76 | * correspond to a placeholder "%key%" in the |
||
77 | * command. |
||
78 | * @param bool $killable Whether the process can be killed by the user. |
||
79 | * |
||
80 | * @return int The exit status of the process. |
||
81 | */ |
||
82 | 2 | public function launchProcess($command, array $arguments = array(), $killable = true) |
|
92 | |||
93 | 2 | View Code Duplication | private function installSignalHandlers($terminable = true) |
|
|||
94 | { |
||
95 | 2 | if (function_exists('pcntl_signal') && !$terminable) { |
|
96 | pcntl_signal(SIGTERM, SIG_IGN); |
||
97 | pcntl_signal(SIGINT, SIG_IGN); |
||
98 | } |
||
99 | 2 | } |
|
100 | |||
101 | 2 | View Code Duplication | private function restoreSignalHandlers($terminable = true) |
108 | |||
109 | 2 | private function run($command, array $arguments) |
|
151 | } |
||
152 |
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.