Completed
Pull Request — master (#347)
by Christoffer
05:20 queued 03:07
created

escapeQuote()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    // Map over the description lines and merge them into a flat array.
13
    return \array_merge(...\array_map(function (string $line) use ($maxLength) {
14
        if (\strlen($line) < ($maxLength + 5)) {
15
            return [$line];
16
        }
17
18
        // For > 120 character long lines, cut at space boundaries into sublines of ~80 chars.
19
        return breakLine($line, $maxLength);
20
    }, \explode("\n", $description)));
21
}
22
23
/**
24
 * @param string $line
25
 * @param int    $maxLength
26
 * @return array
27
 */
28
function breakLine(string $line, int $maxLength): array
29
{
30
    if (\strlen($line) < ($maxLength + 5)) {
31
        return [$line];
32
    }
33
34
    $endPos     = $maxLength - 40;
35
    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

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