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 ( 78c5cf...93608b )
by Cees-Jan
07:34 queued 05:28
created

Factory::parentFromClass()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.9714
cc 2
eloc 18
nc 2
nop 4
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
        $messenger = new Messenger(new Stream(STDIN, $loop), new Stream(STDOUT, $loop), new Stream(STDERR, $loop), [
65
            'read' => 'stdin',
66
            'write_err' => 'stderr',
67
            'write' => 'stdout',
68 2
        ] + $options);
69
70 2
        if ($termiteCallable === null) {
71
            $termiteCallable = function () use ($loop) {
72 1
                $loop->addTimer(
73 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...
74
                    [
75 1
                        $loop,
76 1
                        'stop',
77
                    ]
78
                );
79 2
            };
80
        }
81
82 2
        $messenger->registerRpc(
83 2
            Messenger::TERMINATE_RPC,
84
            function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
85 1
                $messenger->emit('terminate', [
86 1
                    $messenger,
87
                ]);
88 1
                $termiteCallable($payload, $messenger);
89 1
                return new FulfilledPromise();
90 2
            }
91
        );
92
93 2
        return $messenger;
94
    }
95
96
    /**
97
     * @param string $className
98
     * @param LoopInterface $loop
99
     * @param array $options
100
     * @param float $interval
101
     * @return \React\Promise\PromiseInterface
102
     * @throws \Exception
103
     */
104 2
    public static function parentFromClass(
105
        $className,
106
        LoopInterface $loop,
107
        array $options = [],
108
        $interval = self::INTERVAL
109
    ) {
110 2
        if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
111 1
            throw new \Exception('Given class doesn\'t implement ChildInterface');
112
        }
113
114 1
        $process = new Process(self::getProcessForCurrentOS());
115 1
        return static::parent(
116
            $process,
117
            $loop,
118
            $options,
119
            $interval
120
        )->then(function (Messenger $messenger) use ($className) {
121 1
            return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
122 1
                'className' => $className,
123 1
            ]))->then(function () use ($messenger) {
124 1
                return \React\Promise\resolve($messenger);
125 1
            });
126 1
        });
127
    }
128
129
    /**
130
     * @param Detector|null $detector
131
     * @return string
132
     * @throws \Exception
133
     */
134 5
    public static function getProcessForCurrentOS(Detector $detector = null)
135
    {
136 5
        if ($detector === null) {
137 2
            $detector = new Detector();
138
        }
139
140 5
        if ($detector->isUnixLike()) {
141 3
            return 'php ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php';
142
        }
143
144 2
        if ($detector->isWindowsLike()) {
145 1
            return 'php.exe ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php';
146
        }
147
148 1
        throw new \Exception('Unknown OS family');
149
    }
150
}
151