Passed
Pull Request — master (#347)
by Christoffer
06:07
created

escapeQuotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Digia\GraphQL\Schema;
4
5
/**
6
 * @param string $description
7
 * @param int    $maxLength
8
 * @return array
9
 */
10
function descriptionLines(string $description, int $maxLength): array
11
{
12
    $rawLines = \explode("\n", $description);
13
14
    return \array_merge(...\array_map(function (string $line) use ($maxLength) {
15
        if (\strlen($line) < ($maxLength + 5)) {
16
            return [$line];
17
        }
18
19
        // For > 120 character long lines, cut at space boundaries into sublines of ~80 chars.
20
        $broken = breakLine($line, $maxLength);
21
22
        return $broken;
23
    }, $rawLines));
24
}
25
26
/**
27
 * @param string $line
28
 * @param int    $maxLength
29
 * @return array
30
 */
31
function breakLine(string $line, int $maxLength): array
32
{
33
    if (\strlen($line) < ($maxLength + 5)) {
34
        return [$line];
35
    }
36
37
    $endPos     = $maxLength - 40;
38
    return \array_map('trim', \preg_split(
0 ignored issues
show
Bug introduced by
It seems like preg_split('/((?: |^).{1...EG_SPLIT_DELIM_CAPTURE) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

38
    return \array_map('trim', /** @scrutinizer ignore-type */ \preg_split(
Loading history...
39
        "/((?: |^).{15,{$endPos}}(?= |$))/",
40
        $line,
41
        0,
42
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
43
    ));
44
//    $parts      = false === $parts ? [] : $parts;
45
//    $partsCount = \count($parts);
46
//
47
//    if ($partsCount < 4) {
48
//        return [$line];
49
//    }
50
//
51
//    $subLines = [$parts[0] . $parts[1] . $parts[2]];
52
//
53
//    for ($i = 3; $i < $partsCount; $i += 2) {
54
//        $subLines[] = \substr($parts[$i], 1) . ($parts[$i + 1] ?? '');
55
//    }
56
//
57
//    return $subLines;
58
}
59
60
/**
61
 * @param string $line
62
 * @return string
63
 */
64
function escapeQuotes(string $line): string
65
{
66
    return strtr($line, ['"""' => '\\"""', '`' => '\`']);
67
}
68
69
/**
70
 * @param array $lines
71
 * @return string
72
 */
73
function printLines(array $lines): string
74
{
75
    // Don't print empty lines
76
    $lines = \array_filter($lines, function (string $line) {
77
        return $line !== '';
78
    });
79
80
    return printArray("\n", $lines);
81
}
82
83
/**
84
 * @param array $fields
85
 * @return string
86
 */
87
function printInputFields(array $fields): string
88
{
89
    return '(' . printArray(', ', $fields) . ')';
90
}
91
92
/**
93
 * @param string $glue
94
 * @param array  $items
95
 * @return string
96
 */
97
function printArray(string $glue, array $items): string
98
{
99
    return \implode($glue, $items);
100
}
101