|
| 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 | + public const FORMAT_JETBRAINS_ATTRIBUTE = 'jetbrains_attribute'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param self::FORMAT_* $format |
| 39 | + */ |
| 40 | + public static function generate(ArrayNode $node, string $format): string |
| 41 | + { |
| 42 | + if (self::FORMAT_PHPDOC === $format) { |
| 43 | + return self::prependPhpDocWithStar(self::doGeneratePhpDoc($node)); |
| 44 | + } |
| 45 | + |
| 46 | + if (self::FORMAT_JETBRAINS_ATTRIBUTE === $format) { |
| 47 | + return self::doGenerateJetBrainsArrayShape($node); |
| 48 | + } |
| 49 | + |
| 50 | + throw new \LogicException(\sprintf('Unsupported format to generate array shape. Expected one of "%s", got "%s".', implode('", "', [self::FORMAT_PHPDOC, self::FORMAT_JETBRAINS_ATTRIBUTE]), $format)); |
| 51 | + } |
| 52 | + |
| 53 | + private static function doGeneratePhpDoc(NodeInterface $node, int $nestingLevel = 1): string |
| 54 | + { |
| 55 | + if (!$node instanceof ArrayNode) { |
| 56 | + return $node->getName(); |
| 57 | + } |
| 58 | + |
| 59 | + if (!$children = $node->getChildren()) { |
| 60 | + return 'array<array-key, mixed>'; |
| 61 | + } |
| 62 | + |
| 63 | + if ($node instanceof PrototypedArrayNode) { |
| 64 | + $prototype = $node->getPrototype(); |
| 65 | + if ($prototype instanceof ArrayNode) { |
| 66 | + return 'array<'.self::doGeneratePhpDoc($prototype, 1 + $nestingLevel).'>'; |
| 67 | + } |
| 68 | + |
| 69 | + return 'array<'.self::handleNodeType($prototype).'>'; |
| 70 | + } |
| 71 | + |
| 72 | + $arrayShape = "array{\n"; |
| 73 | + |
| 74 | + $handleNode = function (NodeInterface $node, int $nestingLevel): string { |
| 75 | + if ($node instanceof ArrayNode) { |
| 76 | + return self::doGeneratePhpDoc($node, 1 + $nestingLevel); |
| 77 | + } else { |
| 78 | + return self::handleNodeType($node); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + /** @var NodeInterface $child */ |
| 83 | + foreach ($children as $child) { |
| 84 | + $arrayShape .= str_repeat(' ', $nestingLevel * 4).self::dumpNodeKey($child).': '; |
| 85 | + |
| 86 | + if ($child instanceof PrototypedArrayNode) { |
| 87 | + $arrayShape .= 'array<'.$handleNode($child->getPrototype(), $nestingLevel).'>'; |
| 88 | + } else { |
| 89 | + $arrayShape .= $handleNode($child, $nestingLevel); |
| 90 | + } |
| 91 | + |
| 92 | + $arrayShape .= ",\n"; |
| 93 | + } |
| 94 | + |
| 95 | + return $arrayShape.str_repeat(' ', 4 * ($nestingLevel - 1)).'}'; |
| 96 | + } |
| 97 | + |
| 98 | + private static function doGenerateJetBrainsArrayShape(NodeInterface $node, int $nestingLevel = 1): string |
| 99 | + { |
| 100 | + if (!$node instanceof ArrayNode) { |
| 101 | + return $node->getName(); |
| 102 | + } |
| 103 | + |
| 104 | + $children = $node->getChildren(); |
| 105 | + |
| 106 | + $shape = ''; |
| 107 | + if (1 === $nestingLevel) { |
| 108 | + $shape = '#[ArrayShape('; |
| 109 | + } elseif (!$children) { |
| 110 | + return "'array<array-key, mixed>'"; |
| 111 | + } |
| 112 | + |
| 113 | + if ($node instanceof PrototypedArrayNode) { |
| 114 | + $prototype = $node->getPrototype(); |
| 115 | + if ($prototype instanceof ArrayNode) { |
| 116 | + return 'array<'.self::doGeneratePhpDoc($prototype, 1 + $nestingLevel).'>'; |
| 117 | + } |
| 118 | + |
| 119 | + return 'array<'.self::handleNodeType($prototype).'>'; |
| 120 | + } |
| 121 | + |
| 122 | + $shape .= "[\n"; |
| 123 | + |
| 124 | + $handleNode = function (NodeInterface $node, int $nestingLevel): string { |
| 125 | + if ($node instanceof ArrayNode) { |
| 126 | + return self::doGenerateJetBrainsArrayShape($node, 1 + $nestingLevel); |
| 127 | + } |
| 128 | + |
| 129 | + return "'".self::handleNodeType($node)."'"; |
| 130 | + }; |
| 131 | + |
| 132 | + /** @var BaseNode $child */ |
| 133 | + foreach ($children as $child) { |
| 134 | + $shape .= \sprintf("%s'%s' => ", str_repeat(' ', 4 * $nestingLevel), $child->getName()); |
| 135 | + |
| 136 | + if ($child instanceof PrototypedArrayNode) { |
| 137 | + $shape .= '['.$handleNode($child->getPrototype(), $nestingLevel).']'; |
| 138 | + } else { |
| 139 | + $shape .= $handleNode($child, $nestingLevel); |
| 140 | + } |
| 141 | + |
| 142 | + $shape .= ','.self::generateInlinePhpDocForNode($child)."\n"; |
| 143 | + } |
| 144 | + |
| 145 | + $shape .= str_repeat(' ', 4 * ($nestingLevel - 1)).']'; |
| 146 | + |
| 147 | + return $shape.(1 === $nestingLevel ? ')]' : ''); |
| 148 | + } |
| 149 | + |
| 150 | + private static function dumpNodeKey(NodeInterface $node): string |
| 151 | + { |
| 152 | + return $node->getName().($node->isRequired() ? '' : '?'); |
| 153 | + } |
| 154 | + |
| 155 | + private static function handleNumericNode(NumericNode $node): string |
| 156 | + { |
| 157 | + if ($node instanceof IntegerNode) { |
| 158 | + $type = 'int<%s, %s>'; |
| 159 | + } elseif ($node instanceof FloatNode) { |
| 160 | + $type = 'float<%s, %s>'; |
| 161 | + } else { |
| 162 | + $type = 'int<%s, %s>|float<%1$s, %2$s>'; |
| 163 | + } |
| 164 | + |
| 165 | + $min = $node->getMin() ?? 'min'; |
| 166 | + $max = $node->getMax() ?? 'max'; |
| 167 | + |
| 168 | + return \sprintf($type, $min, $max); |
| 169 | + } |
| 170 | + |
| 171 | + private static function prependPhpDocWithStar(string $shape): string |
| 172 | + { |
| 173 | + return str_replace("\n", "\n * ", $shape); |
| 174 | + } |
| 175 | + |
| 176 | + private static function generateInlinePhpDocForNode(BaseNode $node): string |
| 177 | + { |
| 178 | + $hasContent = false; |
| 179 | + $comment = ' /* '; |
| 180 | + |
| 181 | + if ($node->hasDefaultValue() || $node->getInfo() || $node->isDeprecated()) { |
| 182 | + if ($node->isDeprecated()) { |
| 183 | + $hasContent = true; |
| 184 | + $comment .= 'Deprecated: '.$node->getDeprecation($node->getName(), $node->getPath())['message'].' '; |
| 185 | + } |
| 186 | + |
| 187 | + if ($info = $node->getInfo()) { |
| 188 | + $hasContent = true; |
| 189 | + $comment .= $info.' '; |
| 190 | + } |
| 191 | + |
| 192 | + if ($node->hasDefaultValue() && !\is_array($defaultValue = $node->getDefaultValue())) { |
| 193 | + $hasContent = true; |
| 194 | + $comment .= 'Default: '.json_encode($defaultValue).'. '; |
| 195 | + } |
| 196 | + |
| 197 | + $comment .= '*/'; |
| 198 | + } |
| 199 | + |
| 200 | + return $hasContent ? $comment : ''; |
| 201 | + } |
| 202 | + |
| 203 | + private static function handleNodeType(NodeInterface $node): string |
| 204 | + { |
| 205 | + return match (true) { |
| 206 | + $node instanceof BooleanNode => 'bool', |
| 207 | + $node instanceof StringNode => 'string', |
| 208 | + $node instanceof NumericNode => self::handleNumericNode($node), |
| 209 | + $node instanceof EnumNode => $node->getPermissibleValues('|'), |
| 210 | + $node instanceof ScalarNode => 'string|int|float|bool', |
| 211 | + $node instanceof VariableNode => 'mixed', |
| 212 | + }; |
| 213 | + } |
| 214 | +} |
0 commit comments