ObjectDescription   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 29
c 1
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypes() 0 3 1
B parseReturnTypes() 0 35 8
A __construct() 0 3 1
1
<?php
2
3
namespace TgScraper\Parsers;
4
5
use voku\helper\HtmlDomParser;
6
7
/**
8
 * Class ObjectDescription
9
 * @package TgScraper\Parsers
10
 */
11
class ObjectDescription
12
{
13
14
    /**
15
     * @var array
16
     */
17
    private array $types;
18
19
    /**
20
     * @param string $description
21
     */
22
    public function __construct(string $description)
23
    {
24
        $this->types = self::parseReturnTypes($description);
25
    }
26
27
    /**
28
     * @param string $description
29
     * @return array
30
     */
31
    private static function parseReturnTypes(string $description): array
32
    {
33
        $returnTypes = [];
34
        $phrases = explode('.', $description);
35
        $phrases = array_filter(
36
            $phrases,
37
            function ($phrase) {
38
                return (false !== stripos($phrase, 'returns') or false !== stripos($phrase, 'is returned'));
39
            }
40
        );
41
        foreach ($phrases as $phrase) {
42
            $dom = HtmlDomParser::str_get_html($phrase);
43
            $a = $dom->findMulti('a');
44
            $em = $dom->findMulti('em');
45
            foreach ($a as $element) {
46
                if ($element->text() == 'Messages') {
47
                    $returnTypes[] = 'Array<Message>';
48
                    continue;
49
                }
50
                $arrays = substr_count(strtolower($phrase), 'array');
51
                $returnType = $element->text();
52
                for ($i = 0; $i < $arrays; $i++) {
53
                    $returnType = sprintf('Array<%s>', $returnType);
54
                }
55
                $returnTypes[] = $returnType;
56
            }
57
            foreach ($em as $element) {
58
                if (in_array($element->text(), ['False', 'force', 'Array'])) {
59
                    continue;
60
                }
61
                $type = str_replace(['True', 'Int', 'String'], ['bool', 'int', 'string'], $element->text());
62
                $returnTypes[] = $type;
63
            }
64
        }
65
        return $returnTypes;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getTypes(): array
72
    {
73
        return $this->types;
74
    }
75
76
}