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:31
created

Factory::child()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 1
nop 3
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;
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
            $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
34
            self::startParent($process, $server, $loop, $options)->done($resolve, $reject);
35
        });
36
    }
37
38
    /**
39
     * @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
     * @param  LoopInterface                       $loop
41
     * @param  array                               $options
42
     * @param  mixed                               $class
43
     * @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
     */
45 2
    public static function parentFromClass(
46
        $class,
47
        LoopInterface $loop,
48
        array $options = []
49
    ) {
50 2
        if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
51 1
            throw new \Exception('Given class doesn\'t implement ChildInterface');
52
        }
53
54
        return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) {
55 1
            $server = new Server('127.0.0.1:0', $loop);
56 1
            $options['address'] = $server->getAddress();
57
58 1
            $template = '%s';
59 1
            if (isset($options['cmdTemplate'])) {
60
                $template = $options['cmdTemplate'];
61
                unset($options['cmdTemplate']);
62
            }
63
64 1
            $phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : ''));
65 1
            $childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php');
66 1
            $argvString = \escapeshellarg(ArgvEncoder::encode($options));
67 1
            $command = $phpBinary . ' ' . $childProcessPath;
68 1
            $process = new Process(
69 1
                sprintf(
70 1
                    $template,
71 1
                    $command . ' ' . $argvString
72
                )
73
            );
74
75
            self::startParent($process, $server, $loop, $options)->then(function (Messenger $messenger) use ($class) {
76 1
                return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
77 1
                    'className' => $class,
78
                ]))->then(function () use ($messenger) {
79 1
                    return Promise\resolve($messenger);
80 1
                });
81 1
            })->done($resolve, $reject);
82 1
        });
83
    }
84
85
    /**
86
     * @param  LoopInterface                       $loop
87
     * @param  array                               $options
88
     * @param  callable                            $termiteCallable
89
     * @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 1
    public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null)
92
    {
93
        return (new Connector($loop))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options) {
94 1
            return new Messenger($connection, $options);
95
        })->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 1
                        self::TERMINATE_TIMEOUT,
100
                        [
101 1
                            $loop,
102 1
                            'stop',
103
                        ]
104
                    );
105 1
                };
106
            }
107
108 1
            $messenger->registerRpc(
109 1
                Messenger::TERMINATE_RPC,
110
                function (Payload $payload, Messenger $messenger) use ($termiteCallable) {
111 1
                    $messenger->emit('terminate', [
112 1
                        $messenger,
113
                    ]);
114 1
                    $termiteCallable($payload, $messenger);
115
116 1
                    return Promise\resolve([]);
117 1
                }
118
            );
119
120 1
            return $messenger;
121 1
        });
122
    }
123
124 1
    private static function startParent(
125
        Process $process,
126
        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
                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 1
            $server->on('error', function ($et) use ($reject) {
140
                $reject($et);
141 1
            });
142
143 1
            $process->start($loop);
144 1
        });
145
    }
146
}
147