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
Branch dev (718e6b)
by Jérémy
04:44 queued 02:58
created

Server::initEvents()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace CapMousse\ReactRestify;
4
5
use React\Http\Request as HttpRequest;
6
use React\Http\Response as HttpResponse;
7
8
class Server
9
{
10
    /**
11
     * Name of the server
12
     * @var string
13
     */
14
    public $name = "ReactRestify";
15
16
    /**
17
     * Version of the API
18
     * @var null
19
     */
20
    public $version = null;
21
22
    /**
23
     * @var \React\Restify\Router
24
     */
25
    private $router;
26
27
    /**
28
     * @var string
29
     */
30
    private $allowOrigin = "*";
31
32
    /**
33
     * @param null $name
34
     * @param null $version
35
     */
36
    public function __construct($name = null, $version = null)
37
    {
38
        if (null !== $name) {
39
            $this->name = $name;
40
        }
41
42
        if (null !== $version) {
43
            $this->version = $version;
44
        }
45
46
        $this->router = new Routing\Router();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CapMousse\ReactRestify\Routing\Router() of type object<CapMousse\ReactRestify\Routing\Router> is incompatible with the declared type object<React\Restify\Router> of property $router.

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...
47
48
        $this->initEvents();
49
    }
50
51
    /**
52
     * Parse request from user
53
     *
54
     * @param \React\Http\Request  $HttpRequest
0 ignored issues
show
Documentation introduced by
There is no parameter named $HttpRequest. Did you maybe mean $httpRequest?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
55
     * @param \React\Http\Response $HttpResponse
0 ignored issues
show
Documentation introduced by
There is no parameter named $HttpResponse. Did you maybe mean $httpResponse?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
56
     */
57
    public function __invoke(HttpRequest $httpRequest, HttpResponse $httpResponse)
58
    {
59
        $start = microtime(true);
0 ignored issues
show
Unused Code introduced by
$start is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
        $request = new Http\Request($httpRequest);
62
        $response = new Http\Response($httpResponse, $this->name, $this->version);
63
64
        try {
65
            $this->router->launch($request, $response);
66
        } catch (\Exception $e) {
67
            $response
68
                ->setStatus(500)
69
                ->write($e->getMessage())
70
                ->end();
71
        }
72
    }
73
74
    /**
75
     * Create a new group of route
76
     * @param String   $prefix   prefix of the routes
77
     * @param Callable $callback
78
     *
79
     * @return \CapMousse\ReactRestify\Routing\Routes
80
     */
81
    public function group($prefix, $callback)
82
    {
83
        return $this->router->addGroup($prefix, $callback);
84
    }
85
86
    /**
87
     * The the Access-Control-Allow-Origin header
88
     *
89
     * @param string $origin
90
     */
91
    public function setAccessControlAllowOrigin($origin)
92
    {
93
        $this->allowOrigin = $origin;
94
    }
95
96
    /**
97
     * Init default event catch
98
     *
99
     * @return void
100
     */
101
    private function initEvents()
102
    {
103
        $this->router->on('NotFound', function($request, $response) {
104
            $response
105
                ->setStatus(404)
106
                ->write('Not found')
107
                ->end();
108
        });
109
110
        $this->router->on('MethodNotAllowed', function($request, $response) {
111
            $response
112
                ->setStatus(405)
113
                ->write('Method Not Allowed')
114
                ->end();
115
        });
116
117
        $this->router->on('error', function ($request, $response, $error) {
118
            $response
119
                ->reset()
120
                ->setStatus(500)
121
                ->write($error)
122
                ->end();
123
        });
124
    }
125
126
    /**
127
     * Manual router event manager
128
     * @param String   $event
129
     * @param Callable $callback
130
     */
131
    public function on($event, $callback)
132
    {
133
        $this->router->removeAllListeners($event);
134
        $this->router->on($event, $callback);
135
    }
136
137
    /**
138
     * Create runner instance
139
     * @param  Int    $port
140
     * @param  String $host
141
     * @return Server
142
     */
143
    public function listen($port, $host = "127.0.0.1")
144
    {
145
        $runner = new Runner($this);
146
        $runner->listen($port, $host);
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $name      method to call
153
     * @param array  $arguments
154
     */
155
    public function __call($name, $arguments)
156
    {
157
        return $this->router->addRoute($name, $arguments[0], $arguments[1]);
158
    }
159
}
160