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 (#13)
by Cees-Jan
02:21
created

Factory::parentFromClass()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 14
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 28
nc 3
nop 4
crap 20
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
    public static function parent(
28
        Process $process,
29
        LoopInterface $loop,
30
        array $options = [],
31
        $interval = self::INTERVAL
32
    ) {
33
        $process->start($loop, $interval);
34
35
        return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])->
36
            then(function () use ($process, $options) {
37
                $messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [
0 ignored issues
show
Documentation introduced by
$process->stdin is of type object<React\Stream\WritableResourceStream>, but the function expects a object<React\Stream\Stream>.

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...
Documentation introduced by
$process->stdout is of type object<React\Stream\ReadableResourceStream>, but the function expects a object<React\Stream\Stream>.

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...
Documentation introduced by
$process->stderr is of type object<React\Stream\ReadableResourceStream>, but the function expects a object<React\Stream\Stream>.

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...
38
                    'read_err' => 'stderr',
39
                    'read' => 'stdout',
40
                    'write' => 'stdin',
41
                    'callForward' => function ($name, $arguments) use ($process) {
42
                        return call_user_func_array([$process, $name], $arguments);
43
                    },
44
                ] + $options);
45
46
                Util::forwardEvents($process, $messenger, [
47
                    'exit',
48
                ]);
49
50
                return \React\Promise\resolve($messenger);
51
            })
52
        ;
53
    }
54
55
    /**
56
     * @param  LoopInterface $loop
57
     * @param  array         $options
58
     * @param  null          $termiteCallable
59
     * @return Messenger
60
     */
61 1
    public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null)
62
    {
63 1
        $stdin  = new Stream(STDIN, $loop);
64
        $stdout = new Stream(STDOUT, $loop);
65
        $stderr = new Stream(STDERR, $loop);
66
67
        $messenger = new Messenger($stdin, $stdout, $stderr, [
68
            'read' => 'stdin',
69
            'write_err' => 'stderr',
70
            'write' => 'stdout',
71
        ] + $options);
72
73
        $closeNStop = function () use ($loop) {
74
            $loop->stop();
75
        };
76
        $stdin->on('close', $closeNStop);
77
        $stdout->on('close', $closeNStop);
78
        $stderr->on('close', $closeNStop);
79
80
        if ($termiteCallable === null) {
81
            $termiteCallable = function () use ($loop) {
82
                $loop->addTimer(
83
                    self::TERMINATE_TIMEOUT,
84
                    [
85
                        $loop,
86
                        'stop',
87
                    ]
88
                );
89
            };
90
        }
91
92
        $messenger->registerRpc(
93
            Messenger::TERMINATE_RPC,
94
            function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
95
                $messenger->emit('terminate', [
96
                    $messenger,
97
                ]);
98
                $termiteCallable($payload, $messenger);
99
100
                return new FulfilledPromise([]);
101
            }
102
        );
103
104
        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
    public static function parentFromClass(
116
        $className,
117
        LoopInterface $loop,
118
        array $options = [],
119
        $interval = self::INTERVAL
120
    ) {
121
        if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
122
            throw new \Exception('Given class doesn\'t implement ChildInterface');
123
        }
124
125
        $template = '%s';
126
        if (isset($options['cmdTemplate'])) {
127
            $template = $options['cmdTemplate'];
128
            unset($options['cmdTemplate']);
129
        }
130
131
        $phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : ''));
132
        $childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php');
133
        $command = $phpBinary . ' ' . $childProcessPath;
134
        $process = new Process(
135
            sprintf(
136
                $template,
137
                $command . ' ' . ArgvEncoder::encode($options)
138
            )
139
        );
140
141
        return static::parent(
142
            $process,
143
            $loop,
144
            $options,
145
            $interval
146
        )->then(function (Messenger $messenger) use ($className) {
147
            return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
148
                'className' => $className,
149
            ]))->then(function () use ($messenger) {
150
                return \React\Promise\resolve($messenger);
151
            });
152
        });
153
    }
154
}
155