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.

RouteAnnotationHandler::__invoke()   D
last analyzed

Complexity

Conditions 14
Paths 208

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 80
CRAP Score 14.0028

Importance

Changes 0
Metric Value
cc 14
nc 208
nop 3
dl 0
loc 99
ccs 80
cts 82
cp 0.9756
crap 14.0028
rs 4.2884
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\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"));
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"));
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->getUriPrefix(), '/').'/'.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返回值不同, 不兼容
71 4
        if(isset($info[0])){
72 4
            foreach ($info[0] as $i){
73 4
                if(is_array($i)) {
74 3
                    $route->addPathParam($i[0]);
75 3
                }
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 3
                $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();
0 ignored issues
show
Bug introduced by
Consider using $paramClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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 3
            $responseHandler->setMapping('response.content', new ReturnMeta('return','mixed','', new MixedTypeContainer()));
119 3
        }else{
120
            //当存在引用参数作为输出时, 默认将 return 数据绑定的到 data 下, 以防止和引用参数作为输出重叠
121 3
            $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return','mixed','', new MixedTypeContainer()));
122
        }
123
124
125 4
        $container->addRoute($target, $route);
126 4
    }
127
128
    public $returnTarget='response.content.data';
129
}