Passed
Push — master ( b8146f...472e0b )
by Aleksandr
02:43
created

stripSpaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
function stripAndWordWrap($str)
4
{
5
    return wordwrap(stripSpaces($str), 180) . "\n";
6
}
7
8
function formParamLine($param)
9
{
10
    return '**' . $param['name'] . "** ({$param['type']}) - " . stripLines($param['description']);
11
}
12
13
function stripSpaces($str)
14
{
15
    $str = preg_replace('/[\s]{2,}/', " ", $str);
16
    return $str;
17
}
18
19
function stripLines($str)
20
{
21
    $str = str_replace("\n", " ", $str);
22
    $str = stripSpaces($str);
23
    return $str;
24
}
25
26
function formMethodName($str)
27
{
28
    $arr = array_filter(explode('_', $str));
29
    $arr = array_map('ucfirst', $arr);
30
    $arr[0] = lcfirst($arr[0]);
31
    return join('', $arr);
32
}
33
34
function formClassName($str)
35
{
36
    $clear = [
37
        'get_',
38
        'create_',
39
        '_array'
40
    ];
41
    foreach ($clear as $item) {
42
        $str = str_ireplace($item, '', $str);
43
    }
44
    $arr = array_filter(explode('_', $str));
45
    $arr = array_map('ucfirst', $arr);
46
    return join('', $arr);
47
}
48
49
function formParamType($str)
50
{
51
    if ($str == 'text') {
52
        return 'string';
53
    }
54
    return $str;
55
}
56
57
function parseParams($str)
58
{
59
    if (preg_match('/^(\w+)\s+\((\w+)\)\s+-\s+(.+)/uis', $str, $m)) {
60
        return [
61
            'name' => $m[1],
62
            'type' => $m[2],
63
            'description' => $m[3],
64
            'required' => mb_strpos($m[3], 'Необязательный', 0, 'utf-8') === false
65
        ];
66
    } else {
67
        return [];
68
    }
69
}
70
71
function parseReturns($tdHtml)
72
{
73
    $uls = pq($tdHtml)->find('ul');
74
    $returns = [];
75
    if ($uls->count() == 1 && strpos($tdHtml, 'Каждый элемент') !== false) {
76
        if (preg_match('/<td>(.)+<ul>/s', $tdHtml, $m)) {
77
            $ul = "<ul><li>" . trim(strip_tags($m[0])) . "</li></ul>";
78
            $uls = pq("<td>$ul{$uls->eq(0)->htmlOuter()}</td>")->find('ul');
79
        }
80
    }
81
    if ($uls->count() == 2) {
82
        $returnParam = parseParams($uls->eq(0)->text());
83
        $result = [];
84 View Code Duplication
        foreach ($uls->eq(1)->find('li') as $param) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            if ($parsedParam = parseParams(pq($param)->text())) {
86
                $result[] = $parsedParam;
87
            }
88
        }
89
        $returnParam['result'] = $result;
90
        $returns[] = $returnParam;
91
    } else {
92 View Code Duplication
        foreach ($uls->eq(0)->find('li') as $param) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            if ($parsedParam = parseParams(pq($param)->text())) {
94
                $returns[] = $parsedParam;
95
            }
96
        }
97
    }
98
    return $returns;
99
}
100
101
function asTable($array)
102
{
103
    $header = array_shift($array);
104
    $content = "\n\n|" . join("|", $header) . "\n";
105
    $content .= preg_replace('/[^\|]/iu', '-', trim($content)) . "\n";
106
    foreach ($array as $key => $columns) {
107
        $content .= "|" . join("|", $columns) . "\n";
108
    }
109
    return $content;
110
}
111
112
function asHeader($string)
113
{
114
    return "\n\n" . $string . "\n" . str_repeat('=', mb_strlen($string, 'utf-8')) . "\n";
115
}
116
117
function asCode($code)
118
{
119
    return "`$code`";
120
}