BlockString::value()   C
last analyzed

Complexity

Conditions 12
Paths 32

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
rs 6.9666
ccs 22
cts 22
cp 1
cc 12
nc 32
nop 1
crap 12

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
declare(strict_types=1);
4
5
namespace GraphQL\Utils;
6
7
use function array_pop;
8
use function array_shift;
9
use function count;
10
use function implode;
11
use function mb_strlen;
12
use function mb_substr;
13
use function preg_split;
14
use function trim;
15
16
class BlockString
17
{
18
    /**
19
     * Produces the value of a block string from its parsed raw value, similar to
20
     * Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc.
21
     *
22
     * This implements the GraphQL spec's BlockStringValue() static algorithm.
23
     */
24 24
    public static function value($rawString)
25
    {
26
        // Expand a block string's raw value into independent lines.
27 24
        $lines = preg_split("/\\r\\n|[\\n\\r]/", $rawString);
28
29
        // Remove common indentation from all lines but first.
30 24
        $commonIndent = null;
31 24
        $linesLength  = 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

31
        $linesLength  = count(/** @scrutinizer ignore-type */ $lines);
Loading history...
32
33 24
        for ($i = 1; $i < $linesLength; $i++) {
34 21
            $line   = $lines[$i];
35 21
            $indent = self::leadingWhitespace($line);
36
37 21
            if ($indent >= mb_strlen($line) ||
38 21
                ($commonIndent !== null && $indent >= $commonIndent)
39
            ) {
40 20
                continue;
41
            }
42
43 18
            $commonIndent = $indent;
44 18
            if ($commonIndent === 0) {
45 4
                break;
46
            }
47
        }
48
49 24
        if ($commonIndent) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $commonIndent of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50 17
            for ($i = 1; $i < $linesLength; $i++) {
51 17
                $line      = $lines[$i];
52 17
                $lines[$i] = mb_substr($line, $commonIndent);
53
            }
54
        }
55
56
        // Remove leading and trailing blank lines.
57 24
        while (count($lines) > 0 && trim($lines[0], " \t") === '') {
58 18
            array_shift($lines);
59
        }
60 24
        while (count($lines) > 0 && trim($lines[count($lines) - 1], " \t") === '') {
61 21
            array_pop($lines);
62
        }
63
64
        // Return a string of the lines joined with U+000A.
65 24
        return implode("\n", $lines);
66
    }
67
68 21
    private static function leadingWhitespace($str)
69
    {
70 21
        $i = 0;
71 21
        while ($i < mb_strlen($str) && ($str[$i] === ' ' || $str[$i] === '\t')) {
72 20
            $i++;
73
        }
74
75 21
        return $i;
76
    }
77
}
78