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 ( abbaf5...bc538d )
by cao
03:04
created

RouteAnnotationHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 80
cts 82
cp 0.9756
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 12

1 Method

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 99 14
1
<?php
2
3
namespace PhpBoot\Controller\Annotations;
4
5
use FastRoute\RouteParser\Std;
6
use PhpBoot\Controller\ControllerContainer;
7
use PhpBoot\Controller\ExceptionHandler;
8
use PhpBoot\Entity\ContainerFactory;
9
use PhpBoot\Entity\EntityContainerBuilder;
10
use PhpBoot\Metas\ReturnMeta;
11
use PhpBoot\Annotation\AnnotationBlock;
12
use PhpBoot\Annotation\AnnotationTag;
13
use PhpBoot\Controller\RequestHandler;
14
use PhpBoot\Controller\ResponseHandler;
15
use PhpBoot\Controller\Route;
16
use PhpBoot\Entity\MixedTypeContainer;
17
use PhpBoot\Exceptions\AnnotationSyntaxException;
18
use PhpBoot\Metas\ParamMeta;
19
use PhpBoot\Utils\AnnotationParams;
20
21
class RouteAnnotationHandler
22
{
23
    /**
24
     * @param ControllerContainer $container
25
     * @param AnnotationBlock|AnnotationTag $ann
26
     * @param EntityContainerBuilder $entityBuilder
27
     */
28 4
    public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder)
29
    {
30 4
        $params = new AnnotationParams($ann->description, 3);
31 4
        $params->count()>=2 or \PhpBoot\abort(new AnnotationSyntaxException("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::{$ann->parent->name} require 2 params, {$params->count()} given"));
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...
32
33
        //TODO 错误判断: METHOD不支持, path不规范等
34 4
        $httpMethod = strtoupper($params->getParam(0));
35 4
        $target = $ann->parent->name;
36 4
        in_array($httpMethod, [
37 4
            'GET',
38 4
            'POST',
39 4
            'PUT',
40 4
            'HEAD',
41 4
            'PATCH',
42 4
            'OPTIONS',
43
            'DELETE'
44 4
        ]) or \PhpBoot\abort(new AnnotationSyntaxException("unknown method http $httpMethod in {$container->getClassName()}::$target"));
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...
45
        //获取方法参数信息
46 4
        $rfl =  new \ReflectionClass($container->getClassName());
47 4
        $method = $rfl->getMethod($target);
48 4
        $methodParams = $method->getParameters();
49
50 4
        $uri = $params->getParam(1);
51 4
        $uri = rtrim($container->getPathPrefix(), '/').'/'.ltrim($uri, '/');
52 4
        $requestHandler = new RequestHandler();
53 4
        $responseHandler = new ResponseHandler();
54 4
        $exceptionHandler = new ExceptionHandler();
55
56 4
        $route = new Route(
57 4
            $httpMethod,
58 4
            $uri,
59 4
            $requestHandler,
60 4
            $responseHandler,
61 4
            $exceptionHandler,
62 4
            [],
63 4
            $ann->parent->summary,
64 4
            $ann->parent->description
65 4
        );
66
67
        //从路由中获取变量, 用于判断参数是来自路由还是请求
68 4
        $routeParser = new Std();
69 4
        $uri = $params->getParam(1);
70 4
        $info = $routeParser->parse($uri); //0.4和1.0返回值不同, 不兼容
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71 4
        if(isset($info[0])){
72 4
            foreach ($info[0] as $i){
73 4
                if(is_array($i)) {
74 2
                    $route->addPathParam($i[0]);
75 2
                }
76 4
            }
77 4
        }
78
79 4
        $hasRefParam = false;
80
        //设置参数列表
81 4
        $paramsMeta = [];
82 4
        foreach ($methodParams as $param){
83 3
            $paramName = $param->getName();
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
84 3
            $source = "request.$paramName";
85 3
            if($route->hasPathParam($paramName)){ //参数来自路由
86 2
                $source = "request.$paramName";
87 3
            }elseif($httpMethod == 'GET'){
88 3
                $source = "request.$paramName"; //GET请求显示指定来自query string
89 3
            }
90 3
            $paramClass = $param->getClass();
91 3
            if($paramClass){
92
                $paramClass = $paramClass->getName();
93
            }
94 3
            $entityContainer = ContainerFactory::create($entityBuilder, $paramClass);
95 3
            $meta = new ParamMeta($paramName,
96 3
                $source,
97 3
                $paramClass?:'mixed',
98 3
                $param->isOptional(),
99 3
                $param->isOptional()?$param->getDefaultValue():null,
100 3
                $param->isPassedByReference(),
101 3
                null,
102 3
                '',
103
                $entityContainer
104 3
            );
105 3
            $paramsMeta[] = $meta;
106 3
            if($meta->isPassedByReference){
107 3
                $hasRefParam = true;
108 3
                $responseHandler->setMapping('response.content.'.$meta->name, new ReturnMeta(
109 3
                    'params.'.$meta->name,
110 3
                    $meta->type, $meta->description,
111 3
                    ContainerFactory::create($entityBuilder, $meta->type)
112 3
                ));
113 3
            }
114 4
        }
115
116 4
        $requestHandler->setParamMetas($paramsMeta);
117 4
        if(!$hasRefParam){
118 2
            $responseHandler->setMapping('response.content', new ReturnMeta('return','mixed','', new MixedTypeContainer()));
119 2
        }else{
120
            //当存在引用参数作为输出时, 默认将 return 数据绑定的到 data 下, 以防止和引用参数作为输出重叠
121 3
            $responseHandler->setMapping('response.content.data', new ReturnMeta('return','mixed','', new MixedTypeContainer()));
122
        }
123
124
125 4
        $container->addRoute($target, $route);
126
    }
127
}