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.

RequestHandler::handle()   C
last analyzed

Complexity

Conditions 11
Paths 72

Size

Total Lines 51

Duplication

Lines 24
Ratio 47.06 %

Code Coverage

Tests 13
CRAP Score 52.0952

Importance

Changes 0
Metric Value
cc 11
nc 72
nop 4
dl 24
loc 51
ccs 13
cts 43
cp 0.3023
crap 52.0952
rs 6.9224
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
3
namespace PhpBoot\Controller;
4
5
use PhpBoot\Application;
6
use PhpBoot\Metas\ParamMeta;
7
use PhpBoot\Utils\ArrayAdaptor;
8
use PhpBoot\Validator\Validator;
9
use Symfony\Component\HttpFoundation\ParameterBag;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13
class RequestHandler
14
{
15
    /**
16
     * @param ParamMeta[] $paramMates
17
     */
18 4
    public function __construct(array $paramMates=[]){
19 4
        $this->paramMetas = $paramMates;
20 4
    }
21
22
    /**
23
     * @param Application $app
24
     * @param Request $request
25
     * @param array $params
26
     * @param array $reference
27
     * @return void
28
     */
29 1
    public function handle(Application $app, Request $request, array &$params, array &$reference){
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
30
31 1
        $vld = new Validator();
32 1
        $req = ['request'=>$request];
33 1
        $requestArray = new ArrayAdaptor($req);
34 1
        $inputs = [];
35 1 View Code Duplication
        foreach ($this->paramMetas as $k=>$meta){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            if($meta->isPassedByReference){
37
                // param PassedByReference is used to output
38
                continue;
39
            }
40
            $source = \JmesPath\search($meta->source, $requestArray);
41
            if ($source !== null){
42
                $source = ArrayAdaptor::strip($source);
43
                if($source instanceof ParameterBag){
44
                    $source = $source->all();
45
                }
46
                if($meta->container){
47
                    $inputs[$meta->name] = $meta->container->make($source);
48
                }else{
49
                    $inputs[$meta->name] = $source;
50
                }
51
                if($meta->validation){
52
                    $vld->rule($meta->validation, $meta->name);
53
                }
54
            }else{
55
                $meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("the parameter \"{$meta->source}\" is missing"));
56
                $inputs[$meta->name] = $meta->default;
57
            }
58 1
        }
59 1
        $vld = $vld->withData($inputs);
60 1
        $vld->validate() or \PhpBoot\abort(
61
            new \InvalidArgumentException(
62
                json_encode(
63
                    $vld->errors(),
64
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
65
                )
66
            )
67
        );
68
69 1
        $pos = 0;
70 1
        foreach ($this->paramMetas as $meta){
71
            if($meta->isPassedByReference){
72
                $params[$pos] = &$reference[$meta->name];
73
            }else{
74
                $params[$pos] = $inputs[$meta->name];
75
            }
76
            $pos++;
77
78 1
        }
79 1
    }
80
81
    public function getParamNames(){
82
        return array_map(function($meta){return $meta->name;}, $this->paramMetas);
83
    }
84
85
    /**
86
     * 获取参数列表
87
     * @return ParamMeta[]
88
     */
89 5
    public function getParamMetas(){
90 5
        return $this->paramMetas;
91
    }
92
93
    /**
94
     * 获取指定参数信息
95
     * @param $name
96
     * @return ParamMeta|null
97
     */
98 3
    public function getParamMeta($name){
99 3
        foreach ($this->paramMetas as $meta){
100 3
            if($meta->name == $name){
101 3
                return $meta;
102
            }
103 3
        }
104
        return null;
105
    }
106
107
    /**
108
     * @param \PhpBoot\Metas\ParamMeta[] $paramMetas
109
     */
110 4
    public function setParamMetas($paramMetas)
111
    {
112 4
        $this->paramMetas = $paramMetas;
113 4
    }
114
    /**
115
     * @var ParamMeta[]
116
     */
117
    private $paramMetas = [];
118
}