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

Factory::parentFromClass()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4.0081

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
ccs 23
cts 25
cp 0.92
rs 8.5806
cc 4
eloc 28
nc 3
nop 4
crap 4.0081
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
    /**
22
     * @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...
23
     * @param  LoopInterface                       $loop
24
     * @param  array                               $options
25
     * @param  mixed                               $class
26
     * @return Promise\PromiseInterface<Overwatch>
0 ignored issues
show
Documentation introduced by
The doc-type Promise\PromiseInterface<Overwatch> 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...
27
     */
28 2
    public static function parent(
29
        $class,
30
        LoopInterface $loop,
31
        array $options = []
32
    ) {
33 2
        if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
34 1
            throw new \Exception('Given class doesn\'t implement ChildInterface');
35
        }
36
37
        return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) {
38 1
            $server = new Server('127.0.0.1:0', $loop);
39 1
            $options['address'] = $server->getAddress();
40
41 1
            $template = '%s';
42 1
            if (isset($options['cmdTemplate'])) {
43
                $template = $options['cmdTemplate'];
44
                unset($options['cmdTemplate']);
45
            }
46
47 1
            $phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : ''));
48 1
            $childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php');
49 1
            $argvString = \escapeshellarg(ArgvEncoder::encode($options));
50 1
            $command = $phpBinary . ' ' . $childProcessPath;
51 1
            $process = new Process(
52 1
                sprintf(
53 1
                    $template,
54 1
                    $command . ' ' . $argvString
55
                )
56
            );
57
58
            $server->on('connection', function (ConnectionInterface $connection) use ($server, $resolve, $reject, $class, $options) {
59 1
                $server->pause();
60 1
                $messenger = new Messenger($connection, $options);
61 1
                $resolve($messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
62 1
                    'className' => $class,
63
                ]))->then(function () use ($messenger) {
64 1
                    return Promise\resolve($messenger);
65 1
                }, $reject));
66 1
            });
67
            $server->on('error', function ($et) use ($reject) {
68
                $reject($et);
69 1
            });
70
71 1
            $process->start($loop);
72 1
        });
73
    }
74
75
    /**
76
     * @param  LoopInterface                       $loop
77
     * @param  array                               $options
78
     * @param  callable                            $termiteCallable
79
     * @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...
80
     */
81 1
    public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null)
82
    {
83
        return (new Connector($loop))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options) {
84 1
            return new Messenger($connection, $options);
85
        })->then(function (Messenger $messenger) use ($loop, $termiteCallable) {
86 1
            if ($termiteCallable === null) {
87
                $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...
88 1
                    $loop->addTimer(
89 1
                        self::TERMINATE_TIMEOUT,
90
                        [
91 1
                            $loop,
92 1
                            'stop',
93
                        ]
94
                    );
95 1
                };
96
            }
97
98 1
            $messenger->registerRpc(
99 1
                Messenger::TERMINATE_RPC,
100 1
                function (Payload $payload, Messenger $messenger) use ($termiteCallable) {
101 1
                    $messenger->emit('terminate', [
102 1
                        $messenger,
103
                    ]);
104 1
                    $termiteCallable($payload, $messenger);
105
106 1
                    return Promise\resolve([]);
107 1
                }
108
            );
109
110 1
            return $messenger;
111 1
        });
112
    }
113
}
114