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 |
||
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) |
|
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++) { |
54 | 3 | $process = new Process($this->runnable); |
|
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) |
|
89 | |||
90 | /** |
||
91 | * start the pool |
||
92 | */ |
||
93 | 6 | public function start() |
|
106 | } |
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 theid
property of an instance of theAccount
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.