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 ( a81c8e...63f10a )
by Robert
09:51
created

ApiParser::parseArguments()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 2
nop 2
crap 7
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 Http\Client\HttpAsyncClient;
17
use Http\Message\MessageFactory;
18
use IPFS\Utils\CaseFormatter;
19
use Psr\Http\Message\ResponseInterface;
20
use Symfony\Component\DomCrawler\Crawler;
21
22
class ApiParser
23
{
24
    /**
25
     * @var HttpAsyncClient
26
     */
27
    private $client;
28
    /**
29
     * @var MessageFactory
30
     */
31
    private $messageFactory;
32
    /**
33
     * @var Crawler
34
     */
35
    private $crawler;
36
37 2
    public function __construct(HttpAsyncClient $client, MessageFactory $messageFactory, Crawler $crawler)
38
    {
39 2
        $this->client = $client;
40 2
        $this->messageFactory = $messageFactory;
41 2
        $this->crawler = $crawler;
42 2
    }
43
44 1
    public function build(string $url = 'https://ipfs.io/docs/api/', string $prefix = '#api-v0'): array
45
    {
46 1
        return $this->client
47 1
            ->sendAsyncRequest($this->messageFactory->createRequest('GET', $url))
48
            ->then(function (ResponseInterface $response) use ($prefix) {
49 1
                return $this->buildMethods($response, $prefix);
50 1
            })
51 1
            ->wait()
52
        ;
53
    }
54
55 1
    private function buildMethods(ResponseInterface $response, $prefix): array
56
    {
57 1
        $this->crawler->addHtmlContent($response->getBody()->getContents());
58
        $links = $this->crawler->filter('li a[href^="' . $prefix . '"]')->each(function (Crawler $node) {
59 1
            return $node->attr('href');
60 1
        });
61
62 1
        $config = [];
63 1
        foreach ($links as $link) {
64 1
            $link = $this->fixDocumentationLinks($link);
65 1
66
            $anchor = $this->crawler->filter($link)->first();
67
            $description = $anchor->nextAll()->first()->getNode(0)->nodeName === 'p' ? $anchor->nextAll()->first()->text() : null;
68 1
69 1
            $methodConfig = [
70 1
                'parts'       => str_replace('/', ':', str_replace('/api/v0/', '', $anchor->text())),
71
                'description' => $description,
72
                'arguments'   => $this->parseArguments($anchor, $description ? 2 : 1),
73 1
            ];
74 1
75 1
            $nameParts = explode(':', $methodConfig['parts']);
76 1
            if (count($nameParts) > 1) {
77 1
                $class = array_shift($nameParts);
78
                $methodConfig['class'] = ucfirst(CaseFormatter::dashToCamel($class));
79 1
                $methodConfig['method'] = CaseFormatter::dashToCamel(implode('-', $nameParts));
80 1
            } else {
81
                $methodConfig['class'] = 'Basics';
82
                $methodConfig['method'] = CaseFormatter::dashToCamel($methodConfig['parts']);
83 1
            }
84
85
            $config[$methodConfig['class']][] = $methodConfig;
86 1
        }
87
88
        return $config;
89 1
    }
90
91 1
    private function parseArguments(Crawler $anchor, int $index): array
92
    {
93 1
        $argumentsRootNode = $anchor->nextAll()->eq($index)->first();
94 1
95
        if ($argumentsRootNode->getNode(0)->nodeName === 'ul') {
96 1
            $names = [];
97 1
98 1
            return $argumentsRootNode->filter('li')->each(function (Crawler $argument) use (&$names) {
99 1
                $description = $argument->filter('code')->first()->getNode(0)->nextSibling->textContent;
100 1
                $name = CaseFormatter::dashToCamel($argument->filter('code')->first()->text());
101
                if (!isset($names[$name])) {
102 1
                    $names[$name] = 0;
103
                } else {
104
                    ++$names[$name];
105
                }
106 1
107 1
                $config = [
108 1
                    'name'        => $names[$name] === 0 ? $name : $name . $names[$name],
109 1
                    'required'    => $argument->filter('strong')->count() > 0,
110 1
                    'description' => $this->parseDescription($description),
111
                    'default'     => $this->parseDefaultValue($description),
112
                    'type'        => $this->parseTypeHint($description),
113
                ];
114 1
115 1
                $config['default'] = $config['type'] === 'bool' && $config['default'] === null ? false : $config['default'];
116 1
117
                //fixup files
118
                if ('file' === $config['type']) {
119 1
                    $config['type'] = 'string';
120 1
                    $config['name'] = 'file';
121
                }
122
123 1
                return $config;
124
            });
125
        }
126 1
127
        return [];
128 1
    }
129
130 1
    private function parseDefaultValue(string $description)
131 1
    {
132 1
        preg_match('/Default.* [‘|"|“]([^\.]+)[’|"|”]\./', $description, $default);
133
134 1
        if (count($default) === 2) {
135
            $default = preg_replace('/[^\x00-\x7f]/', '', $default[1]);
136 1
137
            return CaseFormatter::stringToBool($default);
138 1
        }
139
    }
140 1
141
    private function parseDescription($description): string
142 1
    {
143 1
        preg_match('/\]\: (.+)/', $description, $default);
144
145
        if (count($default) > 1) {
146
            return substr($default[1], 0, strpos($default[1], '.') ?: strlen($default[1])) . '.';
147 1
        }
148
    }
149 1
150
    private function parseTypeHint(string $description): string
151 1
    {
152 1
        preg_match('/\[([^\]]+)\]/', $description, $default);
153
154
        if (count($default) > 1) {
155
            return $default[1];
156
        }
157
    }
158
159
    private function fixDocumentationLinks(string $link): string
160
    {
161
        if ($link === '#api-v0-statsrepo') {
162
            return '#api-v0-stats-repo';
163
        }
164
165
        return $link;
166
    }
167
}
168