Completed
Pull Request — master (#80)
by Christoffer
02:18
created

blockStringValue()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 18
nc 40
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
/**
6
 * Produces the value of a block string from its parsed raw value, similar to
7
 * Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc.
8
 * This implements the GraphQL spec's BlockStringValue() static algorithm.
9
 *
10
 * @param string $rawString
11
 * @return string
12
 */
13
function blockStringValue(string $rawString): string
14
{
15
    $lines     = preg_split("/\r\n|[\n\r]/", $rawString);
16
    $lineCount = count($lines);
0 ignored issues
show
Bug introduced by
It seems like $lines 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

16
    $lineCount = count(/** @scrutinizer ignore-type */ $lines);
Loading history...
17
18
    $commonIndent = null;
19
20
    for ($i = 1; $i < $lineCount; $i++) {
21
        $line   = $lines[$i];
22
        $indent = leadingWhitespace($line);
23
24
        if ($indent < mb_strlen($line) && ($commonIndent === null || $indent < $commonIndent)) {
25
            $commonIndent = $indent;
26
27
            if ($commonIndent === 0) {
28
                break;
29
            }
30
        }
31
    }
32
33
    if ($commonIndent > 0) {
34
        for ($i = 1; $i < $lineCount; $i++) {
35
            $lines[$i] = sliceString($lines[$i], $commonIndent);
0 ignored issues
show
Bug introduced by
It seems like $commonIndent can also be of type null; however, parameter $start of Digia\GraphQL\Language\sliceString() does only seem to accept integer, 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
            $lines[$i] = sliceString($lines[$i], /** @scrutinizer ignore-type */ $commonIndent);
Loading history...
36
        }
37
    }
38
39
    while (count($lines) > 0 && isBlank($lines[0])) {
40
        array_shift($lines);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $array of array_shift() 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

40
        array_shift(/** @scrutinizer ignore-type */ $lines);
Loading history...
41
    }
42
43
    while (($lineCount = count($lines)) > 0 && isBlank($lines[$lineCount - 1])) {
44
        array_pop($lines);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $array of array_pop() 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

44
        array_pop(/** @scrutinizer ignore-type */ $lines);
Loading history...
45
    }
46
47
    return implode("\n", $lines);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $pieces of implode() 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

47
    return implode("\n", /** @scrutinizer ignore-type */ $lines);
Loading history...
48
}
49
50
/**
51
 * @param string $string
52
 * @return int
53
 */
54
function leadingWhitespace(string $string): int
55
{
56
    $i      = 0;
57
    $length = mb_strlen($string);
58
    while ($i < $length && ($string[$i] === ' ' || $string[$i] === "\t")) {
59
        $i++;
60
    }
61
    return $i;
62
}
63
64
/**
65
 * @param string $string
66
 * @return bool
67
 */
68
function isBlank(string $string): bool
69
{
70
    return leadingWhitespace($string) === mb_strlen($string);
71
}
72