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.

ApiParser   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.95%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 7
dl 0
loc 146
ccs 71
cts 74
cp 0.9595
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A build() 0 10 1
A buildMethods() 0 35 5
B parseArguments() 0 38 7
A parseDefaultValue() 0 10 2
A parseDescription() 0 8 3
A parseTypeHint() 0 8 2
A fixDocumentationLinks() 0 8 2
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://docs.ipfs.io/reference/api/http/', 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('main 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
66 1
            $anchor = $this->crawler->filter($link)->first();
67 1
            $description = 'p' === $anchor->nextAll()->first()->getNode(0)->nodeName ? $anchor->nextAll()->first()->text() : null;
68
69
            $methodConfig = [
70 1
                'parts'       => str_replace('/', ':', str_replace('/api/v0/', '', $anchor->text())),
71 1
                'description' => $description,
72 1
                'arguments'   => $this->parseArguments($anchor, $description ? 2 : 1),
73
            ];
74
75 1
            $nameParts = explode(':', $methodConfig['parts']);
76 1
            if (count($nameParts) > 1) {
77 1
                $class = array_shift($nameParts);
78 1
                $methodConfig['class'] = ucfirst(CaseFormatter::dashToCamel($class));
79 1
                $methodConfig['method'] = CaseFormatter::dashToCamel(implode('-', $nameParts));
80
            } else {
81 1
                $methodConfig['class'] = 'Basics';
82 1
                $methodConfig['method'] = CaseFormatter::dashToCamel($methodConfig['parts']);
83
            }
84
85 1
            $config[$methodConfig['class']][] = $methodConfig;
86
        }
87
88 1
        return $config;
89
    }
90
91 1
    private function parseArguments(Crawler $anchor, int $index): array
92
    {
93 1
        $argumentsRootNode = $anchor->nextAll()->eq($index)->first();
94
95 1
        if ('ul' === $argumentsRootNode->getNode(0)->nodeName) {
96 1
            $names = [];
97
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 1
                if (!isset($names[$name])) {
102 1
                    $names[$name] = 0;
103
                } else {
104 1
                    ++$names[$name];
105
                }
106
107
                $config = [
108 1
                    'name'        => 0 === $names[$name] ? $name : $name . $names[$name],
109 1
                    'required'    => $argument->filter('strong')->count() > 0,
110 1
                    'description' => $this->parseDescription($description),
111 1
                    'default'     => $this->parseDefaultValue($description),
112 1
                    'type'        => $this->parseTypeHint($description),
113
                ];
114
115 1
                $config['default'] = 'bool' === $config['type'] && null === $config['default'] ? false : $config['default'];
116
117
                //fixup files
118 1
                if ('file' === $config['type']) {
119 1
                    $config['type'] = 'string';
120 1
                    $config['name'] = 'file';
121
                }
122
123 1
                return $config;
124 1
            });
125
        }
126
127 1
        return [];
128
    }
129
130 1
    private function parseDefaultValue(string $description)
131
    {
132 1
        preg_match('/Default.* [‘|"|“]([^\.]+)[’|"|”]\./', $description, $default);
133
134 1
        if (2 === count($default)) {
135 1
            $default = preg_replace('/[^\x00-\x7f]/', '', $default[1]);
136
137 1
            return CaseFormatter::stringToBool($default);
138
        }
139 1
    }
140
141 1
    private function parseDescription($description): string
142
    {
143 1
        preg_match('/\]\: (.+)/', $description, $default);
144
145 1
        if (count($default) > 1) {
146 1
            return substr($default[1], 0, strpos($default[1], '.') ?: strlen($default[1])) . '.';
147
        }
148
    }
149
150 1
    private function parseTypeHint(string $description): string
151
    {
152 1
        preg_match('/\[([^\]]+)\]/', $description, $default);
153
154 1
        if (count($default) > 1) {
155 1
            return $default[1];
156
        }
157
    }
158
159 1
    private function fixDocumentationLinks(string $link): string
160
    {
161 1
        if ('#api-v0-statsrepo' === $link) {
162
            return '#api-v0-stats-repo';
163
        }
164
165 1
        return $link;
166
    }
167
}
168