AST
2 months ago
BlockString.php
2 months ago
DirectiveLocation.php
2 months ago
Lexer.php
2 months ago
Parser.php
2 months ago
Printer.php
2 months ago
Source.php
2 months ago
SourceLocation.php
2 months ago
Token.php
2 months ago
Visitor.php
2 months ago
VisitorOperation.php
2 months ago
VisitorRemoveNode.php
2 months ago
VisitorSkipNode.php
2 months ago
VisitorStop.php
2 months ago
BlockString.php
156 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Language; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Utils\Utils; |
| 6 | |
| 7 | /** |
| 8 | * @see \Automattic\WooCommerce\Vendor\GraphQL\Tests\Language\BlockStringTest |
| 9 | */ |
| 10 | class BlockString |
| 11 | { |
| 12 | /** |
| 13 | * Produces the value of a block string from its parsed raw value, similar to |
| 14 | * CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc. |
| 15 | * |
| 16 | * This implements the Automattic\WooCommerce\Vendor\GraphQL spec's BlockStringValue() static algorithm. |
| 17 | */ |
| 18 | public static function dedentBlockStringLines(string $rawString): string |
| 19 | { |
| 20 | $lines = Utils::splitLines($rawString); |
| 21 | |
| 22 | // Remove common indentation from all lines but first. |
| 23 | $commonIndent = self::getIndentation($rawString); |
| 24 | $linesLength = count($lines); |
| 25 | |
| 26 | if ($commonIndent > 0) { |
| 27 | for ($i = 1; $i < $linesLength; ++$i) { |
| 28 | $lines[$i] = mb_substr($lines[$i], $commonIndent); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Remove leading and trailing blank lines. |
| 33 | $startLine = 0; |
| 34 | while ($startLine < $linesLength && self::isBlank($lines[$startLine])) { |
| 35 | ++$startLine; |
| 36 | } |
| 37 | |
| 38 | $endLine = $linesLength; |
| 39 | while ($endLine > $startLine && self::isBlank($lines[$endLine - 1])) { |
| 40 | --$endLine; |
| 41 | } |
| 42 | |
| 43 | // Return a string of the lines joined with U+000A. |
| 44 | return implode("\n", array_slice($lines, $startLine, $endLine - $startLine)); |
| 45 | } |
| 46 | |
| 47 | private static function isBlank(string $str): bool |
| 48 | { |
| 49 | $strLength = mb_strlen($str); |
| 50 | for ($i = 0; $i < $strLength; ++$i) { |
| 51 | if ($str[$i] !== ' ' && $str[$i] !== '\t') { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | public static function getIndentation(string $value): int |
| 60 | { |
| 61 | $isFirstLine = true; |
| 62 | $isEmptyLine = true; |
| 63 | $indent = 0; |
| 64 | $commonIndent = null; |
| 65 | $valueLength = mb_strlen($value); |
| 66 | |
| 67 | for ($i = 0; $i < $valueLength; ++$i) { |
| 68 | switch (Utils::charCodeAt($value, $i)) { |
| 69 | case 13: // \r |
| 70 | if (Utils::charCodeAt($value, $i + 1) === 10) { |
| 71 | ++$i; // skip \r\n as one symbol |
| 72 | } |
| 73 | // falls through |
| 74 | // no break |
| 75 | case 10: // \n |
| 76 | $isFirstLine = false; |
| 77 | $isEmptyLine = true; |
| 78 | $indent = 0; |
| 79 | break; |
| 80 | case 9: // \t |
| 81 | case 32: // <space> |
| 82 | ++$indent; |
| 83 | break; |
| 84 | default: |
| 85 | if ( |
| 86 | $isEmptyLine |
| 87 | && ! $isFirstLine |
| 88 | && ($commonIndent === null || $indent < $commonIndent) |
| 89 | ) { |
| 90 | $commonIndent = $indent; |
| 91 | } |
| 92 | |
| 93 | $isEmptyLine = false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $commonIndent ?? 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Print a block string in the indented block form by adding a leading and |
| 102 | * trailing blank line. However, if a block string starts with whitespace and is |
| 103 | * a single-line, adding a leading blank line would strip that whitespace. |
| 104 | */ |
| 105 | public static function print(string $value): string |
| 106 | { |
| 107 | $escapedValue = str_replace('"""', '\\"""', $value); |
| 108 | |
| 109 | // Expand a block string's raw value into independent lines. |
| 110 | $lines = Utils::splitLines($escapedValue); |
| 111 | $isSingleLine = count($lines) === 1; |
| 112 | |
| 113 | // If common indentation is found we can fix some of those cases by adding leading new line |
| 114 | $forceLeadingNewLine = count($lines) > 1; |
| 115 | foreach ($lines as $i => $line) { |
| 116 | if ($i === 0) { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | if ($line !== '' && preg_match('/^\s/', $line) !== 1) { |
| 121 | $forceLeadingNewLine = false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Trailing triple quotes just looks confusing but doesn't force trailing new line |
| 126 | $hasTrailingTripleQuotes = preg_match('/\\\\"""$/', $escapedValue) === 1; |
| 127 | |
| 128 | // Trailing quote (single or double) or slash forces trailing new line |
| 129 | $hasTrailingQuote = preg_match('/"$/', $value) === 1 && ! $hasTrailingTripleQuotes; |
| 130 | $hasTrailingSlash = preg_match('/\\\\$/', $value) === 1; |
| 131 | $forceTrailingNewline = $hasTrailingQuote || $hasTrailingSlash; |
| 132 | |
| 133 | // add leading and trailing new lines only if it improves readability |
| 134 | $printAsMultipleLines = ! $isSingleLine |
| 135 | || mb_strlen($value) > 70 |
| 136 | || $forceTrailingNewline |
| 137 | || $forceLeadingNewLine |
| 138 | || $hasTrailingTripleQuotes; |
| 139 | |
| 140 | $result = ''; |
| 141 | |
| 142 | // Format a multi-line block quote to account for leading space. |
| 143 | $skipLeadingNewLine = $isSingleLine && preg_match('/^\s/', $value) === 1; |
| 144 | if (($printAsMultipleLines && ! $skipLeadingNewLine) || $forceLeadingNewLine) { |
| 145 | $result .= "\n"; |
| 146 | } |
| 147 | |
| 148 | $result .= $escapedValue; |
| 149 | if ($printAsMultipleLines) { |
| 150 | $result .= "\n"; |
| 151 | } |
| 152 | |
| 153 | return '"""' . $result . '"""'; |
| 154 | } |
| 155 | } |
| 156 |