ParallelPool   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 10.99 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 14
lcom 1
cbo 2
dl 10
loc 91
ccs 40
cts 40
cp 1
rs 10
c 2
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
B keep() 0 16 5
A start() 5 13 3
A reload() 5 15 3

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
 * @author Jenner <[email protected]>
4
 * @blog http://www.huyanping.cn
5
 * @license https://opensource.org/licenses/MIT MIT
6
 * @datetime: 2015/11/19 20:49
7
 */
8
9
namespace Jenner\SimpleFork;
10
11
/**
12
 * parallel pool
13
 *
14
 * @package Jenner\SimpleFork
15
 */
16
class ParallelPool extends AbstractPool
17
{
18
19
    /**
20
     * @var callable|Runnable sub process callback
21
     */
22
    protected $runnable;
23
24
    /**
25
     * @var int max process count
26
     */
27
    protected $max;
28
29
    /**
30
     * @param callable|Runnable $callback
31
     * @param int $max
32
     */
33 9
    public function __construct($callback, $max = 4)
34
    {
35 9
        if (!is_callable($callback) && !($callback instanceof Runnable)) {
36 3
            throw new \InvalidArgumentException('callback must be a callback function or a object of Runnalbe');
37
        }
38
39 6
        $this->runnable = $callback;
0 ignored issues
show
Documentation Bug introduced by
It seems like $callback can also be of type object<Jenner\SimpleFork\Runnable>. However, the property $runnable is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
40 6
        $this->max = $max;
41 6
    }
42
43
    /**
44
     * start the same number processes and kill the old sub process
45
     * just like nginx -s reload
46
     * this method will block until all the old process exit;
47
     *
48
     * @param bool $block
49
     */
50 3
    public function reload($block = true)
51
    {
52 3
        $old_processes = $this->processes;
53 3 View Code Duplication
        for ($i = 0; $i < $this->max; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54 3
            $process = new Process($this->runnable);
0 ignored issues
show
Documentation introduced by
$this->runnable is of type callable, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55 3
            $process->start();
56 3
            $this->processes[$process->getPid()] = $process;
57 3
        }
58
59 3
        foreach ($old_processes as $process) {
60 3
            $process->shutdown();
61 3
            $process->wait($block);
62 3
            unset($this->processes[$process->getPid()]);
63 3
        }
64 3
    }
65
66
    /**
67
     * keep sub process count
68
     *
69
     * @param bool $block block the master process
70
     * to keep the sub process count all the time
71
     * @param int $interval check time interval
72
     */
73 3
    public function keep($block = false, $interval = 100)
74
    {
75
        do {
76 3
            $this->start();
77
78
            // recycle sub process and delete the processes
79
            // which are not running from process list
80 3
            foreach ($this->processes as $process) {
81 3
                if (!$process->isRunning()) {
82 3
                    unset($this->processes[$process->getPid()]);
83 3
                }
84 3
            }
85
86 3
            $block ? usleep($interval) : null;
87 3
        } while ($block);
88 3
    }
89
90
    /**
91
     * start the pool
92
     */
93 6
    public function start()
94
    {
95 6
        $alive_count = $this->aliveCount();
96
        // create sub process and run
97 6
        if ($alive_count < $this->max) {
98 6
            $need = $this->max - $alive_count;
99 6 View Code Duplication
            for ($i = 0; $i < $need; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100 6
                $process = new Process($this->runnable);
0 ignored issues
show
Documentation introduced by
$this->runnable is of type callable, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101 6
                $process->start();
102 6
                $this->processes[$process->getPid()] = $process;
103 6
            }
104 6
        }
105
    }
106
}