simPod /
graphql-php
| 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
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
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 For 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 |