|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Config\Builder; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Definition\ArrayNode; |
| 15 | +use Symfony\Component\Config\Definition\BaseNode; |
| 16 | +use Symfony\Component\Config\Definition\BooleanNode; |
| 17 | +use Symfony\Component\Config\Definition\EnumNode; |
| 18 | +use Symfony\Component\Config\Definition\FloatNode; |
| 19 | +use Symfony\Component\Config\Definition\IntegerNode; |
| 20 | +use Symfony\Component\Config\Definition\NodeInterface; |
| 21 | +use Symfony\Component\Config\Definition\NumericNode; |
| 22 | +use Symfony\Component\Config\Definition\PrototypedArrayNode; |
| 23 | +use Symfony\Component\Config\Definition\ScalarNode; |
| 24 | +use Symfony\Component\Config\Definition\StringNode; |
| 25 | +use Symfony\Component\Config\Definition\VariableNode; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Alexandre Daubois <alex.daubois@gmail.com> |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +final class ArrayShapeGenerator |
| 33 | +{ |
| 34 | + public const FORMAT_PHPDOC = 'phpdoc'; |
| 35 | + |
| 36 | + public static function generate(ArrayNode $node): string |
| 37 | + { |
| 38 | + return self::prependPhpDocWithStar(self::doGeneratePhpDoc($node)); |
| 39 | + } |
| 40 | + |
| 41 | + private static function doGeneratePhpDoc(NodeInterface $node, int $nestingLevel = 1): string |
| 42 | + { |
| 43 | + if (!$node instanceof ArrayNode) { |
| 44 | + return $node->getName(); |
| 45 | + } |
| 46 | + |
| 47 | + if ($node instanceof PrototypedArrayNode) { |
| 48 | + $isHashmap = (bool) $node->getKeyAttribute(); |
| 49 | + |
| 50 | + $prototype = $node->getPrototype(); |
| 51 | + if ($prototype instanceof ArrayNode) { |
| 52 | + return 'array<'.($isHashmap ? 'string, ' : '').self::doGeneratePhpDoc($prototype, $nestingLevel).'>'; |
| 53 | + } |
| 54 | + |
| 55 | + return 'array<'.($isHashmap ? 'string, ' : '').self::handleScalarNode($prototype).'>'; |
| 56 | + } |
| 57 | + |
| 58 | + if (!($children = $node->getChildren()) && !$node->getParent() instanceof PrototypedArrayNode) { |
| 59 | + return 'array<array-key, mixed>'; |
| 60 | + } |
| 61 | + |
| 62 | + $arrayShape = \sprintf("array{%s\n", self::generateInlinePhpDocForNode($node)); |
| 63 | + |
| 64 | + /** @var NodeInterface $child */ |
| 65 | + foreach ($children as $child) { |
| 66 | + $arrayShape .= str_repeat(' ', $nestingLevel * 4).self::dumpNodeKey($child).': '; |
| 67 | + |
| 68 | + if ($child instanceof PrototypedArrayNode) { |
| 69 | + $isHashmap = (bool) $child->getKeyAttribute(); |
| 70 | + |
| 71 | + $arrayShape .= 'array<'.($isHashmap ? 'string, ' : '').self::handleNode($child->getPrototype(), $nestingLevel, self::FORMAT_PHPDOC).'>'; |
| 72 | + } else { |
| 73 | + $arrayShape .= self::handleNode($child, $nestingLevel, self::FORMAT_PHPDOC); |
| 74 | + } |
| 75 | + |
| 76 | + $arrayShape .= \sprintf(",%s\n", !$child instanceof ArrayNode ? self::generateInlinePhpDocForNode($child) : ''); |
| 77 | + } |
| 78 | + |
| 79 | + return $arrayShape.str_repeat(' ', 4 * ($nestingLevel - 1)).'}'; |
| 80 | + } |
<
325D
td data-grid-cell-id="diff-ecd29623aefc0d05358573ce718e8e5d63a80507e4df7991083b6e1c4730e058-empty-81-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
81 | + |
| 82 | + private static function dumpNodeKey(NodeInterface $node): string |
| 83 | + { |
| 84 | + $name = $node->getName(); |
| 85 | + $quoted = str_starts_with($name, '@') |
| 86 | + || \in_array(strtolower($name), ['int', 'float', 'bool', 'null', 'scalar'], true) |
| 87 | + || strpbrk($name, '\'"'); |
| 88 | + |
| 89 | + if ($quoted) { |
| 90 | + $name = "'".addslashes($name)."'"; |
| 91 | + } |
| 92 | + |
| 93 | + return $name.($node->isRequired() ? '' : '?'); |
| 94 | + } |
| 95 | + |
| 96 | + private static function handleNumericNode(NumericNode $node): string |
| 97 | + { |
| 98 | + $min = $node->getMin() ?? 'min'; |
| 99 | + $max = $node->getMax() ?? 'max'; |
| 100 | + |
| 101 | + if ($node instanceof IntegerNode) { |
| 102 | + return \sprintf('int<%s, %s>', $min, $max); |
| 103 | + } elseif ($node instanceof FloatNode) { |
| 104 | + return 'float'; |
| 105 | + } |
| 106 | + |
| 107 | + return \sprintf('int<%s, %s>|float', $min, $max); |
| 108 | + } |
| 109 | + |
| 110 | + private static function prependPhpDocWithStar(string $shape): string |
| 111 | + { |
| 112 | + return str_replace("\n", "\n * ", $shape); |
| 113 | + } |
| 114 | + |
| 115 | + private static function generateInlinePhpDocForNode(BaseNode $node): string |
| 116 | + { |
| 117 | + $hasContent = false; |
| 118 | + $comment = ' // '; |
| 119 | + |
| 120 | + if ($node->hasDefaultValue() || $node->getInfo() || $node->isDeprecated()) { |
| 121 | + if ($node->isDeprecated()) { |
| 122 | + $hasContent = true; |
| 123 | + $comment .= 'Deprecated: '.$node->getDeprecation($node->getName(), $node->getPath())['message'].' '; |
| 124 | + } |
| 125 | + |
| 126 | + if ($info = $node->getInfo()) { |
| 127 | + $hasContent = true; |
| 128 | + $comment .= $info.' '; |
| 129 | + } |
| 130 | + |
| 131 | + if ($node->hasDefaultValue() && !\is_array($defaultValue = $node->getDefaultValue())) { |
| 132 | + $hasContent = true; |
| 133 | + $comment .= 'Default: '.json_encode($defaultValue, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return $hasContent ? rtrim($comment) : ''; |
| 138 | + } |
| 139 | + |
| 140 | + private static function handleNode(NodeInterface $node, int $nestingLevel, string $format): string |
| 141 | + { |
| 142 | + if ($node instanceof ArrayNode) { |
| 143 | + return self::doGeneratePhpDoc($node, 1 + $nestingLevel); |
| 144 | + } |
| 145 | + |
| 146 | + return self::handleScalarNode($node); |
| 147 | + } |
| 148 | + |
| 149 | + private static function handleScalarNode(NodeInterface $node): string |
| 150 | + { |
| 151 | + return match (true) { |
| 152 | + $node instanceof BooleanNode => 'bool', |
| 153 | + $node instanceof StringNode => 'string', |
| 154 | + $node instanceof NumericNode => self::handleNumericNode($node), |
| 155 | + $node instanceof EnumNode => $node->getPermissibleValues('|'), |
| 156 | + $node instanceof ScalarNode => 'string|int|float|bool', |
| 157 | + $node instanceof VariableNode => 'mixed', |
| 158 | + }; |
| 159 | + } |
| 160 | +} |
0 commit comments