AutoScheduler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 14 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 7
loc 50
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 7 15 2
A isEmpty() 0 4 1
A interruptConsume() 0 1 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
2
3
namespace mpyw\Co\Internal;
4
use mpyw\Co\CURLException;
5
use React\Promise\Deferred;
6
use React\Promise\PromiseInterface;
7
8
class AutoScheduler extends AbstractScheduler
9
{
10
    /**
11
     * Constructor.
12
     * Initialize cURL multi handle.
13
     * @param CoOption $options
14
     * @param resource $mh      curl_multi
15
     */
16 2
    public function __construct(CoOption $options, $mh)
17 2
    {
18 2
        curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, $options['concurrency']);
19 2
        $this->mh = $mh;
20 2
        $this->options = $options;
21 2
    }
22
23
    /**
24
     * Call curl_multi_add_handle().
25
     * @param resource $ch
26
     * @return PromiseInterface
27
     */
28 2
    public function add($ch)
29 2
    {
30 2
        $deferred = new Deferred;
31 2
        $errno = curl_multi_add_handle($this->mh, $ch);
32 2 View Code Duplication
        if ($errno !== CURLM_OK) {
33
            // @codeCoverageIgnoreStart
34
            $msg = curl_multi_strerror($errno) . ": $ch";
35
            $deferred->reject(new \RuntimeException($msg));
36
            return $deferred->promise();
37
            // @codeCoverageIgnoreEnd
38
        }
39 2
        $this->added[(string)$ch] = $ch;
40 2
        $this->deferreds[(string)$ch] = $deferred;
41 2
        return $deferred->promise();
42
    }
43
44
    /**
45
     * Are there no cURL handles?
46
     * @return bool
47
     */
48 2
    public function isEmpty()
49 2
    {
50 2
        return !$this->added;
51
    }
52
53
    /**
54
     * Do nothing.
55
     */
56
    protected function interruptConsume() {}
57
}
58