Completed
Push — master ( edcf20...3ec6c0 )
by Marcel
03:05 queued 01:22
created

src/Server/WebSocketServerFactory.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server;
4
5
use Ratchet\Http\Router;
0 ignored issues
show
This use statement conflicts with another class in this namespace, BeyondCode\LaravelWebSockets\Server\Router.

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\Socket\Server;
7
use Ratchet\Server\IoServer;
8
use React\Socket\SecureServer;
9
use React\EventLoop\LoopInterface;
10
use React\EventLoop\Factory as LoopFactory;
11
use Symfony\Component\Routing\RequestContext;
12
use Symfony\Component\Routing\RouteCollection;
13
use Symfony\Component\Routing\Matcher\UrlMatcher;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
16
17
class WebSocketServerFactory
18
{
19
    /** @var string */
20
    protected $host = '127.0.0.1';
21
22
    /** @var int */
23
    protected $port = 8080;
24
25
    /** @var \React\EventLoop\LoopInterface */
26
    protected $loop;
27
28
    /** @var \Symfony\Component\Routing\RouteCollection */
29
    protected $routes;
30
31
    /** @var Symfony\Component\Console\Output\OutputInterface */
32
    protected $consoleOutput;
33
34
    public function __construct()
35
    {
36
        $this->loop = LoopFactory::create();
37
    }
38
39
    public function useRoutes(RouteCollection $routes)
40
    {
41
        $this->routes = $routes;
42
43
        return $this;
44
    }
45
46
    public function setHost(string $host)
47
    {
48
        $this->host = $host;
49
50
        return $this;
51
    }
52
53
    public function setPort(string $port)
54
    {
55
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
The property $port was declared of type integer, but $port is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
56
57
        return $this;
58
    }
59
60
    public function setLoop(LoopInterface $loop)
61
    {
62
        $this->loop = $loop;
63
64
        return $this;
65
    }
66
67
    public function setConsoleOutput(OutputInterface $consoleOutput)
68
    {
69
        $this->consoleOutput = $consoleOutput;
0 ignored issues
show
Documentation Bug introduced by
It seems like $consoleOutput of type object<Symfony\Component...Output\OutputInterface> is incompatible with the declared type object<BeyondCode\Larave...Output\OutputInterface> of property $consoleOutput.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
71
        return $this;
72
    }
73
74
    public function createServer(): IoServer
75
    {
76
        $socket = new Server("{$this->host}:{$this->port}", $this->loop);
77
78
        if (config('websockets.ssl.local_cert')) {
79
            $socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
80
        }
81
82
        $urlMatcher = new UrlMatcher($this->routes, new RequestContext);
83
84
        $router = new Router($urlMatcher);
85
86
        $app = new OriginCheck($router, config('websockets.allowed_origins', []));
87
88
        $httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024);
89
90
        if (HttpLogger::isEnabled()) {
91
            $httpServer = HttpLogger::decorate($httpServer);
92
        }
93
94
        return new IoServer($httpServer, $socket, $this->loop);
95
    }
96
}
97