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 ( b77b49...6ef5d2 )
by Cees-Jan
02:41
created

Factory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 86.84%

Importance

Changes 16
Bugs 1 Features 2
Metric Value
wmc 10
c 16
b 1
f 2
lcom 0
cbo 11
dl 0
loc 149
ccs 33
cts 38
cp 0.8684
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B parent() 0 27 1
B child() 0 33 2
B parentFromClass() 0 36 3
A getProcessForCurrentOS() 0 16 4
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 1
        array $options = [],
32 1
        $interval = self::INTERVAL
33
    ) {
34
        $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
                        return call_user_func_array([$process, $name], $arguments);
44 2
                    },
45
                ] + $options);
46
47
                Util::forwardEvents($process, $messenger, [
48
                    'exit',
49
                ]);
50
51
                return \React\Promise\resolve($messenger);
52 2
            })
53 2
        ;
54 1
    }
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
        $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
        ] + $options);
69
70 2
        if ($termiteCallable === null) {
71
            $termiteCallable = function () use ($loop) {
72
                $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
                        $loop,
76 1
                        'stop',
77
                    ]
78
                );
79 1
            };
80
        }
81
82
        $messenger->registerRpc(
83 2
            Messenger::TERMINATE_RPC,
84
            function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
85 1
                $messenger->emit('terminate', [
86
                    $messenger,
87
                ]);
88
                $termiteCallable($payload, $messenger);
89
                return new FulfilledPromise();
90 1
            }
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 2
        array $options = [],
108 2
        $interval = self::INTERVAL
109
    ) {
110
        if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
111
            throw new \Exception('Given class doesn\'t implement ChildInterface');
112
        }
113
114 1
        $template = '%s';
115 1
        if (isset($options['cmdTemplate'])) {
116
            $template = $options['cmdTemplate'];
117 1
            unset($options['cmdTemplate']);
118
        }
119
120
        $process = new Process(
121
            sprintf(
122
                $template,
123
                self::getProcessForCurrentOS() . ' ' . ArgvEncoder::encode($options)
124
            )
125
        );
126
127
        return static::parent(
128
            $process,
129
            $loop,
130
            $options,
131
            $interval
132
        )->then(function (Messenger $messenger) use ($className) {
133 1
            return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
134
                'className' => $className,
135
            ]))->then(function () use ($messenger) {
136
                return \React\Promise\resolve($messenger);
137
            });
138
        });
139
    }
140
141
    /**
142
     * @param Detector|null $detector
143
     * @return string
144
     * @throws \Exception
145
     */
146 5
    public static function getProcessForCurrentOS(Detector $detector = null)
147
    {
148 5
        if ($detector === null) {
149
            $detector = new Detector();
150
        }
151
152
        if ($detector->isUnixLike()) {
153 3
            return 'php ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php';
154
        }
155
156
        if ($detector->isWindowsLike()) {
157 1
            return 'php.exe ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php';
158
        }
159
160
        throw new \Exception('Unknown OS family');
161 5
    }
162
}
163