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.

RpcProxy::createRequest()   F
last analyzed

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 95

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 77
CRAP Score 24.0694

Importance

Changes 0
Metric Value
cc 24
nc 72576
nop 3
dl 0
loc 95
ccs 77
cts 81
cp 0.9506
crap 24.0694
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PhpBoot\RPC;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
6
use PhpBoot\Application;
7
use PhpBoot\Controller\ControllerContainer;
8
use PhpBoot\Controller\ControllerContainerBuilder;
9
use PhpBoot\Controller\ExceptionRenderer;
10
use PhpBoot\Controller\Route;
11
use PhpBoot\Utils\ArrayAdaptor;
12
use PhpBoot\Utils\ArrayHelper;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
class RpcProxy
17
{
18
    /**
19
     * PRC 代理
20
     *
21
     * 通过__call实现代理时, 无法传递引用参数, 默认处理方式是抛出异常。如果想忽略此参数, 可以把ignoreReferenceParam设置为 true。
22
     *
23
     * 如果需要使用引用参数, 可以继承RpcProxy, 并重对应方法。
24
     *
25
     * TODO 支持鉴权
26
     *
27
     * RpcProxy constructor.
28
     * @param Application $app
29
     * @param ControllerContainerBuilder $builder
30
     * @param Client $http
31
     * @param string $interface
32
     * @param string $prefix
33
     */
34 4
    public function __construct(Application $app,
35
                                ControllerContainerBuilder $builder,
36
                                Client $http,
37
                                $interface,
38
                                $prefix='/')
39
    {
40 4
        $this->container = $builder->build($interface);
41 4
        $this->http = $http;
42 4
        $this->uriPrefix = $prefix;
43 4
        $this->app = $app;
44 4
    }
45
46
    public function __call($method, $args)
47
    {
48
        $route = $this->container->getRoute($method)
49
            or \PhpBoot\abort($this->container->getClassName()." $method is not a valid http interface");
50
51
        $request = $this->createRequest($method, $route, $args);
0 ignored issues
show
Security Bug introduced by
It seems like $route defined by $this->container->getRoute($method) on line 48 can also be of type false; however, PhpBoot\RPC\RpcProxy::createRequest() does only seem to accept object<PhpBoot\Controller\Route>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
52
53
        if(MultiRpc::isRunning()){
54
            $op = $this->http->sendAsync($request);
55
            $res = MultiRpc::wait($op);
0 ignored issues
show
Compatibility introduced by
$op of type object<GuzzleHttp\Promise\PromiseInterface> is not a sub-type of object<GuzzleHttp\Promise\Promise>. It seems like you assume a concrete implementation of the interface GuzzleHttp\Promise\PromiseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
            return $this->mapResponse($method, $route, $res, $args);
0 ignored issues
show
Security Bug introduced by
It seems like $route defined by $this->container->getRoute($method) on line 48 can also be of type false; however, PhpBoot\RPC\RpcProxy::mapResponse() does only seem to accept object<PhpBoot\Controller\Route>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
57
        }else{
58
            $res = $this->http->send($request);
59
            return $this->mapResponse($method, $route, $res, $args);
0 ignored issues
show
Security Bug introduced by
It seems like $route defined by $this->container->getRoute($method) on line 48 can also be of type false; however, PhpBoot\RPC\RpcProxy::mapResponse() does only seem to accept object<PhpBoot\Controller\Route>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
60
        }
61
    }
62
63
    /**
64
     * @param $actionName
65
     * @param Route $route
66
     * @param array $args
67
     * @return RequestInterface
68
     */
69 3
    public function createRequest($actionName, Route $route, array $args)
70
    {
71 3
        $params = $route->getRequestHandler()->getParamMetas();
72
        //TODO 支持 query、content、path以外的其他参数, 如cookie,path等
73 3
        $request = [];
74 3
        foreach ($params as $pos=>$param){
75 3
            if(!array_key_exists($pos, $args) && $param->isOptional){
76 3
                $args[$pos] = $param->default;
77 3
            }
78 3
            array_key_exists($pos, $args) or \PhpBoot\abort(
79
                $this->container->getClassName()." $actionName missing param {$param->name}");
80
81 3
            if(!$param->isPassedByReference){
82 3
                ArrayHelper::set($request, $param->source, $args[$pos]);
83 3
            }
84 3
        }
85
86 3
        if(isset($request['request'])){
87 3
            $request = $request['request'];
88 3
        }
89 3
        $uri = $route->getUri();
90 3
        foreach($route->getPathParams() as $path){
91 1
            if(isset($request[$path])){
92 1
                $uri = str_replace('{'.$path.'}', urlencode($request[$path]) , $uri);
93 1
                unset($request[$path]);
94 1
            }
95 3
        }
96 3
        $httpMethod = $route->getMethod();
97
98 3
        $query = [];
99 3
        $body = null;
100 3
        $headers = ['Content-Type'=>'application/json'];
101
102 3
        if(isset($request['query'])){
103 1
            $query += $request['query'];
104 1
        }
105 3
        unset($request['query']);
106
107 3
        if(isset($request['headers'])){
108 1
            $headers += $request['headers'];
109 1
        }
110 3
        unset($request['headers']);
111
112 3
        if(isset($request['cookies'])){
113 1
            $cookies = [];
114 1
            foreach ($request['cookies'] as $k=>$v){
115 1
                $cookies[] = "$k=$v";
116 1
            }
117 1
            $headers['Cookie'] = implode('; ', $cookies);
118 1
        }
119 3
        unset($request['cookies']);
120
121 3
        if(isset($request['request'])){
122 1
            if($body === null){
123 1
                $body = [];
124 1
            }
125 1
            $body += $request['request'];
126 1
        }
127 3
        unset($request['request']);
128
129 3
        if(isset($request['files'])){
130
            \PhpBoot\abort(new \UnexpectedValueException("sending request with files is not support"));
131
        }
132 3
        if(in_array($httpMethod, ['GET', 'OPTION'])){
133 1
            foreach ($request as $k => $v){
134 1
                if(!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])){
135 1
                    $query[$k] = $v;
136 1
                }
137 1
            }
138 1
        }else{
139 2
            foreach ($request as $k => $v){
140 2
                if(!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])){
141 2
                    if($body === null){
142 1
                        $body = [];
143 1
                    }
144 2
                    $body[$k] = $v;
145 2
                }
146 2
            }
147
        }
148 3
        if($body !== null){
149 2
            $body = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
150 2
        }
151 3
        $uri = $this->uriPrefix.ltrim($uri, '/');
152 3
        if(strpos($uri,'?') === false){
153 3
            $uri = $uri.'?'.http_build_query($query);
154 3
        }else{
155
            $uri = $uri.'&'.http_build_query($query);
156
        }
157 3
        return new \GuzzleHttp\Psr7\Request(
158 3
            $httpMethod,
159 3
            $uri,
160 3
            $headers,
161
            $body
162 3
        );
163
    }
164
165
166 1
    public function mapResponse($actionName, Route $route, ResponseInterface $response, $requestArg=[])
0 ignored issues
show
Unused Code introduced by
The parameter $actionName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168 1
        $response = \Symfony\Component\HttpFoundation\Response::create(
169 1
            (string)$response->getBody(),
170 1
            $response->getStatusCode(),
171 1
            $response->getHeaders()
172 1
            );
173 1
        $namedArgs = [];
174
175 1
        foreach ($route->getRequestHandler()->getParamMetas() as $pos=>$param){
176
177 1
            if($param->isPassedByReference){
178 1
                $namedArgs[$param->name] = &$requestArg[$pos];
179 1
            }
180 1
        }
181
182
183 1
        $content = json_decode((string)$response->getContent(), true);
184
185
        //TODO 远端接口没有抛出异常,但设置了 status( status 不是200)时如何处理
186 1
        $handler = $route->getResponseHandler();
187
188 1
        if($response->getStatusCode() >= 200 && $response->getStatusCode() <300){
189
190
191 1
            $returns = $handler->getMappings();
192 1
            $buffer = [];
193 1
            ArrayHelper::set($buffer, 'response.content', $content);
194 1
            ArrayHelper::set($buffer, 'response.headers', new ArrayAdaptor($response->headers));
195 1
            ArrayHelper::set($buffer, 'response.cookies', $response->headers->getCookies());
196
197
            //TODO 支持 cookie
198
            //ArrayHelper::set($response, 'response.cookies');
199
200
201
            $mapping = [
202
                'params'=>$namedArgs
203 1
            ];
204
205 1
            foreach ($returns as $map=>$return){
206
207 1
                $data = \JmesPath\search($map, $buffer);
208 1
                if(!$return->container){
209
                    continue;
210
                }
211 1
                $data = $return->container->make($data, false);
212 1
                ArrayHelper::set($mapping, $return->source, $data);
213 1
            }
214
215 1
        }else{
216
217
            //TODO 如果多个 异常对应同一个 statusCode 怎么处理
218
            $exceptions = $route->getExceptionHandler()->getExceptions();
219
220
            $errName = null;
0 ignored issues
show
Unused Code introduced by
$errName 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...
221
            foreach ($exceptions as $err){
222
223
                $renderer = $this->app->get(ExceptionRenderer::class);
224
                $exec = $renderer->render(
225
                    $this->app->make($err, ['message'=>(string)$response->getContent()])
226
                );
227
228
                if( $exec->getStatusCode() == $response->getStatusCode()){
229
                    throw $exec;
230
                }
231
            }
232
            throw new \RuntimeException((string)$response->getContent());
233
234
        };
235
236 1
        if(isset($mapping['return'])){
237 1
            return $mapping['return'];
238
        };
239
    }
240
    /**
241
     * @var ControllerContainer
242
     */
243
    protected $container;
244
245
    /**
246
     * @var ClientInterface
247
     */
248
    protected $http;
249
250
    /**
251
     * @var string
252
     */
253
    protected $uriPrefix;
254
255
    /**
256
     * @var Application
257
     */
258
    protected $app;
259
}
260