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 ( 709c6a...063dd9 )
by Robert
32:19 queued 28:25
created

ApiGenerator   B

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 98.36%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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 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($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
73 1
        foreach ($methodConfig['arguments'] as $parameterConfig) {
74 1
            $parameter = $this->builder
75 1
                ->param($parameterConfig['name'])
76 1
                ->setTypeHint($parameterConfig['type']);
77
78 1
            if (!$parameterConfig['required']) {
79 1
                $default = $parameterConfig['default'];
80
81 1
                if ($parameterConfig['type'] === 'int') {
82
                    $default = (int) $default;
83
                }
84 1
                $parameter->setDefault($default);
85
            }
86
87 1
            $method->addParam($parameter);
88
        }
89
90 1
        $method->addStmt(new Node\Stmt\Return_(
91 1
            new Node\Expr\New_(new Node\Name('Command'), [
92 1
                new Node\Scalar\MagicConst\Method(),
93 1
                new Node\Expr\FuncCall(new Node\Name('get_defined_vars')),
94
            ])
95
        ));
96
97 1
        return $method;
98
    }
99
100 1
    private function buildClassNode(string $name, array $methods): Node
101
    {
102 1
        return $this->builder->namespace('IPFS\Api')
103 1
            ->addStmt($this->builder->use(Endpoint::class)->as('Endpoint'))
104 1
            ->addStmt($this->builder->use(Command::class))
105 1
            ->addStmt($this->builder->class($name)
106 1
                ->implement('Api')
107 1
                ->addStmts($methods)
108 1
                ->setDocComment(<<<'EOS'
109
/**
110
 * @author Robert Schönthal <[email protected]>
111
 * @autogenerated
112
 * @codeCoverageIgnore
113 1
 */
114
EOS
115
                )
116 1
                ->makeFinal()
117
            )
118 1
            ->getNode();
119
    }
120
121 1
    private function buildDocBlock(array $methodConfig): string
122
    {
123 1
        $params = count($methodConfig['arguments']) ? '' : '*';
124
125 1
        foreach ($methodConfig['arguments'] as $index => $argument) {
126 1
            $suffix = $index + 1 === count($methodConfig['arguments']) ? "\n*" : "\n";
127 1
            $params .= sprintf('* @param %s $%s %s%s', $argument['type'], $argument['name'], $argument['description'], $suffix);
128
        }
129
130
        return <<<EOS
131
/**
132 1
 * {$methodConfig['description']}
133
 *
134 1
 * @Endpoint(name="{$methodConfig['parts']}")
135
 *
136 1
 {$params}
137
 * @return Command
138 1
 */
139
EOS;
140
    }
141
142 1
    private function buildHeaderNode(): Node
143
    {
144 1
        return new Node\Stmt\Declare_(
145 1
            [new Node\Stmt\DeclareDeclare('strict_types', new Node\Scalar\LNumber(1))]
146
        );
147
    }
148
}
149