Completed
Pull Request — master (#597)
by Mateusz
03:44
created

AbortableProcess   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 15
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 13 3
1
<?php
2
/**
3
 * AbortableProcess implements a workaround to make Symfony Process not kill itself when we
4
 * trap a Unix signal.
5
 *
6
 * Process is relying on error_get_last to learn if the stream reading was interrupted by a signal
7
 * (see UnixSignal's readAndWrite, the hasSystemCallBeenInterrupted call). Unfortunately this doesn't
8
 * work if we trapp the signal, which wipes out the error, which makes Process think it's some
9
 * other kind of error which makes it commit suicide for no good reason.
10
 *
11
 * Workaround is to avoid blocking stream_select as ->run nor ->wait do. Fortunately we can call
12
 * ->isRunning instead, which uses a non-blocking readPipes via updateStatus. This means our callback
13
 * will still get called if we loop over ->isRunning.
14
 */
15
class AbortableProcess extends \Symfony\Component\Process\Process {
16
	public function run($callback = null) {
17
		$this->start($callback);
18
		while($this->isRunning()) {
19
			$this->checkTimeout();
20
			sleep(1);
21
		}
22
23
		if ($this->hasBeenSignaled()) {
24
			throw new RuntimeException(sprintf('The process has been signaled with signal "%s".', $this->getTermSignal));
0 ignored issues
show
Bug introduced by
The property getTermSignal does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
		}
26
27
		return $this->getExitCode();
28
	}
29
}
30