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.
Passed
Push — master ( 36cfe5...083d1a )
by cao
04:22
created

RequestHandler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 45.61%

Importance

Changes 0
Metric Value
dl 0
loc 106
ccs 26
cts 57
cp 0.4561
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getParamNames() 0 3 1
C handle() 0 51 11
A getParamMetas() 0 3 1
A getParamMeta() 0 8 3
A setParamMetas() 0 4 1
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
        foreach ($this->paramMetas as $k=>$meta){
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("param $source is required"));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
56
                $inputs[$meta->name] = $meta->default;
57
            }
58 1
        }
59 1
        $vld = $vld->withData($inputs);
60 1
        $vld->validate() or \PhpBoot\abort(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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
}