GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( afc5ea...828b9a )
by Cees-Jan
23:31 queued 06:50
created

Factory::parentFromClass()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.1598

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 17
cts 23
cp 0.7391
rs 8.8571
cc 3
eloc 26
nc 3
nop 4
crap 3.1598
1
<?php
2
3
namespace WyriHaximus\React\ChildProcess\Messenger;
4
5
use React\ChildProcess\Process;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WyriHaximus\React\ChildProcess\Messenger\Process.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use React\EventLoop\LoopInterface;
7
use React\Promise\FulfilledPromise;
8
use React\Stream\Stream;
9
use React\Stream\Util;
10
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory;
11
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
12
13
class Factory
14
{
15
    const INTERVAL = 0.1;
16
    const TIMEOUT = 13;
17
    const TERMINATE_TIMEOUT = 1;
18
    const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register';
19
20
    /**
21
     * @param  Process                         $process
22
     * @param  LoopInterface                   $loop
23
     * @param  array                           $options
24
     * @param  float                           $interval
25
     * @return \React\Promise\PromiseInterface
26
     */
27 2
    public static function parent(
28
        Process $process,
29
        LoopInterface $loop,
30
        array $options = [],
31
        $interval = self::INTERVAL
32
    ) {
33 2
        $process->start($loop, $interval);
34
35 2
        return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])->
36
            then(function () use ($process, $options) {
37 2
                $messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [
38 2
                    'read_err' => 'stderr',
39 2
                    'read' => 'stdout',
40 2
                    'write' => 'stdin',
41
                    'callForward' => function ($name, $arguments) use ($process) {
42 1
                        return call_user_func_array([$process, $name], $arguments);
43 2
                    },
44 2
                ] + $options);
45
46 2
                Util::forwardEvents($process, $messenger, [
47 2
                    'exit',
48
                ]);
49
50 2
                return \React\Promise\resolve($messenger);
51 2
            })
52
        ;
53
    }
54
55
    /**
56
     * @param  LoopInterface $loop
57
     * @param  array         $options
58
     * @param  null          $termiteCallable
59
     * @return Messenger
60
     */
61 2
    public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null)
62
    {
63 2
        $stdin  = new Stream(STDIN, $loop);
64 2
        $stdout = new Stream(STDOUT, $loop);
65 2
        $stderr = new Stream(STDERR, $loop);
66
67 2
        $messenger = new Messenger($stdin, $stdout, $stderr, [
68
            'read' => 'stdin',
69
            'write_err' => 'stderr',
70
            'write' => 'stdout',
71 2
        ] + $options);
72
73
        $closeNStop = function () use ($loop) {
74
            $loop->stop();
75 2
        };
76 2
        $stdin->on('close', $closeNStop);
77 2
        $stdout->on('close', $closeNStop);
78 2
        $stderr->on('close', $closeNStop);
79
80 2
        if ($termiteCallable === null) {
81
            $termiteCallable = function () use ($loop) {
82 1
                $loop->addTimer(
83 1
                    self::TERMINATE_TIMEOUT,
84
                    [
85 1
                        $loop,
86 1
                        'stop',
87
                    ]
88
                );
89 2
            };
90
        }
91
92 2
        $messenger->registerRpc(
93 2
            Messenger::TERMINATE_RPC,
94
            function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
95 1
                $messenger->emit('terminate', [
96 1
                    $messenger,
97
                ]);
98 1
                $termiteCallable($payload, $messenger);
99
100 1
                return new FulfilledPromise([]);
101 2
            }
102
        );
103
104 2
        return $messenger;
105
    }
106
107
    /**
108
     * @param  string                          $className
109
     * @param  LoopInterface                   $loop
110
     * @param  array                           $options
111
     * @param  float                           $interval
112
     * @throws \Exception
113
     * @return \React\Promise\PromiseInterface
114
     */
115 2
    public static function parentFromClass(
116
        $className,
117
        LoopInterface $loop,
118
        array $options = [],
119
        $interval = self::INTERVAL
120
    ) {
121 2
        if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
122 1
            throw new \Exception('Given class doesn\'t implement ChildInterface');
123
        }
124
125 1
        $template = '%s';
126 1
        if (isset($options['cmdTemplate'])) {
127
            $template = $options['cmdTemplate'];
128
            unset($options['cmdTemplate']);
129
        }
130
131 1
        $command = PHP_BINARY . ' ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
132 1
        $process = new Process(
133 1
            sprintf(
134
                $template,
135 1
                $command . ' ' . ArgvEncoder::encode($options)
136
            )
137
        );
138
139 1
        return static::parent(
140
            $process,
141
            $loop,
142
            $options,
143 1
            $interval
144
        )->then(function (Messenger $messenger) use ($className) {
145 1
            return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
146 1
                'className' => $className,
147 1
            ]))->then(function () use ($messenger) {
148 1
                return \React\Promise\resolve($messenger);
149 1
            });
150 1
        });
151
    }
152
}
153