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.

Command::bindInput()   B
last analyzed

Complexity

Conditions 11
Paths 72

Size

Total Lines 49

Duplication

Lines 24
Ratio 48.98 %

Code Coverage

Tests 26
CRAP Score 18.4742

Importance

Changes 0
Metric Value
cc 11
nc 72
nop 3
dl 24
loc 49
ccs 26
cts 43
cp 0.6047
crap 18.4742
rs 7.3166
c 0
b 0
f 0

How to fix   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
 * Created by PhpStorm.
4
 * User: caoyangmin
5
 * Date: 2018/6/14
6
 * Time: 下午6:03
7
 */
8
9
namespace PhpBoot\Console;
10
11
12
use DI\FactoryInterface;
13
use PhpBoot\Entity\ArrayContainer;
14
use PhpBoot\Entity\EntityContainer;
15
use PhpBoot\Metas\ParamMeta;
16
use PhpBoot\Utils\ArrayAdaptor;
17
use PhpBoot\Validator\Validator;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\HttpFoundation\ParameterBag;
22
23
class Command extends \Symfony\Component\Console\Command\Command
24
{
25
26
    /**
27
     * @var ParamMeta[]
28
     */
29
    private $paramMetas;
30
31
    /**
32
     * @var string
33
     */
34
    private $actionName;
35
36 1
    public function __construct($actionName, $name)
37
    {
38 1
        parent::__construct($name?:$actionName);
39 1
        $this->actionName = $actionName;
40 1
    }
41
    /**
42
     * @param ParamMeta[] $paramMetas
43
     */
44 1
    public function setParamMetas($paramMetas)
45
    {
46 1
        $this->paramMetas = $paramMetas;
47 1
    }
48
49 1
    public function postCreate(ConsoleContainer $container)
50
    {
51 1
        if($container->getModuleName()){
52 1
            $this->setName($container->getModuleName().'.'.$this->getName());
53 1
        }
54
55 1
        foreach ($this->paramMetas as $paramMeta){
56 1
            $mode = $paramMeta->isOptional?InputArgument::OPTIONAL:InputArgument::REQUIRED;
57 1
            if($paramMeta->container instanceof ArrayContainer || $paramMeta->container instanceof EntityContainer){
58 1
                $mode = $mode|InputArgument::IS_ARRAY;
59 1
            }
60 1
            $this->addArgument($paramMeta->name,
61 1
                $mode,
62 1
                $paramMeta->description,
63 1
                $paramMeta->default
64 1
                );
65 1
        }
66 1
    }
67
68 1
    public function getParamMeta($name)
69
    {
70 1
        foreach ($this->paramMetas as $meta){
71 1
            if($meta->name == $name){
72 1
                return $meta;
73
            }
74 1
        }
75
        return null;
76
    }
77
78
    /**
79
     * @param FactoryInterface $factory
80
     * @param ConsoleContainer $container
81
     * @param InputInterface $input
82
     * @param OutputInterface $output
83
     * @return mixed
84
     * @throws \DI\DependencyException
85
     * @throws \DI\NotFoundException
86
     */
87 1
    public function invoke(FactoryInterface $factory,ConsoleContainer $container, InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output 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...
88
    {
89 1
        $params = [];
90 1
        $reference = [];
91 1
        $this->bindInput($input, $params,$reference);
92 1
        $code = call_user_func_array([$container->getInstance($factory), $this->actionName], $params);
93 1
        return $code;
94
    }
95
96 1
    public function bindInput(InputInterface $input, array &$params, array &$reference){
0 ignored issues
show
Unused Code introduced by
The parameter $reference 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...
97 1
        $vld = new Validator();
98 1
        $req = ['argv'=>$input->getArguments()];
99 1
        $requestArray = new ArrayAdaptor($req);
100 1
        $inputs = [];
101 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...
102 1
            if($meta->isPassedByReference){
103
                // param PassedByReference is used to output
104
                continue;
105
            }
106 1
            $source = \JmesPath\search($meta->source, $requestArray);
107 1
            if ($source !== null){
108 1
                $source = ArrayAdaptor::strip($source);
109 1
                if($source instanceof ParameterBag){
110
                    $source = $source->all();
111
                }
112 1
                if($meta->container){
113 1
                    $inputs[$meta->name] = $meta->container->make($source);
114 1
                }else{
115
                    $inputs[$meta->name] = $source;
116
                }
117 1
                if($meta->validation){
118
                    $vld->rule($meta->validation, $meta->name);
119
                }
120 1
            }else{
121
                $meta->isOptional or \PhpBoot\abort(new \InvalidArgumentException("the parameter \"{$meta->source}\" is missing"));
122
                $inputs[$meta->name] = $meta->default;
123
            }
124 1
        }
125 1
        $vld = $vld->withData($inputs);
126 1
        $vld->validate() or \PhpBoot\abort(
127
            new \InvalidArgumentException(
128
                json_encode(
129
                    $vld->errors(),
130
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
131
                )
132
            )
133
        );
134
135 1
        $pos = 0;
136 1
        foreach ($this->paramMetas as $meta){
137 1
            if($meta->isPassedByReference){
138
                $params[$pos] = null;
139
            }else{
140 1
                $params[$pos] = $inputs[$meta->name];
141
            }
142 1
            $pos++;
143 1
        }
144
    }
145
}