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
Pull Request — master (#4)
by Cees-Jan
05:38 queued 02:29
created

Factory::child()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 2

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 44
ccs 24
cts 24
cp 1
rs 8.8571
cc 2
eloc 28
nc 2
nop 3
crap 2
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 Tivie\OS\Detector;
11
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory;
12
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
13
14
class Factory
15
{
16
    const INTERVAL = 0.1;
17
    const TIMEOUT = 13;
18
    const TERMINATE_TIMEOUT = 1;
19
    const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register';
20
21
    /**
22
     * @param Process $process
23
     * @param LoopInterface $loop
24
     * @param array $options
25
     * @param float $interval
26
     * @return \React\Promise\PromiseInterface
27
     */
28 2
    public static function parent(
29
        Process $process,
30
        LoopInterface $loop,
31
        array $options = [],
32
        $interval = self::INTERVAL
33
    ) {
34 2
        $process->start($loop, $interval);
35
36 2
        return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])->
37
            then(function () use ($process, $options) {
38 2
                $messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [
39 2
                    'read_err' => 'stderr',
40 2
                    'read' => 'stdout',
41 2
                    'write' => 'stdin',
42
                    'callForward' => function ($name, $arguments) use ($process) {
43 1
                        return call_user_func_array([$process, $name], $arguments);
44 2
                    },
45 2
                ] + $options);
46
47 2
                Util::forwardEvents($process, $messenger, [
48 2
                    'exit',
49
                ]);
50
51 2
                return \React\Promise\resolve($messenger);
52 2
            })
53
        ;
54
    }
55
56
    /**
57
     * @param LoopInterface $loop
58
     * @param array $options
59
     * @param null $termiteCallable
60
     * @return Messenger
61
     */
62 2
    public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null)
63
    {
64 2
        $stdin  = new Stream(STDIN, $loop);
65 2
        $stdout = new Stream(STDOUT, $loop);
66 2
        $stderr = new Stream(STDERR, $loop);
67
68 2
        $messenger = new Messenger($stdin, $stdout, $stderr, [
69
            'read' => 'stdin',
70
            'write_err' => 'stderr',
71
            'write' => 'stdout',
72 2
        ] + $options);
73
74
        $closeNStop = function () use ($loop) {
75
            $loop->stop();
76 2
        };
77 2
        $stdin->on('close', $closeNStop);
78 2
        $stdout->on('close', $closeNStop);
79 2
        $stderr->on('close', $closeNStop);
80
81 2
        if ($termiteCallable === null) {
82
            $termiteCallable = function () use ($loop) {
83 1
                $loop->addTimer(
84 1
                    self::TERMINATE_TIMEOUT,
0 ignored issues
show
Documentation introduced by
self::TERMINATE_TIMEOUT is of type integer, but the function expects a object<React\EventLoop\numeric>.

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...
85
                    [
86 1
                        $loop,
87 1
                        'stop',
88
                    ]
89
                );
90 2
            };
91
        }
92
93 2
        $messenger->registerRpc(
94 2
            Messenger::TERMINATE_RPC,
95
            function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
96 1
                $messenger->emit('terminate', [
97 1
                    $messenger,
98
                ]);
99 1
                $termiteCallable($payload, $messenger);
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
     * @return \React\Promise\PromiseInterface
113
     * @throws \Exception
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
        $process = new Process(
132
            sprintf(
133
                $template,
134 1
                self::getProcessForCurrentOS() . ' ' . ArgvEncoder::encode($options)
135
            )
136
        );
137
138 1
        return static::parent(
139
            $process,
140
            $loop,
141
            $options,
142
            $interval
143
        )->then(function (Messenger $messenger) use ($className) {
144 1
            return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
145 1
                'className' => $className,
146 1
            ]))->then(function () use ($messenger) {
147 1
                return \React\Promise\resolve($messenger);
148 1
            });
149 1
        });
150
    }
151
152
    /**
153
     * @param Detector|null $detector
154
     * @return string
155
     * @throws \Exception
156
     */
157 5
    public static function getProcessForCurrentOS(Detector $detector = null)
158
    {
159 5
        if ($detector === null) {
160 2
            $detector = new Detector();
161
        }
162
163 5
        if ($detector->isUnixLike()) {
164 3
            return 'php ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
165
        }
166
167 2
        if ($detector->isWindowsLike()) {
168 1
            return 'php.exe ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
169
        }
170
171 1
        throw new \Exception('Unknown OS family');
172
    }
173
}
174