WrapperRunner   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 6
dl 0
loc 197
ccs 0
cts 99
cp 0
rs 9.6
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 1
A load() 0 7 2
B assignAllPendingTests() 0 18 5
A sendStopMessages() 0 6 2
A waitForAllToFinish() 0 14 4
A waitForStreamsToChange() 0 12 2
A progressedWorkers() 0 17 4
A streamsOf() 0 9 2
A complete() 0 12 2
A setExitCode() 0 10 3
A flushWorker() 0 8 2
A startWorkers() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParaTest\Runners\PHPUnit;
6
7
class WrapperRunner extends BaseRunner
8
{
9
    const PHPUNIT_FAILURES = 1;
10
    const PHPUNIT_ERRORS = 2;
11
12
    /**
13
     * @var array
14
     */
15
    protected $streams;
16
17
    /**
18
     * @var Worker[]
19
     */
20
    protected $workers;
21
22
    /**
23
     * @var array
24
     */
25
    protected $modified;
26
27
    public function run()
28
    {
29
        parent::run();
30
31
        $this->startWorkers();
32
        $this->assignAllPendingTests();
33
        $this->sendStopMessages();
34
        $this->waitForAllToFinish();
35
        $this->complete();
36
    }
37
38
    protected function load()
39
    {
40
        if ($this->options->functional) {
0 ignored issues
show
Documentation introduced by
The property $functional is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
            throw new \RuntimeException('The `functional` option is not supported yet in the WrapperRunner. Only full classes can be run due to the current PHPUnit commands causing classloading issues.');
42
        }
43
        parent::load();
44
    }
45
46
    private function startWorkers()
47
    {
48
        $wrapper = realpath(__DIR__ . '/../../../bin/phpunit-wrapper');
49
        for ($i = 1; $i <= $this->options->processes; ++$i) {
0 ignored issues
show
Documentation introduced by
The property $processes is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
            $worker = new Worker();
51
            if ($this->options->noTestTokens) {
0 ignored issues
show
Documentation introduced by
The property $noTestTokens is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
                $token = null;
53
                $uniqueToken = null;
54
            } else {
55
                $token = $i;
56
                $uniqueToken = uniqid();
57
            }
58
            $worker->start($wrapper, $token, $uniqueToken);
59
            $this->streams[] = $worker->stdout();
60
            $this->workers[] = $worker;
61
        }
62
    }
63
64
    private function assignAllPendingTests()
65
    {
66
        $phpunit = $this->options->phpunit;
0 ignored issues
show
Documentation introduced by
The property $phpunit is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
        $phpunitOptions = $this->options->filtered;
0 ignored issues
show
Documentation introduced by
The property $filtered is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
        // $phpunitOptions['no-globals-backup'] = null;  // removed in phpunit 6.0
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
69
        while (count($this->pending)) {
70
            $this->waitForStreamsToChange($this->streams);
71
            foreach ($this->progressedWorkers() as $worker) {
72
                if ($worker->isFree()) {
73
                    $this->flushWorker($worker);
74
                    $pending = array_shift($this->pending);
75
                    if ($pending) {
76
                        $worker->assign($pending, $phpunit, $phpunitOptions);
77
                    }
78
                }
79
            }
80
        }
81
    }
82
83
    private function sendStopMessages()
84
    {
85
        foreach ($this->workers as $worker) {
86
            $worker->stop();
87
        }
88
    }
89
90
    private function waitForAllToFinish()
91
    {
92
        $toStop = $this->workers;
93
        while (count($toStop) > 0) {
94
            $toCheck = $this->streamsOf($toStop);
95
            $new = $this->waitForStreamsToChange($toCheck);
0 ignored issues
show
Unused Code introduced by
$new is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
            foreach ($this->progressedWorkers() as $index => $worker) {
97
                if (!$worker->isRunning()) {
98
                    $this->flushWorker($worker);
99
                    unset($toStop[$index]);
100
                }
101
            }
102
        }
103
    }
104
105
    // put on WorkersPool
106
    private function waitForStreamsToChange(array $modified)
107
    {
108
        $write = [];
109
        $except = [];
110
        $result = stream_select($modified, $write, $except, 1);
111
        if ($result === false) {
112
            throw new \RuntimeException('stream_select() returned an error while waiting for all workers to finish.');
113
        }
114
        $this->modified = $modified;
115
116
        return $result;
117
    }
118
119
    /**
120
     * put on WorkersPool.
121
     *
122
     * @return Worker[]
123
     */
124
    private function progressedWorkers(): array
125
    {
126
        $result = [];
127
        foreach ($this->modified as $modifiedStream) {
128
            $found = null;
129
            foreach ($this->streams as $index => $stream) {
130
                if ($modifiedStream === $stream) {
131
                    $found = $index;
132
                    break;
133
                }
134
            }
135
            $result[$found] = $this->workers[$found];
136
        }
137
        $this->modified = [];
138
139
        return $result;
140
    }
141
142
    /**
143
     * Returns the output streams of a subset of workers.
144
     *
145
     * @param array    keys are positions in $this->workers
146
     *
147
     * @return array
148
     */
149
    private function streamsOf(array $workers): array
150
    {
151
        $streams = [];
152
        foreach (array_keys($workers) as $index) {
153
            $streams[$index] = $this->streams[$index];
154
        }
155
156
        return $streams;
157
    }
158
159
    private function complete()
160
    {
161
        $this->setExitCode();
162
        $this->printer->printResults();
163
        $this->interpreter->rewind();
164
        $this->log();
165
        $this->logCoverage();
166
        $readers = $this->interpreter->getReaders();
167
        foreach ($readers as $reader) {
168
            $reader->removeLog();
169
        }
170
    }
171
172
    private function setExitCode()
173
    {
174
        if ($this->interpreter->getTotalErrors()) {
0 ignored issues
show
Documentation Bug introduced by
The method getTotalErrors does not exist on object<ParaTest\Logging\LogInterpreter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
175
            $this->exitcode = self::PHPUNIT_ERRORS;
176
        } elseif ($this->interpreter->getTotalFailures()) {
0 ignored issues
show
Documentation Bug introduced by
The method getTotalFailures does not exist on object<ParaTest\Logging\LogInterpreter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
177
            $this->exitcode = self::PHPUNIT_FAILURES;
178
        } else {
179
            $this->exitcode = 0;
180
        }
181
    }
182
183
    private function flushWorker(Worker $worker)
184
    {
185
        if ($this->hasCoverage()) {
186
            $this->getCoverage()->addCoverageFromFile($worker->getCoverageFileName());
187
        }
188
        $worker->printFeedback($this->printer);
189
        $worker->reset();
190
    }
191
192
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
193
    private function testIsStillRunning($test)
194
    {
195
        if(!$test->isDoneRunning()) return true;
196
        $this->setExitCode($test);
197
        $test->stop();
198
        if (static::PHPUNIT_FATAL_ERROR === $test->getExitCode())
199
            throw new \Exception($test->getStderr(), $test->getExitCode());
200
        return false;
201
    }
202
     */
203
}
204