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 ( 105235...11f67e )
by Robert
10:42
created

ApiGenerator   B

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 16
dl 0
loc 120
c 0
b 0
f 0
ccs 0
cts 61
cp 0
rs 8.4614

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 22 3
B buildMethodNode() 0 35 4
A buildClassNode() 0 20 1
A buildDocBlock() 0 20 4
A buildHeaderNode() 0 6 1
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\Api;
24
25
use IPFS\Annotation\Api as Endpoint;
26
use IPFS\Command\Command;
27
use IPFS\Utils\CaseFormatter;
28
use PhpParser\Builder\Method;
29
use PhpParser\BuilderFactory;
30
use PhpParser\Node;
31
use PhpParser\PrettyPrinter\Standard;
32
33
class ApiGenerator
34
{
35
    /**
36
     * @var BuilderFactory
37
     */
38
    private $builder;
39
40
    public function __construct(BuilderFactory $builder)
41
    {
42
        $this->builder = $builder;
43
    }
44
45
    public function build(array $configs)
46
    {
47
        foreach ($configs as $name => $config) {
48
            $methods = [];
49
50
            foreach ($config as $methodConfig) {
51
                $method = $this->buildMethodNode($methodConfig);
52
53
                $methods[] = $method;
54
            }
55
56
            $header = $this->buildHeaderNode();
57
            $class = $this->buildClassNode($name, $methods);
58
59
            $prettyPrinter = new Standard();
60
61
            file_put_contents(
62
                __DIR__ . '/' . ucfirst(CaseFormatter::dashToCamel($name)) . '.php',
63
                $prettyPrinter->prettyPrintFile([$header, $class])
64
            );
65
        }
66
    }
67
68
    private function buildMethodNode(array $methodConfig): Method
69
    {
70
        $method = $this->builder
71
            ->method($methodConfig['method'])
72
            ->makePublic()
73
            ->setReturnType('Command')
74
            ->setDocComment($this->buildDocBlock($methodConfig))
75
        ;
76
77
        foreach ($methodConfig['arguments'] as $parameterConfig) {
78
            $parameter = $this->builder
79
                ->param($parameterConfig['name'])
80
                ->setTypeHint($parameterConfig['type']);
81
82
            if (!$parameterConfig['required']) {
83
                $default = $parameterConfig['default'];
84
85
                if ($parameterConfig['type'] === 'int') {
86
                    $default = (int) $default;
87
                }
88
                $parameter->setDefault($default);
89
            }
90
91
            $method->addParam($parameter);
92
        }
93
94
        $method->addStmt(new Node\Stmt\Return_(
95
            new Node\Expr\New_(new Node\Name('Command'), [
96
                new Node\Scalar\MagicConst\Method(),
97
                new Node\Expr\FuncCall(new Node\Name('get_defined_vars')),
98
            ])
99
        ));
100
101
        return $method;
102
    }
103
104
    private function buildClassNode(string $name, array $methods): Node
105
    {
106
        return $this->builder->namespace('IPFS\Api')
107
            ->addStmt($this->builder->use(Endpoint::class)->as('Endpoint'))
108
            ->addStmt($this->builder->use(Command::class))
109
            ->addStmt($this->builder->class($name)
110
                ->implement('Api')
111
                ->addStmts($methods)
112
                ->setDocComment(<<<'EOS'
113
/**
114
 * @author Robert Schönthal <[email protected]>
115
 * @autogenerated
116
 * @codeCoverageIgnore
117
 */
118
EOS
119
                )
120
                ->makeFinal()
121
            )
122
            ->getNode();
123
    }
124
125
    private function buildDocBlock(array $methodConfig): string
126
    {
127
        $params = count($methodConfig['arguments']) ? '' : '*';
128
129
        foreach ($methodConfig['arguments'] as $index => $argument) {
130
            $suffix = $index + 1 === count($methodConfig['arguments']) ? "\n*" : "\n";
131
            $params .= sprintf('* @param %s $%s %s%s', $argument['type'], $argument['name'], $argument['description'], $suffix);
132
        }
133
134
        return <<<EOS
135
/**
136
 * {$methodConfig['description']}
137
 *
138
 * @Endpoint(name="{$methodConfig['parts']}")
139
 *
140
 {$params}
141
 * @return Command
142
 */
143
EOS;
144
    }
145
146
    private function buildHeaderNode(): Node
147
    {
148
        return new Node\Stmt\Declare_(
149
            [new Node\Stmt\DeclareDeclare('strict_types', new Node\Scalar\LNumber(1))]
150
        );
151
    }
152
}
153