Passed
Pull Request — master (#347)
by Christoffer
02:20
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
    // 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
36
    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

36
    return \array_map('trim', /** @scrutinizer ignore-type */ \preg_split(
Loading history...
37
        "/((?: |^).{15,{$endPos}}(?= |$))/",
38
        $line,
39
        0,
40
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
41
    ));
42
}
43
44
/**
45
 * @param string $line
46
 * @return string
47
 */
48
function escapeQuotes(string $line): string
49
{
50
    return \strtr($line, ['"""' => '\\"""', '`' => '\`']);
51
}
52
53
/**
54
 * @param array $lines
55
 * @return string
56
 */
57
function printLines(array $lines): string
58
{
59
    // Don't print empty lines
60
    $lines = \array_filter($lines, function (string $line) {
61
        return $line !== '';
62
    });
63
64
    return printArray("\n", $lines);
65
}
66
67
/**
68
 * @param array $fields
69
 * @return string
70
 */
71
function printInputFields(array $fields): string
72
{
73
    return '(' . printArray(', ', $fields) . ')';
74
}
75
76
/**
77
 * @param string $glue
78
 * @param array  $items
79
 * @return string
80
 */
81
function printArray(string $glue, array $items): string
82
{
83
    return \implode($glue, $items);
84
}
85