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
15:45 queued 05:53
created

Factory::startParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1.0019

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 4
dl 0
loc 22
ccs 14
cts 16
cp 0.875
crap 1.0019
rs 9.2
c 0
b 0
f 0
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;
8
use React\Socket\ConnectionInterface;
9
use React\Socket\Connector;
10
use React\Socket\Server;
11
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory;
12
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
13
14
final 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
    public static function parent(
22
        Process $process,
23
        LoopInterface $loop,
24
        array $options = []
25
    ) {
26
        return new Promise\Promise(function ($resolve, $reject) use ($process, $loop, $options) {
27 2
            $server = new Server('127.0.0.1:0', $loop);
28
            $argvString = \escapeshellarg(ArgvEncoder::encode([
29
                'address' => $server->getAddress(),
30
            ]));
31
32
            $process = new Process($process->getCommand() . ' ' . $argvString);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $process, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
33 2
34
            self::startParent($process, $server, $loop, $options)->done($resolve, $reject);
35 2
        });
36
    }
37 2
38 2
    /**
39 2
     * @param  string                              $process
0 ignored issues
show
Bug introduced by
There is no parameter named $process. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40 2
     * @param  LoopInterface                       $loop
41
     * @param  array                               $options
42 1
     * @param  mixed                               $class
43 2
     * @return Promise\PromiseInterface<Messenger>
0 ignored issues
show
Documentation introduced by
The doc-type Promise\PromiseInterface<Messenger> could not be parsed: Expected "|" or "end of type", but got "<" at position 24. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
44 2
     */
45
    public static function parentFromClass(
46 2
        $class,
47 2
        LoopInterface $loop,
48
        array $options = []
49
    ) {
50 2
        if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
51 2
            throw new \Exception('Given class doesn\'t implement ChildInterface');
52
        }
53
54
        return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) {
55
            $server = new Server('127.0.0.1:0', $loop);
56
            $options['address'] = $server->getAddress();
57
58
            $template = '%s';
59
            if (isset($options['cmdTemplate'])) {
60
                $template = $options['cmdTemplate'];
61 2
                unset($options['cmdTemplate']);
62
            }
63 2
64 2
            $phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : ''));
65 2
            $childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php');
66
            $argvString = \escapeshellarg(ArgvEncoder::encode($options));
67 2
            $command = $phpBinary . ' ' . $childProcessPath;
68
            $process = new Process(
69
                sprintf(
70
                    $template,
71 2
                    $command . ' ' . $argvString
72
                )
73
            );
74
75 2
            self::startParent($process, $server, $loop, $options)->then(function (Messenger $messenger) use ($class) {
76 2
                return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
77 2
                    'className' => $class,
78 2
                ]))->then(function () use ($messenger) {
79
                    return Promise\resolve($messenger);
80 2
                });
81
            })->done($resolve, $reject);
82 1
        });
83 1
    }
84
85 1
    /**
86 1
     * @param  LoopInterface                       $loop
87
     * @param  array                               $options
88
     * @param  callable                            $termiteCallable
89 2
     * @return Promise\PromiseInterface<Messenger>
0 ignored issues
show
Documentation introduced by
The doc-type Promise\PromiseInterface<Messenger> could not be parsed: Expected "|" or "end of type", but got "<" at position 24. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
90
     */
91
    public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null)
92 2
    {
93 2
        return (new Connector($loop))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options) {
94
            return new Messenger($connection, $options);
95 1
        })->then(function (Messenger $messenger) use ($loop, $termiteCallable) {
96 1
            if ($termiteCallable === null) {
97
                $termiteCallable = function () use ($loop) {
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $termiteCallable, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
98 1
                    $loop->addTimer(
99
                        self::TERMINATE_TIMEOUT,
100 1
                        [
101 2
                            $loop,
102
                            'stop',
103
                        ]
104 2
                    );
105
                };
106
            }
107
108
            $messenger->registerRpc(
109
                Messenger::TERMINATE_RPC,
110
                function (Payload $payload, Messenger $messenger) use ($termiteCallable) {
111
                    $messenger->emit('terminate', [
112
                        $messenger,
113
                    ]);
114
                    $termiteCallable($payload, $messenger);
115 2
116
                    return Promise\resolve([]);
117
                }
118
            );
119
120
            return $messenger;
121 2
        });
122 1
    }
123
124
    private static function startParent(
125 1
        Process $process,
126 1
        Server $server,
127
        LoopInterface $loop,
128
        array $options
129
    ) {
130
        return new Promise\Promise(function ($resolve, $reject) use ($process, $server, $loop, $options) {
131 1
            $server->on(
132 1
                'connection',
133 1
                function (ConnectionInterface $connection) use ($server, $resolve, $reject, $options) {
134 1
                    $server->close();
135 1
                    $messenger = new Messenger($connection, $options);
136 1
                    $resolve($messenger);
137 1
                }
138
            );
139
            $server->on('error', function ($et) use ($reject) {
140
                $reject($et);
141 1
            });
142 1
143 1
            $process->start($loop);
144 1
        });
145 1
    }
146
}
147