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.

ApiGenerator::buildDocBlock()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the "php-ipfs" package.
7
 *
8
 * (c) Robert Schönthal <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace IPFS\Api;
15
16
use IPFS\Annotation\Api as Endpoint;
17
use IPFS\Command\Command;
18
use IPFS\Utils\CaseFormatter;
19
use PhpParser\Builder\Method;
20
use PhpParser\BuilderFactory;
21
use PhpParser\Node;
22
use PhpParser\PrettyPrinter\Standard;
23
24
class ApiGenerator
25
{
26
    /**
27
     * @var BuilderFactory
28
     */
29
    private $builder;
30
    /**
31
     * @var string
32
     */
33
    private $location;
34
35 2
    public function __construct(BuilderFactory $builder, $location = __DIR__)
36
    {
37 2
        $this->builder = $builder;
38 2
        $this->location = $location;
39 2
    }
40
41 1
    public function build(array $configs)
42
    {
43 1
        foreach ($configs as $name => $config) {
44 1
            $methods = [];
45
46 1
            foreach ($config as $methodConfig) {
47 1
                $method = $this->buildMethodNode($methodConfig);
48
49 1
                $methods[] = $method;
50
            }
51
52 1
            $header = $this->buildHeaderNode();
53 1
            $class = $this->buildClassNode($name, $methods);
54
55 1
            $prettyPrinter = new Standard();
56
57 1
            file_put_contents(
58 1
                $this->location . '/' . ucfirst(CaseFormatter::dashToCamel($this->generateClassName($name))) . '.php',
59 1
                $prettyPrinter->prettyPrintFile([$header, $class])
60
            );
61
        }
62 1
    }
63
64 1
    private function buildMethodNode(array $methodConfig): Method
65
    {
66 1
        $method = $this->builder
67 1
            ->method($methodConfig['method'])
68 1
            ->makePublic()
69 1
            ->setReturnType('Command')
70 1
            ->setDocComment($this->buildDocBlock($methodConfig));
71
72 1
        foreach ($methodConfig['arguments'] as $parameterConfig) {
73 1
            $parameter = $this->builder
74 1
                ->param($parameterConfig['name'])
75 1
                ->setTypeHint($parameterConfig['type']);
76
77 1
            if (!$parameterConfig['required']) {
78 1
                $default = $parameterConfig['default'];
79
80 1
                if ('int' === $parameterConfig['type']) {
81
                    $default = (int) $default;
82
                }
83 1
                $parameter->setDefault($default);
84
            }
85
86 1
            $method->addParam($parameter);
87
        }
88
89 1
        $method->addStmt(new Node\Stmt\Return_(
90 1
            new Node\Expr\New_(new Node\Name('Command'), [
91 1
                new Node\Scalar\MagicConst\Method(),
92 1
                new Node\Expr\FuncCall(new Node\Name('get_defined_vars')),
93
            ])
94
        ));
95
96 1
        return $method;
97
    }
98
99 1
    private function buildClassNode(string $name, array $methods): Node
100
    {
101 1
        return $this->builder->namespace('IPFS\Api')
102 1
            ->addStmt($this->builder->use(Endpoint::class)->as('Endpoint'))
103 1
            ->addStmt($this->builder->use(Command::class))
104 1
            ->addStmt(
105 1
                $this->builder->class($this->generateClassName($name))
106 1
                    ->implement('Api')
107 1
                    ->addStmts($methods)
108 1
                    ->setDocComment(
109
                        <<<'EOS'
110 1
/**
111
 * @author Robert Schönthal <[email protected]>
112
 * @autogenerated
113
 * @codeCoverageIgnore
114
 */
115
EOS
116
                    )
117 1
                    ->makeFinal()
118
            )
119 1
            ->getNode();
120
    }
121
122 1
    private function generateClassName(string $name): string
123
    {
124 1
        if ('Object' === $name) {
125
            return 'Cobject';
126
        }
127
128 1
        return $name;
129
    }
130
131 1
    private function buildDocBlock(array $methodConfig): string
132
    {
133 1
        $params = count($methodConfig['arguments']) ? '' : '*';
134
135 1
        foreach ($methodConfig['arguments'] as $index => $argument) {
136 1
            $suffix = $index + 1 === count($methodConfig['arguments']) ? "\n*" : "\n";
137 1
            $params .= sprintf('* @param %s $%s %s%s', $argument['type'], $argument['name'], $argument['description'], $suffix);
138
        }
139
140
        return <<<EOS
141
/**
142 1
 * {$methodConfig['description']}
143
 *
144 1
 * @Endpoint(name="{$methodConfig['parts']}")
145
 *
146 1
 {$params}
147
 * @return Command
148
 */
149
EOS;
150
    }
151
152 1
    private function buildHeaderNode(): Node
153
    {
154 1
        return new Node\Stmt\Declare_(
155 1
            [new Node\Stmt\DeclareDeclare('strict_types', new Node\Scalar\LNumber(1))]
156
        );
157
    }
158
}
159