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.
Completed
Push β€” master ( 0268aa...3ec9bd )
by cao
05:24
created

Command::bindInput()   C

Complexity

Conditions 11
Paths 72

Size

Total Lines 49
Code Lines 35

Duplication

Lines 24
Ratio 48.98 %

Code Coverage

Tests 26
CRAP Score 18.4742

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 72
nop 3
dl 24
loc 49
ccs 26
cts 43
cp 0.6047
crap 18.4742
rs 5.2653
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 PhpBoot\Entity\ArrayContainer;
13
use PhpBoot\Entity\EntityContainer;
14
use PhpBoot\Metas\ParamMeta;
15
use PhpBoot\Utils\ArrayAdaptor;
16
use PhpBoot\Validator\Validator;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\HttpFoundation\ParameterBag;
21
22
class Command extends \Symfony\Component\Console\Command\Command
23
{
24
25
    /**
26
     * @var ParamMeta[]
27
     */
28
    private $paramMetas;
29
30
    /**
31
     * @var string
32
     */
33
    private $actionName;
34
35 1
    public function __construct($actionName, $name)
36
    {
37 1
        parent::__construct($name?:$actionName);
38 1
        $this->actionName = $actionName;
39 1
    }
40
    /**
41
     * @param ParamMeta[] $paramMetas
42
     */
43 1
    public function setParamMetas($paramMetas)
44
    {
45 1
        $this->paramMetas = $paramMetas;
46 1
    }
47
48 1
    public function postCreate(ConsoleContainer $container)
49
    {
50 1
        if($container->getModuleName()){
51 1
            $this->setName($container->getModuleName().'.'.$this->getName());
52 1
        }
53
54 1
        foreach ($this->paramMetas as $paramMeta){
55 1
            $mode = $paramMeta->isOptional?InputArgument::OPTIONAL:InputArgument::REQUIRED;
56 1
            if($paramMeta->container instanceof ArrayContainer || $paramMeta->container instanceof EntityContainer){
57 1
                $mode = $mode|InputArgument::IS_ARRAY;
58 1
            }
59 1
            $this->addArgument($paramMeta->name,
60 1
                $mode,
61 1
                $paramMeta->description,
62 1
                $paramMeta->default
63 1
                );
64 1
        }
65 1
    }
66
67 1
    public function getParamMeta($name)
68
    {
69 1
        foreach ($this->paramMetas as $meta){
70 1
            if($meta->name == $name){
71 1
                return $meta;
72
            }
73 1
        }
74
        return null;
75
    }
76
77 1
    public function invoke(ConsoleContainer $container, InputInterface $input, OutputInterface $output)
78
    {
79 1
        $params = [];
80 1
        $reference = [];
81 1
        $this->bindInput($input, $params,$reference);
82 1
        ob_start();
83 1
        $code = call_user_func_array([$container->getInstance(), $this->actionName], $params);
84 1
        $out = ob_get_contents();
85 1
        ob_end_clean();
86 1
        $output->write($out);
87 1
        return $code;
88
    }
89
90 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...
91 1
        $vld = new Validator();
92 1
        $req = ['argv'=>$input->getArguments()];
93 1
        $requestArray = new ArrayAdaptor($req);
94 1
        $inputs = [];
95 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...
96 1
            if($meta->isPassedByReference){
97
                // param PassedByReference is used to output
98
                continue;
99
            }
100 1
            $source = \JmesPath\search($meta->source, $requestArray);
101 1
            if ($source !== null){
102 1
                $source = ArrayAdaptor::strip($source);
103 1
                if($source instanceof ParameterBag){
104
                    $source = $source->all();
105
                }
106 1
                if($meta->container){
107 1
                    $inputs[$meta->name] = $meta->container->make($source);
108 1
                }else{
109
                    $inputs[$meta->name] = $source;
110
                }
111 1
                if($meta->validation){
112
                    $vld->rule($meta->validation, $meta->name);
113
                }
114 1
            }else{
115
                $meta->isOptional or \PhpBoot\abort(new \InvalidArgumentException("the parameter \"{$meta->source}\" is missing"));
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...
116
                $inputs[$meta->name] = $meta->default;
117
            }
118 1
        }
119 1
        $vld = $vld->withData($inputs);
120 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...
121
            new \InvalidArgumentException(
122
                json_encode(
123
                    $vld->errors(),
124
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
125
                )
126
            )
127
        );
128
129 1
        $pos = 0;
130 1
        foreach ($this->paramMetas as $meta){
131 1
            if($meta->isPassedByReference){
132
                $params[$pos] = null;
133
            }else{
134 1
                $params[$pos] = $inputs[$meta->name];
135
            }
136 1
            $pos++;
137 1
        }
138
    }
139
}