Passed
Pull Request — master (#209)
by Christoffer
02:17
created

descriptionLines()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 2
dl 0
loc 21
rs 9.0534
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
    $lines         = [];
13
    $rawLines      = \explode("\n", $description);
14
    $rawLinesCount = \count($rawLines);
15
16
    for ($i = 0; $i < $rawLinesCount; $i++) {
17
        if ('' === $rawLines[$i]) {
18
            $lines[] = $rawLines[$i];
19
            continue;
20
        }
21
22
        // For > 120 character long lines, cut at space boundaries into sublines
23
        // of ~80 chars.
24
        $subLines = breakLine($rawLines[$i], $maxLength);
25
        for ($j = 0; $j < \count($subLines); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
26
            $lines[] = $subLines[$j];
27
        }
28
    }
29
30
    return $lines;
31
}
32
33
/**
34
 * @param string $line
35
 * @param int    $maxLength
36
 * @return array
37
 */
38
function breakLine(string $line, int $maxLength): array
39
{
40
    if (\strlen($line) < ($maxLength + 5)) {
41
        return [$line];
42
    }
43
44
    $pos        = $maxLength - 40;
45
    $parts      = \preg_split("/((?: |^).{15,{$pos}}(?= |$))/", $line);
46
    $partsCount = \count($parts);
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $var of count() does only seem to accept Countable|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

46
    $partsCount = \count(/** @scrutinizer ignore-type */ $parts);
Loading history...
47
48
    if ($partsCount < 4) {
49
        return [$line];
50
    }
51
52
    $subLines = [$parts[0] . $parts[1] . $parts[2]];
53
54
    for ($i = 3; $i < $partsCount; $i++) {
55
        $subLines[] = \array_slice($parts[$i], 1) . $parts[$i + 1];
0 ignored issues
show
Bug introduced by
Are you sure array_slice($parts[$i], 1) of type array can be used in concatenation? ( Ignorable by Annotation )

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

55
        $subLines[] = /** @scrutinizer ignore-type */ \array_slice($parts[$i], 1) . $parts[$i + 1];
Loading history...
56
    }
57
58
    return $subLines;
59
}
60
61
/**
62
 * @param string $line
63
 * @return string
64
 */
65
function escapeQuote(string $line): string
66
{
67
    return \preg_replace('/"""/g', '\\"""', $line);
68
}
69