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 (#10)
by Matthew
05:33
created

Factory::parentFromClass()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0155

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 22
cts 25
cp 0.88
rs 8.8571
cc 3
eloc 26
nc 3
nop 4
crap 3.0155
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 1
        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 2
                ]);
49
50 2
                return \React\Promise\resolve($messenger);
51 2
            })
52 2
        ;
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 2
            'read' => 'stdin',
69 2
            'write_err' => 'stderr',
70 2
            '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,
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...
84
                    [
85 1
                        $loop,
86 1
                        'stop',
87
                    ]
88 1
                );
89 2
            };
90 2
        }
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 1
                ]);
98 1
                $termiteCallable($payload, $messenger);
99 1
                return new FulfilledPromise([]);
100
            }
101 2
        );
102
103 2
        return $messenger;
104
    }
105
106
    /**
107
     * @param string $className
108
     * @param LoopInterface $loop
109
     * @param array $options
110
     * @param float $interval
111
     * @return \React\Promise\PromiseInterface
112
     * @throws \Exception
113
     */
114 2
    public static function parentFromClass(
115
        $className,
116
        LoopInterface $loop,
117
        array $options = [],
118
        $interval = self::INTERVAL
119
    ) {
120 2
        if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
121 1
            throw new \Exception('Given class doesn\'t implement ChildInterface');
122
        }
123
124 1
        $template = '%s';
125 1
        if (isset($options['cmdTemplate'])) {
126
            $template = $options['cmdTemplate'];
127
            unset($options['cmdTemplate']);
128
        }
129
130 1
        $command = PHP_BINARY . ' ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
131 1
        $process = new Process(
132 1
            sprintf(
133 1
                $template,
134 1
                $command . ' ' . ArgvEncoder::encode($options)
135 1
            )
136 1
        );
137
138 1
        return static::parent(
139 1
            $process,
140 1
            $loop,
141 1
            $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