AsynchronousProcessRunner   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 16.98 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 9
loc 53
c 0
b 0
f 0
wmc 4
lcom 1
cbo 5
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A runProcesses() 0 8 2
A createProcessDefinition() 9 9 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
declare(strict_types=1);
3
4
/**
5
 * File: AsynchronousProcessRunner.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\ReactPhpPlayground\Standalone\ChildProcess;
12
13
use MSlwk\ReactPhpPlayground\Api\Data\ProcessInterface;
14
use MSlwk\ReactPhpPlayground\Model\Adapter\ReactPHP\ProcessFactory;
15
use MSlwk\TypeSafeArray\ObjectArray;
16
use React\EventLoop\LoopInterface;
17
18
/**
19
 * Class AsynchronousProcessRunner
20
 * @package MSlwk\ReactPhpPlayground\Standalone\ChildProcess
21
 */
22
class AsynchronousProcessRunner implements ProcessRunnerInterface
23
{
24
    /**
25
     * @var LoopInterface
26
     */
27
    private $loop;
28
29
    /**
30
     * @var ProcessFactory
31
     */
32
    private $processFactory;
33
34
    /**
35
     * AsynchronousProcessRunner constructor.
36
     * @param LoopInterface $loop
37
     * @param ProcessFactory $processFactory
38
     */
39
    public function __construct(
40
        LoopInterface $loop,
41
        ProcessFactory $processFactory
42
    ) {
43
        $this->loop = $loop;
44
        $this->processFactory = $processFactory;
45
    }
46
47
    /**
48
     * @param ObjectArray $processes
49
     * @return void
50
     */
51
    public function runProcesses(ObjectArray $processes): void
52
    {
53
        /** @var ProcessInterface $process */
54
        foreach ($processes as $process) {
55
            $this->createProcessDefinition($process);
56
        }
57
        $this->loop->run();
58
    }
59
60
    /**
61
     * @codeCoverageIgnore
62
     * @param ProcessInterface $process
63
     * @return void
64
     */
65 View Code Duplication
    protected function createProcessDefinition(ProcessInterface $process): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67
        $reactProcess = $this->processFactory->create($process->getCommand());
68
        $reactProcess->start($this->loop);
69
70
        $reactProcess->stdout->on('data', function ($chunk) {
71
            echo $chunk;
72
        });
73
    }
74
}
75