Passed
Push — master ( 1dbf07...686b05 )
by Aleksandr
05:06
created

parseReturns()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 22
Code Lines 16

Duplication

Lines 5
Ratio 22.73 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 22
cc 8
eloc 16
nc 18
nop 1
rs 6.6037
1
<?php
2
function clearFolder($folder)
3
{
4
    $files = glob(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . '*');
5
    foreach ($files as $file) {
6
        if (is_file($file)) {
7
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

7
            /** @scrutinizer ignore-unhandled */ @unlink($file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
8
        }
9
    }
10
}
11
12
function stripAndWordWrap($str)
13
{
14
    return wordwrap(stripSpaces($str), 180) . "\n";
15
}
16
17
function formParamLine($param)
18
{
19
    return '**' . $param['name'] . "** ({$param['type']}) - " . stripLines($param['description']);
20
}
21
22
function stripSpaces($str)
23
{
24
    $str = preg_replace('/[\s]{2,}/', " ", $str);
25
    return $str;
26
}
27
28
function stripLines($str)
29
{
30
    $str = str_replace("\n", " ", $str);
31
    $str = stripSpaces($str);
32
    return $str;
33
}
34
35
function formMethodName($str)
36
{
37
    $arr = array_filter(explode('_', $str));
38
    $arr = array_map('ucfirst', $arr);
39
    $arr[0] = lcfirst($arr[0]);
40
    return join('', $arr);
41
}
42
43
function formClassName($str, $clear = ['get_', '_array'])
44
{
45
    foreach ($clear as $item) {
46
        $str = str_ireplace($item, '', $str);
47
    }
48
    $arr = array_filter(explode('_', $str));
49
    $arr = array_map('ucfirst', $arr);
50
    return join('', $arr);
51
}
52
53
function formParamType($str)
54
{
55
    switch ($str) {
56
        case 'text':
57
            return 'string';
58
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
59
        case 'date':
60
            return 'string Y-m-d';
61
            break;
62
        case 'datetime':
63
            return 'string Y-m-d H:i:s';
64
            break;
65
    }
66
    return $str;
67
}
68
69
function parseParams($str)
70
{
71
    if (preg_match('/^(\w+)\s+\((\w+)\)\s+-\s+(.+)/uis', $str, $m)) {
72
        return [
73
            'name' => $m[1],
74
            'type' => $m[2],
75
            'description' => $m[3],
76
            'required' => mb_strpos($m[3], 'Необязательный', 0, 'utf-8') === false
77
        ];
78
    } else {
79
        return [];
80
    }
81
}
82
83
function parseReturns($tdHtml)
84
{
85
    $uls = pq($tdHtml)->find('ul');
86
    if ($uls->count() == 1 && strpos($tdHtml, 'Каждый элемент') !== false) {
87
        if (preg_match('/<td>(.)+<ul>/s', $tdHtml, $m)) {
88
            $ul = "<ul><li>" . trim(strip_tags($m[0])) . "</li></ul>";
89
            $uls = pq("<td>$ul{$uls->eq(0)->htmlOuter()}</td>")->find('ul');
90
        }
91
    }
92
    $count = $uls->count();
93
    $parsing = [];
94 View Code Duplication
    foreach ($uls->eq($count == 2 ? 1 : 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...
95
        if ($parsedParam = parseParams(pq($param)->text())) {
96
            $parsing[] = $parsedParam;
97
        }
98
    }
99
    if ($count == 2) {
100
        $returnParam = parseParams($uls->eq(0)->text());
101
        $returnParam['result'] = $parsing;
102
        return [$returnParam];
103
    } else {
104
        return $parsing;
105
    }
106
}
107
108
function asTable($array)
109
{
110
    $header = array_shift($array);
111
    $content = "\n\n|" . join("|", $header) . "\n";
112
    $content .= preg_replace('/[^\|]/iu', '-', trim($content)) . "\n";
113
    foreach ($array as $key => $columns) {
114
        $content .= "|" . join("|", $columns) . "\n";
115
    }
116
    return $content;
117
}
118
119
function asHeader($string)
120
{
121
    return "\n\n" . $string . "\n" . str_repeat('=', mb_strlen($string, 'utf-8')) . "\n";
122
}
123
124
function asCode($code)
125
{
126
    return "`$code`";
127
}