AST.php
2 months ago
ASTDefinitionBuilder.php
2 months ago
BreakingChangesFinder.php
2 months ago
BuildClientSchema.php
2 months ago
BuildSchema.php
2 months ago
InterfaceImplementations.php
2 months ago
LazyException.php
2 months ago
LexicalDistance.php
2 months ago
MixedStore.php
2 months ago
PairSet.php
2 months ago
PhpDoc.php
2 months ago
SchemaExtender.php
2 months ago
SchemaPrinter.php
2 months ago
TypeComparators.php
2 months ago
TypeInfo.php
2 months ago
Utils.php
2 months ago
Value.php
2 months ago
PhpDoc.php
53 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Utils; |
| 4 | |
| 5 | class PhpDoc |
| 6 | { |
| 7 | /** @param string|false|null $docBlock */ |
| 8 | public static function unwrap($docBlock): ?string |
| 9 | { |
| 10 | if ($docBlock === false || $docBlock === null) { |
| 11 | return null; |
| 12 | } |
| 13 | |
| 14 | $content = preg_replace('~([\r\n]) \* (.*)~i', '$1$2', $docBlock); // strip * |
| 15 | assert(is_string($content), 'regex is statically known to be valid'); |
| 16 | |
| 17 | $content = preg_replace('~([\r\n])[\* ]+([\r\n])~i', '$1$2', $content); // strip single-liner * |
| 18 | assert(is_string($content), 'regex is statically known to be valid'); |
| 19 | |
| 20 | $content = substr($content, 3); // strip leading /** |
| 21 | $content = substr($content, 0, -2); // strip trailing */ |
| 22 | |
| 23 | return static::nonEmptyOrNull($content); |
| 24 | } |
| 25 | |
| 26 | /** @param string|false|null $docBlock */ |
| 27 | public static function unpad($docBlock): ?string |
| 28 | { |
| 29 | if ($docBlock === false || $docBlock === null) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | $lines = explode("\n", $docBlock); |
| 34 | $lines = array_map( |
| 35 | static fn (string $line): string => ' ' . trim($line), |
| 36 | $lines |
| 37 | ); |
| 38 | |
| 39 | $content = implode("\n", $lines); |
| 40 | |
| 41 | return static::nonEmptyOrNull($content); |
| 42 | } |
| 43 | |
| 44 | protected static function nonEmptyOrNull(string $maybeEmptyString): ?string |
| 45 | { |
| 46 | $trimmed = trim($maybeEmptyString); |
| 47 | |
| 48 | return $trimmed === '' |
| 49 | ? null |
| 50 | : $trimmed; |
| 51 | } |
| 52 | } |
| 53 |