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 ( 4d9f58...3d3029 )
by Robert
05:04 queued 02:49
created

CommandBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 11.63%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 11
dl 0
loc 113
ccs 5
cts 43
cp 0.1163
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A generateCommands() 0 17 4
A generateCommand() 0 10 1
C buildDefinition() 0 31 7
A createCode() 0 19 1
A sanitizeArguments() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license. For more information, see
20
 * <https://github.com/digitalkaoz/php-ipfs>
21
 */
22
23
namespace IPFS\Console;
24
25
use ArgumentsResolver\NamedArgumentsResolver;
26
use IPFS\Api;
27
use IPFS\Client;
28
use IPFS\Utils\AnnotationReader;
29
use IPFS\Utils\CaseFormatter;
30
use Pimple\Container;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputDefinition;
34
use Symfony\Component\Console\Input\InputInterface;
35
use Symfony\Component\Console\Input\InputOption;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
class CommandBuilder
39
{
40
    /**
41
     * @var array|Api\Api[]
42
     */
43
    private $apis;
44
    /**
45
     * @var AnnotationReader
46
     */
47
    private $reader;
48
    /**
49
     * @var Container
50
     */
51
    private $container;
52
53 1
    public function __construct(array $apis, Container $container)
54
    {
55 1
        $this->apis = $apis;
56 1
        $this->container = $container;
57
58 1
        $this->reader = $this->container[AnnotationReader::class];
59 1
    }
60
61
    public function generateCommands(): array
62
    {
63
        $commands = [];
64
65
        foreach ($this->apis as $class) {
66
            $api = new \ReflectionClass($class);
67
68
            foreach ($api->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
69
                if ($this->reader->isApi($method)) {
70
                    $command = $this->generateCommand($class, $api->getShortName(), $method);
71
                    $commands[$command->getName()] = $command;
72
                }
73
            }
74
        }
75
76
        return $commands;
77
    }
78
79
    private function generateCommand(Api\Api $api, string $name, \ReflectionMethod $method): Command
80
    {
81
        $command = new ApiCommand(strtolower($this->reader->getName($method)));
82
83
        return $command
84
            ->setDefinition($this->buildDefinition($method))
85
            ->setDescription($this->reader->getDescription($method))
86
            ->setCode($this->createCode($api, $name, $method))
87
        ;
88
    }
89
90
    private function buildDefinition(\ReflectionMethod $method): InputDefinition
91
    {
92
        $definition = new InputDefinition();
93
94
        $parameters = $this->reader->getParameters($method);
95
96
        foreach ($parameters as $name => $param) {
97
            $name = CaseFormatter::camelToDash($name);
98
99
            if ($param->hasDefault()) {
100
                $mode = InputOption::VALUE_NONE;
101
                $default = $param->getDefault();
102
103
                if (true === $default || is_string($default)) {
104
                    $mode = InputOption::VALUE_OPTIONAL;
105
                } elseif (is_bool($default)) {
106
                    $default = null;
107
                } elseif (!is_string($default)) {
108
                    $mode = InputOption::VALUE_REQUIRED;
109
                }
110
111
                $definition->addOption(new InputOption($name, null, $mode, $param->getDescription(), $default));
112
113
                continue;
114
            }
115
116
            $definition->addArgument(new InputArgument($name, InputArgument::REQUIRED, $param->getDescription(), null));
117
        }
118
119
        return $definition;
120
    }
121
122
    private function createCode(Api\Api $api, $name, \ReflectionMethod $method): \Closure
123
    {
124
        return function (InputInterface $input, OutputInterface $output) use ($name, $method, $api) {
125
            $fn = $method->getClosure($api);
126
127
            $options = CaseFormatter::dashToCamelArray($input->getOptions());
128
            $arguments = CaseFormatter::dashToCamelArray($input->getArguments());
129
130
            $args = (new NamedArgumentsResolver($method))->resolve(array_merge($options, $arguments));
131
            $args = $this->sanitizeArguments($args);
132
133
            $client = new Client($this->container[$input->getOption('driver')]);
134
            $response = $client->execute($fn(...$args));
135
136
            $output->writeln($response);
137
            //dump($response, json_decode($response, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
138
            //$output->writeln(print_r(json_decode($response, true), true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% 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...
139
        };
140
    }
141
142
    public function sanitizeArguments(array $args): array
143
    {
144
        foreach ($args as $index => $value) {
145
            $args[$index] = CaseFormatter::stringToBool($value);
146
        }
147
148
        return $args;
149
    }
150
}
151