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

ApiParser   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.1%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 134
ccs 67
cts 69
cp 0.971
rs 10
c 0
b 0
f 0

7 Methods

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